File size: 2,019 Bytes
688a486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from io import BytesIO

app = FastAPI()

# Enable CORS to allow requests from frontend (React)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Change ["http://localhost:5173"] for better security
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load your model
model = load_model("densenet201_food_classification.h5")

# Define class indices
class_indices = {
    0: "burger",
    1: "butter_naan",
    2: "chai",
    3: "chapati",
    4: "chole_bhature",
    5: "dal_makhani",
    6: "dhokla",
    7: "fried_rice",
    8: "idli",
    9: "jalebi",
    10: "kaathi_rolls",
    11: "kadai_paneer",
    12: "kulfi",
    13: "masala_dosa",
    14: "momos",
    15: "paani_puri",
    16: "pakode",
    17: "pav_bhaji",
    18: "pizza",
    19: "samosa"
}

def predict_image(image, model):
    try:
        img = load_img(image, target_size=(224, 224))
        image_array = img_to_array(img) / 255.0
        image_array = np.expand_dims(image_array, axis=0)

        predictions = model.predict(image_array)
        class_idx = np.argmax(predictions)
        class_label = class_indices.get(class_idx, "Unknown")
        confidence = float(predictions[0][class_idx])

        return class_label, confidence
    except Exception as e:
        return None, None

@app.post("/predict/")
async def predict(file: UploadFile = File(...)):
    try:
        image_data = await file.read()
        image = BytesIO(image_data)

        class_label, confidence = predict_image(image, model)

        if class_label is None:
            return {"error": "Prediction failed"}

        return {"predicted_class": class_label, "confidence": f"{confidence:.2f}"}
    except Exception as e:
        return {"error": f"Internal Server Error: {str(e)}"}