Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import PIL.Image as Image
|
2 |
import gradio as gr
|
|
|
3 |
from ultralytics import ASSETS, YOLO
|
4 |
|
5 |
model = YOLO("./best.pt")
|
6 |
|
|
|
7 |
def predict_image(img, conf_threshold, iou_threshold):
|
8 |
results = model.predict(
|
9 |
source=img,
|
@@ -14,41 +16,28 @@ def predict_image(img, conf_threshold, iou_threshold):
|
|
14 |
imgsz=640,
|
15 |
)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
if results:
|
21 |
-
# Take the result with the highest confidence
|
22 |
-
result = max(results, key=lambda x: x["conf"])
|
23 |
-
|
24 |
-
# Extract box, label, and highest probability
|
25 |
-
box = result["xyxy"][0]
|
26 |
-
label = result["label"][0]
|
27 |
-
confidence = result["conf"]
|
28 |
-
|
29 |
-
# Return the box, label, and highest probability
|
30 |
-
return {
|
31 |
-
"box": box,
|
32 |
-
"label": label,
|
33 |
-
"confidence": confidence
|
34 |
-
}
|
35 |
-
else:
|
36 |
-
# No bounding box found
|
37 |
-
return {"box": [], "label": "No detection", "confidence": 0.0}
|
38 |
|
|
|
39 |
|
40 |
|
41 |
iface = gr.Interface(
|
42 |
fn=predict_image,
|
43 |
inputs=[
|
44 |
-
gr.Image(type="pil", label="
|
45 |
-
gr.Slider(minimum=0, maximum=1, value=0.25, label="
|
46 |
-
gr.Slider(minimum=0, maximum=1, value=0.45, label="
|
47 |
],
|
48 |
-
outputs=gr.Image(type="pil", label="
|
49 |
-
title="
|
50 |
-
description="
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
if __name__ == '__main__':
|
54 |
-
iface.launch()
|
|
|
1 |
import PIL.Image as Image
|
2 |
import gradio as gr
|
3 |
+
|
4 |
from ultralytics import ASSETS, YOLO
|
5 |
|
6 |
model = YOLO("./best.pt")
|
7 |
|
8 |
+
|
9 |
def predict_image(img, conf_threshold, iou_threshold):
|
10 |
results = model.predict(
|
11 |
source=img,
|
|
|
16 |
imgsz=640,
|
17 |
)
|
18 |
|
19 |
+
for r in results:
|
20 |
+
im_array = r.plot()
|
21 |
+
im = Image.fromarray(im_array[..., ::-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
return im
|
24 |
|
25 |
|
26 |
iface = gr.Interface(
|
27 |
fn=predict_image,
|
28 |
inputs=[
|
29 |
+
gr.Image(type="pil", label="Upload Image"),
|
30 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
31 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
|
32 |
],
|
33 |
+
outputs=gr.Image(type="pil", label="Result"),
|
34 |
+
title="Ultralytics Gradio",
|
35 |
+
description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
|
36 |
+
examples=[
|
37 |
+
[ASSETS / "bus.jpg", 0.25, 0.45],
|
38 |
+
[ASSETS / "zidane.jpg", 0.25, 0.45],
|
39 |
+
]
|
40 |
)
|
41 |
|
42 |
if __name__ == '__main__':
|
43 |
+
iface.launch()
|