-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (104 loc) · 3.63 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import io
from PIL import Image
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
from pydantic import BaseModel
from fastapi import FastAPI, Form, UploadFile
from typing import Annotated
# Tensorflow
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import get_file
from tensorflow.keras.utils import load_img
from tensorflow.keras.utils import img_to_array
import tensorflow_decision_forests
import numpy as np
from data import crop_data, leaf_disease
from gcs import GCStorage
app = FastAPI()
origins = ["*"]
methods = ["*"]
headers = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=methods,
allow_headers=headers,
)
@app.get("/")
async def root():
return {"message": "Welcome !"}
class CropInput(BaseModel):
nitrogen: int = 0
phosphorous: int = 0
potassium: int = 0
temperature: int = 0
humidity: int = 0
ph: int = 0
rainfall: int = 0
@app.post("/crop-recommendations")
async def get_crop_recommendations(
nitrogen: Annotated[float, Form()],
phosphorous: Annotated[float, Form()],
potassium: Annotated[float, Form()],
temperature: Annotated[float, Form()],
humidity: Annotated[float, Form()],
ph: Annotated[float, Form()],
rainfall: Annotated[float, Form()]
):
model = tf.saved_model.load('./model/crop_recommdation')
input_data = {
"N": np.array([nitrogen]),
"P": np.array([phosphorous]),
"K": np.array([potassium]),
"temperature": np.array([temperature]),
"humidity": np.array([humidity]),
"ph": np.array([ph]),
"rainfall": np.array([rainfall])
}
pred = model.serve(input_data)
# Daftar nama kelas
class_names = ['rice', 'maize', 'chickpea', 'kidneybeans', 'pigeonpeas', 'mothbeans', 'mungbean', 'blackgram',
'lentil', 'pomegranate', 'banana', 'mango', 'grapes', 'watermelon', 'muskmelon', 'apple',
'orange', 'papaya', 'coconut', 'cotton', 'jute', 'coffee']
# Mencari indeks dengan nilai prediksi tertinggi
predicted_label = pred[0]
predicted_index = np.argmax(predicted_label)
# Mengambil nama kelas yang sesuai dengan indeks
predicted_class = class_names[predicted_index]
return crop_data[predicted_class]
def get_predict_detail(predictions, classes):
predicts = []
class_names = ['bacterial_leaf_blight', 'brown_spot', 'healthy', 'leaf_blast', 'leaf_scald', 'narrow_brown_spot']
for x in predictions.tolist():
predicts.append({
"disease": leaf_disease[class_names[x]],
"percentage": classes[x],
})
return predicts
@app.post("/rice-disease-detection")
async def get_rice_leaf_disease_detection(image: UploadFile):
model_dir = "./model/rice_leaf_detection.h5"
model = load_model(model_dir)
contents = image.file.read()
temp_file = io.BytesIO()
temp_file.write(contents)
temp_file.seek(0)
load_image = load_img(temp_file, target_size=(224, 224))
x = img_to_array(load_image)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images)[0]
prediction_probabilities = tf.math.top_k(classes, k=3)
predictions = prediction_probabilities.indices.numpy()
image_link = GCStorage().upload_file(image)
return {
"predictions": get_predict_detail(predictions, classes.tolist()),
"image_url": image_link,
}
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
uvicorn.run(app, host="0.0.0.0", port=8000)