Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
from roboflow import Roboflow
|
6 |
+
|
7 |
+
rf = Roboflow(api_key=roboflow_key)
|
8 |
+
project = rf.workspace('yudi-pratama-putra-rwuep').project("corn-pest-detection-2")
|
9 |
+
model = project.version(3).model
|
10 |
+
|
11 |
+
|
12 |
+
def predict_image(image, confidence, overlap):
|
13 |
+
colors = [
|
14 |
+
"Red",
|
15 |
+
"Green",
|
16 |
+
"Blue",
|
17 |
+
"Yellow",
|
18 |
+
"Cyan",
|
19 |
+
"Magenta",
|
20 |
+
"Orange",
|
21 |
+
"Purple",
|
22 |
+
"Brown",
|
23 |
+
"Pink",
|
24 |
+
"DarkRed",
|
25 |
+
"Black",
|
26 |
+
"White"
|
27 |
+
]
|
28 |
+
|
29 |
+
pest_class = ['aphids', 'army worm', 'black cutworm', 'corn borer', 'grub', 'large cutworm', 'mole cricket', 'peach borer', 'potosiabre vitarsis', 'red spider', 'white margined moth', 'wireworm', 'yellow cutworm']
|
30 |
+
|
31 |
+
prediction = model.predict(image, confidence=confidence, overlap=overlap).json()
|
32 |
+
|
33 |
+
img = Image.open(image).convert("RGB")
|
34 |
+
|
35 |
+
img = img.resize((img.width * 2, img.height * 2))
|
36 |
+
|
37 |
+
draw = ImageDraw.Draw(img)
|
38 |
+
font = ImageFont.load_default(size=30)
|
39 |
+
|
40 |
+
for result in prediction['predictions']:
|
41 |
+
x0 = result['x'] - result['width'] / 2
|
42 |
+
y0 = result['y'] - result['height'] / 2
|
43 |
+
x1 = result['x'] + result['width'] / 2
|
44 |
+
y1 = result['y'] + result['height'] / 2
|
45 |
+
|
46 |
+
x0 *= 2
|
47 |
+
y0 *= 2
|
48 |
+
x1 *= 2
|
49 |
+
y1 *= 2
|
50 |
+
|
51 |
+
for i in range(len(pest_class)):
|
52 |
+
if result['class'] == pest_class[i]:
|
53 |
+
draw.rectangle([x0, y0, x1, y1], outline=colors[i], width=3)
|
54 |
+
|
55 |
+
label = f"{result['class']} ({result['confidence']*100:.2f}%)"
|
56 |
+
|
57 |
+
draw.text((x0, y0 - 35), label, fill=colors[i], font=font)
|
58 |
+
|
59 |
+
if prediction['predictions'] == []:
|
60 |
+
img = Image.open(image)
|
61 |
+
|
62 |
+
return img
|
63 |
+
|
64 |
+
inputs_image = [
|
65 |
+
gr.Image(type='filepath', label='input image'),
|
66 |
+
gr.Slider(minimum=0, maximum=100, value=40, label='Confidence (%)'),
|
67 |
+
gr.Slider(minimum=0, maximum=100, value=30, label='Overlap (%)')
|
68 |
+
]
|
69 |
+
outputs_image = [
|
70 |
+
gr.Image(type='numpy', label='output image')
|
71 |
+
]
|
72 |
+
|
73 |
+
interface_image = gr.Interface(
|
74 |
+
fn=predict_image,
|
75 |
+
inputs=inputs_image,
|
76 |
+
outputs=outputs_image,
|
77 |
+
title="Corn Pest Detection",
|
78 |
+
description=(
|
79 |
+
"Upload an image and the model will detect pests.\n\n"
|
80 |
+
"Model detected: aphids, army worm, black cutworm, corn borer, grub, large cutworm, mole cricket, peach borer, "
|
81 |
+
"potosiabre vitarsis, red spider, white margined moth, wireworm, yellow cutworm.\n\n"
|
82 |
+
"Confidence: The higher the confidence value, the more certain the model is about the detected object being correct. "
|
83 |
+
"For example, a higher confidence threshold will filter out less certain predictions.\n\n"
|
84 |
+
"Overlap: The higher the accepted overlap value, the more predictions are allowed, even if they overlap with each other. "
|
85 |
+
"A higher overlap value can help detect multiple objects that are close together."
|
86 |
+
)
|
87 |
+
)
|
88 |
+
|
89 |
+
interface_image.launch(share=True)
|