Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
model = tf.keras.models.load_model('946')
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
@app.post("/classify_plant")
|
| 12 |
+
async def classify(image: UploadFile = File(...)):
|
| 13 |
+
if image is not None:
|
| 14 |
+
img = Image.open(io.BytesIO(await image.read()))
|
| 15 |
+
img = img.resize((64,64))
|
| 16 |
+
img_array = np.array(img) / 255.0
|
| 17 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 18 |
+
predictions = model.predict(img_array)
|
| 19 |
+
predicted_class_idx = np.argmax(predictions)
|
| 20 |
+
predicted_class_idx = int(predicted_class_idx)
|
| 21 |
+
return {"prediction": predicted_class_idx}
|
| 22 |
+
else:
|
| 23 |
+
return {"error": "No image provided"}
|