-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (42 loc) · 1.41 KB
/
app.py
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
import numpy as np
from fastapi import FastAPI, UploadFile, File
from io import BytesIO
from keras.preprocessing import image
from keras.models import load_model
# Load the saved Keras model
model = load_model('PoultryModel.h5')
# Define a function to preprocess the input image
def preprocess_image(img):
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
return img_array
# Define a function to make predictions
def predict_image(img, model):
# Preprocess the image
img = preprocess_image(img)
# Make prediction
prediction = model.predict(img)
return prediction
def interpret_prediction(prediction):
classes = ['cocci', 'healthy', 'NCD', 'salmo']
threshold = 0.5
best_score_index = 0
best_score = prediction[0][0]
for i, score in enumerate(prediction[0]):
if score > best_score:
best_score = score
best_score_index = i
if best_score >= threshold:
return classes[best_score_index]
else:
return "Unknown"
app = FastAPI()
@app.post("/predict/")
async def predict(file: UploadFile = File(...)):
contents = await file.read()
img = image.load_img(BytesIO(contents), target_size=(256, 256))
prediction = predict_image(img, model)
return {"prediction": interpret_prediction(prediction)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)