Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,9 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from ultralytics import YOLO
|
3 |
from PIL import Image
|
4 |
-
|
5 |
-
|
6 |
|
7 |
model_path = 'new_data_improved_object_detector.pt'
|
8 |
-
|
9 |
model = YOLO(model_path)
|
10 |
|
11 |
-
|
12 |
def predict_image(img, conf_threshold, iou_threshold):
|
13 |
"""Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
|
14 |
# Convert the input image to grayscale
|
@@ -16,44 +11,61 @@ def predict_image(img, conf_threshold, iou_threshold):
|
|
16 |
|
17 |
results = model.predict(
|
18 |
source=img,
|
19 |
-
conf
|
20 |
-
iou=
|
21 |
show_labels=True,
|
22 |
show_conf=True,
|
23 |
imgsz=640,
|
24 |
)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
for r in results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
im_array = r.plot()
|
28 |
im = Image.fromarray(im_array[..., ::-1])
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
|
|
34 |
|
35 |
iface = gr.Interface(
|
36 |
-
|
37 |
fn=predict_image,
|
38 |
inputs=[
|
39 |
gr.Image(type="pil", label="Upload Image"),
|
40 |
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
41 |
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
42 |
],
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
|
50 |
-
examples = [
|
51 |
-
['1.jpg'],
|
52 |
-
['2.jpg'],
|
53 |
-
['3.jpg']
|
54 |
-
]
|
55 |
)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
iface.launch(debug=True, share=False).queue()
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
+
import gradio as gr
|
|
|
3 |
|
4 |
model_path = 'new_data_improved_object_detector.pt'
|
|
|
5 |
model = YOLO(model_path)
|
6 |
|
|
|
7 |
def predict_image(img, conf_threshold, iou_threshold):
|
8 |
"""Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
|
9 |
# Convert the input image to grayscale
|
|
|
11 |
|
12 |
results = model.predict(
|
13 |
source=img,
|
14 |
+
conf=conf_threshold,
|
15 |
+
iou=iou_threshold,
|
16 |
show_labels=True,
|
17 |
show_conf=True,
|
18 |
imgsz=640,
|
19 |
)
|
20 |
|
21 |
+
im_arrays = []
|
22 |
+
all_model_result=[]
|
23 |
+
all_xywh = []
|
24 |
+
all_clss = []
|
25 |
+
all_names = []
|
26 |
+
all_confidence = []
|
27 |
+
all_xyxy =[]
|
28 |
+
|
29 |
for r in results:
|
30 |
+
model_result =results[0]
|
31 |
+
xywh = r.boxes.xywh.cpu().tolist()
|
32 |
+
clss = r.boxes.cls.cpu().tolist()
|
33 |
+
names = [r.names[cls] for cls in clss] # Convert class indices to names
|
34 |
+
confidence = r.boxes.conf.cpu().tolist()
|
35 |
+
xyxy = r.boxes.cpu().xyxy.tolist()
|
36 |
+
|
37 |
im_array = r.plot()
|
38 |
im = Image.fromarray(im_array[..., ::-1])
|
39 |
+
im_arrays.append(im)
|
40 |
|
41 |
+
all_model_result.extend(model_result)
|
42 |
+
all_xywh.extend(xywh)
|
43 |
+
all_clss.extend(clss)
|
44 |
+
all_names.extend(names)
|
45 |
+
all_confidence.extend(confidence)
|
46 |
+
all_xyxy.extend(xyxy)
|
47 |
|
48 |
+
return im_arrays, model_result,all_xywh, all_clss, all_names, all_confidence,xyxy
|
49 |
|
50 |
iface = gr.Interface(
|
|
|
51 |
fn=predict_image,
|
52 |
inputs=[
|
53 |
gr.Image(type="pil", label="Upload Image"),
|
54 |
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
55 |
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
56 |
],
|
57 |
+
outputs=[
|
58 |
+
gr.Gallery(label="Result Images"),
|
59 |
+
gr.JSON(label="Detection Bounding Boxes (model_result)"),
|
60 |
+
gr.JSON(label="Detection Bounding Boxes (xywh)"),
|
61 |
+
gr.JSON(label="Detection Class Indices"),
|
62 |
+
gr.JSON(label="Detection Class Names"),
|
63 |
+
gr.JSON(label="Detection Confidence Scores"),
|
64 |
+
gr.JSON(label="Detection Bounding Boxes (xyxy)")
|
65 |
+
],
|
66 |
+
title="Ultralytics Gradio",
|
67 |
description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
|
|
|
|
|
|
|
|
|
|
|
68 |
)
|
69 |
|
70 |
+
if __name__ == "__main__":
|
71 |
+
iface.launch()
|
|