Update app.py
Browse files
app.py
CHANGED
@@ -48,32 +48,41 @@ def get_deepseek_solution(disease_name):
|
|
48 |
|
49 |
def response2(image, image_size=640, conf_threshold=0.3, iou_threshold=0.6):
|
50 |
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
|
51 |
-
|
52 |
text = ""
|
53 |
solution = ""
|
54 |
-
|
55 |
detected_diseases = set()
|
56 |
|
57 |
for r in results:
|
58 |
im_array = r.plot()
|
59 |
im = Image.fromarray(im_array[..., ::-1])
|
|
|
60 |
for r in results:
|
61 |
conf = np.array(r.boxes.conf.cpu())
|
62 |
cls = np.array(r.boxes.cls.cpu()).astype(int)
|
63 |
xywh = np.array(r.boxes.xywh.cpu()).astype(int)
|
64 |
|
65 |
for con, cl, xy in zip(conf, cls, xywh):
|
|
|
|
|
|
|
|
|
|
|
66 |
confidence = round(float(con) * 100, 1)
|
67 |
-
text += f"Detected {
|
68 |
-
detected_diseases.add(
|
|
|
|
|
69 |
|
70 |
for disease in detected_diseases:
|
71 |
-
if disease
|
72 |
-
solution += f"\n--- {disease} ---\n"
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
else:
|
76 |
-
solution += "\
|
77 |
|
78 |
return im, text.strip(), solution.strip()
|
79 |
|
@@ -86,11 +95,11 @@ def process_video(video_path):
|
|
86 |
ret, frame = cap.read()
|
87 |
if not ret:
|
88 |
break
|
89 |
-
pil_img = Image.fromarray(frame[..., ::-1])
|
90 |
result = model.predict(source=pil_img)
|
91 |
for r in result:
|
92 |
im_array = r.plot()
|
93 |
-
processed_frame = Image.fromarray(im_array[..., ::-1])
|
94 |
yield processed_frame
|
95 |
cap.release()
|
96 |
|
@@ -103,7 +112,7 @@ inputs = [
|
|
103 |
|
104 |
outputs = [
|
105 |
gr.Image(type="pil", label="Output Image"),
|
106 |
-
gr.Textbox(label="Result"),
|
107 |
gr.Textbox(label="AI-Powered Solution")
|
108 |
]
|
109 |
|
@@ -114,22 +123,22 @@ examples = [
|
|
114 |
]
|
115 |
|
116 |
title = """Corn Diseases Detection Finetuned YOLOv11 <br></br> <a href="https://colab.research.google.com/drive/1vnxtgPKOgfC8nyCL9hjrNFed75StsqGQ?usp=sharing"> <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab" style="display:inline-block;"> </a> """
|
117 |
-
description = 'Image Size:
|
118 |
|
119 |
video_iface = gr.Interface(
|
120 |
fn=process_video,
|
121 |
inputs=gr.Video(label="Upload Video", interactive=True),
|
122 |
outputs=gr.Image(type="pil", label="Result"),
|
123 |
title=title,
|
124 |
-
description="Upload video
|
125 |
)
|
126 |
|
127 |
image_iface = gr.Interface(
|
128 |
-
fn=response2,
|
129 |
-
inputs=inputs,
|
130 |
-
outputs=outputs,
|
131 |
-
examples=examples,
|
132 |
-
title=title,
|
133 |
description=description
|
134 |
)
|
135 |
|
@@ -137,4 +146,3 @@ demo = gr.TabbedInterface([image_iface, video_iface], ["Image Inference", "Video
|
|
137 |
|
138 |
if __name__ == '__main__':
|
139 |
demo.launch()
|
140 |
-
|
|
|
48 |
|
49 |
def response2(image, image_size=640, conf_threshold=0.3, iou_threshold=0.6):
|
50 |
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
|
51 |
+
|
52 |
text = ""
|
53 |
solution = ""
|
|
|
54 |
detected_diseases = set()
|
55 |
|
56 |
for r in results:
|
57 |
im_array = r.plot()
|
58 |
im = Image.fromarray(im_array[..., ::-1])
|
59 |
+
|
60 |
for r in results:
|
61 |
conf = np.array(r.boxes.conf.cpu())
|
62 |
cls = np.array(r.boxes.cls.cpu()).astype(int)
|
63 |
xywh = np.array(r.boxes.xywh.cpu()).astype(int)
|
64 |
|
65 |
for con, cl, xy in zip(conf, cls, xywh):
|
66 |
+
if cl < len(name):
|
67 |
+
disease_name = name[cl]
|
68 |
+
else:
|
69 |
+
disease_name = "Unknown"
|
70 |
+
|
71 |
confidence = round(float(con) * 100, 1)
|
72 |
+
text += f"Detected {disease_name} with confidence {confidence}% at ({xy[0]},{xy[1]})\n"
|
73 |
+
detected_diseases.add(disease_name)
|
74 |
+
|
75 |
+
explanation_cache = {}
|
76 |
|
77 |
for disease in detected_diseases:
|
78 |
+
if disease.lower() == "healthy":
|
79 |
+
solution += f"\n--- {disease} ---\nTanaman tampak sehat. Tidak ada tindakan diperlukan.\n"
|
80 |
+
elif disease in name:
|
81 |
+
if disease not in explanation_cache:
|
82 |
+
explanation_cache[disease] = get_deepseek_solution(disease)
|
83 |
+
solution += f"\n--- {disease} ---\n{explanation_cache[disease]}\n"
|
84 |
else:
|
85 |
+
solution += f"\n--- {disease} ---\nJenis penyakit tidak dikenali. Tidak dapat memberikan solusi.\n"
|
86 |
|
87 |
return im, text.strip(), solution.strip()
|
88 |
|
|
|
95 |
ret, frame = cap.read()
|
96 |
if not ret:
|
97 |
break
|
98 |
+
pil_img = Image.fromarray(frame[..., ::-1])
|
99 |
result = model.predict(source=pil_img)
|
100 |
for r in result:
|
101 |
im_array = r.plot()
|
102 |
+
processed_frame = Image.fromarray(im_array[..., ::-1])
|
103 |
yield processed_frame
|
104 |
cap.release()
|
105 |
|
|
|
112 |
|
113 |
outputs = [
|
114 |
gr.Image(type="pil", label="Output Image"),
|
115 |
+
gr.Textbox(label="Result"),
|
116 |
gr.Textbox(label="AI-Powered Solution")
|
117 |
]
|
118 |
|
|
|
123 |
]
|
124 |
|
125 |
title = """Corn Diseases Detection Finetuned YOLOv11 <br></br> <a href="https://colab.research.google.com/drive/1vnxtgPKOgfC8nyCL9hjrNFed75StsqGQ?usp=sharing"> <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab" style="display:inline-block;"> </a> """
|
126 |
+
description = 'Image Size: Ukuran gambar untuk inferensi.\nConfidence Threshold: Minimum confidence untuk deteksi.\nIOU Threshold: Threshold untuk Non-Maximum Suppression (NMS).'
|
127 |
|
128 |
video_iface = gr.Interface(
|
129 |
fn=process_video,
|
130 |
inputs=gr.Video(label="Upload Video", interactive=True),
|
131 |
outputs=gr.Image(type="pil", label="Result"),
|
132 |
title=title,
|
133 |
+
description="Upload video untuk deteksi penyakit jagung."
|
134 |
)
|
135 |
|
136 |
image_iface = gr.Interface(
|
137 |
+
fn=response2,
|
138 |
+
inputs=inputs,
|
139 |
+
outputs=outputs,
|
140 |
+
examples=examples,
|
141 |
+
title=title,
|
142 |
description=description
|
143 |
)
|
144 |
|
|
|
146 |
|
147 |
if __name__ == '__main__':
|
148 |
demo.launch()
|
|