CoolFace
Apppublic

ILYAS72066/Chunky_Panday

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
main.py45 linesDownload Raw Back to root
1from fastapi import FastAPI, UploadFile, File
2import numpy as np
3from PIL import Image
4import io
5from tensorflow import keras
6
7# Load model
8model = keras.models.load_model('model.h5')
9
10# Class labels (ordered list)
11class_labels = [
12    'Apple__Apple_scab', 'Apple_Black_rot', 'Apple_Cedar_apple_rust', 'Apple__healthy',
13    'Blueberry__healthy', 'Cherry(including_sour)Powdery_mildew', 'Cherry(including_sour)_healthy',
14    'Corn_(maize)Cercospora_leaf_spot Gray_leaf_spot', 'Corn(maize)Common_rust',
15    'Corn_(maize)Northern_Leaf_Blight', 'Corn(maize)healthy', 'Grape__Black_rot',
16    'Grape__Esca(Black_Measles)', 'Grape__Leaf_blight(Isariopsis_Leaf_Spot)', 'Grape___healthy',
17    'Orange__Haunglongbing(Citrus_greening)', 'Peach__Bacterial_spot', 'Peach__healthy',
18    'Pepper,bell_Bacterial_spot', 'Pepper,_bell_healthy', 'Potato_Early_blight', 'Potato__Late_blight',
19    'Potato__healthy', 'Raspberry_healthy', 'Soybean_healthy', 'Squash__Powdery_mildew',
20    'Strawberry__Leaf_scorch', 'Strawberry_healthy', 'Tomato_Bacterial_spot', 'Tomato__Early_blight',
21    'Tomato__Late_blight', 'Tomato_Leaf_Mold', 'Tomato__Septoria_leaf_spot',
22    'Tomato__Spider_mites Two-spotted_spider_mite', 'Tomato__Target_Spot',
23    'Tomato__Tomato_Yellow_Leaf_Curl_Virus', 'Tomato_Tomato_mosaic_virus', 'Tomato__healthy'
24]
25
26# Initialize FastAPI app
27app = FastAPI()
28
29# Preprocess function
30def preprocess_image(image_bytes):
31    image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
32    image = image.resize((224, 224))
33    img_array = np.array(image) / 255.0  # normalize (assuming your model was trained with normalization)
34    img_array = np.expand_dims(img_array, axis=0)  # add batch dimension
35    return img_array
36
37@app.post("/predict")
38async def predict(file: UploadFile = File(...)):
39    image_bytes = await file.read()
40    img_array = preprocess_image(image_bytes)
41    predictions = model.predict(img_array)
42    predicted_class = class_labels[np.argmax(predictions)]
43    confidence = float(np.max(predictions))
44    return {"prediction": predicted_class, "confidence": confidence}
45