-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
59 lines (49 loc) · 1.77 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
51
52
53
54
55
56
57
58
59
# Library imports
import numpy as np
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import PlainTextResponse
import numpy as np
import io
from PIL import Image
import cv2
from keras.models import load_model
import warnings
warnings.filterwarnings("ignore")
model = load_model('plant_classifier.h5')
# Name of Classes
target_names = ['Healthy', 'Powdery', 'Rust']
app = FastAPI(
title="Plant Disease Detection API",
description="""An API that utilises a Deep Learning model built with Keras(Tensorflow) to detect if a plant is healthy or suffering from Rust and Powder formation.""",
version="0.0.1",
debug=True,
)
@app.get("/", response_class=PlainTextResponse)
async def running():
note = """
Plant Disease Detection API 🙌🏻
Note: add "/docs" to the URL to get the Swagger UI Docs or "/redoc"
"""
return note
favicon_path = "favicon.png"
@app.post("/predict")
async def root(file: UploadFile = File(...)):
"""
The root function returns the prediction of an image using a pretrained model.
Parameters:
file (UploadFile): The image to be predicted.
Returns:
result (str): The prediction of the image as a string.
Args:
file:UploadFile=File(...): Specify that the file is uploaded as a multipart/form-data request
Returns:
The prediction of the model in json format
"""
contents = io.BytesIO(await file.read())
file_bytes = np.asarray(bytearray(contents.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
img = cv2.resize(img, (80, 80))
img.shape = (1, 80, 80, 3)
image = model.predict(img)
result = target_names[np.argmax(image)]
return (str("Result from prediction: " +result + " plant."))