Spaces:
Runtime error
Runtime error
HashamUllah
commited on
Commit
•
f9e98b8
1
Parent(s):
4f77f85
Upload 4 files
Browse files- app1.py +50 -0
- categories.json +40 -0
- plant_disease_detection.h5 +3 -0
- temp_image.jpg +0 -0
app1.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import json
|
8 |
+
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Load the TensorFlow model
|
12 |
+
model = tf.keras.models.load_model('./plant_disease_detection.h5')
|
13 |
+
|
14 |
+
# Load categories
|
15 |
+
with open('./categories.json') as f:
|
16 |
+
categories = json.load(f)
|
17 |
+
|
18 |
+
def preprocess_image(image_bytes):
|
19 |
+
# Convert the image to a NumPy array
|
20 |
+
image = Image.open(io.BytesIO(image_bytes))
|
21 |
+
image = image.resize((224, 224)) # Adjust size as needed
|
22 |
+
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
|
23 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
24 |
+
return image_array
|
25 |
+
|
26 |
+
@app.post('/predict')
|
27 |
+
async def predict(file: UploadFile = File(...)):
|
28 |
+
if file.content_type.startswith('image/') is False:
|
29 |
+
raise HTTPException(status_code=400, detail='Invalid file type')
|
30 |
+
|
31 |
+
image_bytes = await file.read()
|
32 |
+
image_array = preprocess_image(image_bytes)
|
33 |
+
|
34 |
+
# Make prediction
|
35 |
+
predictions = model.predict(image_array)
|
36 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
37 |
+
|
38 |
+
# Map to category names
|
39 |
+
predicted_label = categories.get(str(predicted_class), 'Unknown')
|
40 |
+
|
41 |
+
return JSONResponse(content={
|
42 |
+
'class': predicted_label,
|
43 |
+
'confidence': float(predictions[0][predicted_class])
|
44 |
+
})
|
45 |
+
|
46 |
+
|
47 |
+
if __name__ == '__main__':
|
48 |
+
import uvicorn
|
49 |
+
uvicorn.run(app, host='0.0.0.0', port=8080)
|
50 |
+
|
categories.json
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"0": "Apple___Apple_scab",
|
3 |
+
"1": "Apple___Black_rot",
|
4 |
+
"2": "Apple___Cedar_apple_rust",
|
5 |
+
"3": "Apple___healthy",
|
6 |
+
"4": "Blueberry___healthy",
|
7 |
+
"5": "Cherry_(including_sour)___Powdery_mildew",
|
8 |
+
"6": "Cherry_(including_sour)___healthy",
|
9 |
+
"7": "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
|
10 |
+
"8": "Corn_(maize)___Common_rust_",
|
11 |
+
"9": "Corn_(maize)___Northern_Leaf_Blight",
|
12 |
+
"10": "Corn_(maize)___healthy",
|
13 |
+
"11": "Grape___Black_rot",
|
14 |
+
"12": "Grape___Esca_(Black_Measles)",
|
15 |
+
"13": "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)",
|
16 |
+
"14": "Grape___healthy",
|
17 |
+
"15": "Orange___Haunglongbing_(Citrus_greening)",
|
18 |
+
"16": "Peach___Bacterial_spot",
|
19 |
+
"17": "Peach___healthy",
|
20 |
+
"18": "Pepper,_bell___Bacterial_spot",
|
21 |
+
"19": "Pepper,_bell___healthy",
|
22 |
+
"20": "Potato___Early_blight",
|
23 |
+
"21": "Potato___Late_blight",
|
24 |
+
"22": "Potato___healthy",
|
25 |
+
"23": "Raspberry___healthy",
|
26 |
+
"24": "Soybean___healthy",
|
27 |
+
"25": "Squash___Powdery_mildew",
|
28 |
+
"26": "Strawberry___Leaf_scorch",
|
29 |
+
"27": "Strawberry___healthy",
|
30 |
+
"28": "Tomato___Bacterial_spot",
|
31 |
+
"29": "Tomato___Early_blight",
|
32 |
+
"30": "Tomato___Late_blight",
|
33 |
+
"31": "Tomato___Leaf_Mold",
|
34 |
+
"32": "Tomato___Septoria_leaf_spot",
|
35 |
+
"33": "Tomato___Spider_mites Two-spotted_spider_mite",
|
36 |
+
"34": "Tomato___Target_Spot",
|
37 |
+
"35": "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
|
38 |
+
"36": "Tomato___Tomato_mosaic_virus",
|
39 |
+
"37": "Tomato___healthy"
|
40 |
+
}
|
plant_disease_detection.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d762c7af973c2e146345f7e7a891b371f48b5ea8c5f16dffbb939eb832d3d21d
|
3 |
+
size 9947768
|
temp_image.jpg
ADDED