Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import os
|
5 |
-
from ultralytics import YOLO
|
6 |
|
7 |
-
|
8 |
-
UPLOAD_FOLDER = 'uploads'
|
9 |
-
RESULT_FOLDER = 'results'
|
10 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
-
os.makedirs(RESULT_FOLDER, exist_ok=True)
|
12 |
|
13 |
-
# 加载模型
|
14 |
-
model = YOLO('yolov8n.pt')
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
class_counts = {}
|
26 |
|
27 |
-
|
28 |
-
boxes = result.boxes
|
29 |
-
for box in boxes:
|
30 |
-
x1, y1, x2, y2 = box.xyxy[0]
|
31 |
-
conf = box.conf[0]
|
32 |
-
cls = box.cls[0]
|
33 |
-
class_name = model.names[int(cls)]
|
34 |
|
35 |
-
# 计算每种类别的数量
|
36 |
-
if class_name in class_counts:
|
37 |
-
class_counts[class_name] += 1
|
38 |
-
else:
|
39 |
-
class_counts[class_name] = 1
|
40 |
|
41 |
-
# 绘制边界框和标签
|
42 |
-
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
|
43 |
-
cv2.putText(image, f'{class_name}:{conf:.2f}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9,
|
44 |
-
(36, 255, 12), 2)
|
45 |
-
detection_results.append(f'Class: {class_name}, Confidence: {conf:.2f}, Box: ({x1}, {y1}), ({x2}, {y2})')
|
46 |
-
|
47 |
-
# 在图像上显示检测到的物体数量信息
|
48 |
-
y_offset = 30
|
49 |
-
for class_name, count in class_counts.items():
|
50 |
-
cv2.putText(image, f'{class_name}: {count}', (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
51 |
-
y_offset += 30
|
52 |
-
|
53 |
-
# 保存处理后的图像
|
54 |
-
result_filename = 'result_image.jpg'
|
55 |
-
result_path = os.path.join(RESULT_FOLDER, result_filename)
|
56 |
-
cv2.imwrite(result_path, image)
|
57 |
-
|
58 |
-
return image, '\n'.join(detection_results)
|
59 |
-
|
60 |
-
# 创建Gradio界面
|
61 |
iface = gr.Interface(
|
62 |
-
fn=
|
63 |
-
inputs=
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
|
69 |
-
|
70 |
-
iface.launch(
|
71 |
-
share=True,
|
72 |
-
server_name="0.0.0.0",
|
73 |
-
server_port=7860,
|
74 |
-
debug=True,
|
75 |
-
#auth=("username", "password"),
|
76 |
-
auth=("test", "12345"),
|
77 |
-
auth_message="Please enter your credentials to access the app.",
|
78 |
-
inbrowser=True
|
79 |
-
)
|
|
|
1 |
import gradio as gr
|
2 |
+
import PIL.Image as Image
|
3 |
+
from ultralytics import ASSETS, YOLO
|
|
|
|
|
4 |
|
5 |
+
model = YOLO("yolov8n.pt")
|
|
|
|
|
|
|
|
|
6 |
|
|
|
|
|
7 |
|
8 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
9 |
+
"""Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
|
10 |
+
results = model.predict(
|
11 |
+
source=img,
|
12 |
+
conf=conf_threshold,
|
13 |
+
iou=iou_threshold,
|
14 |
+
show_labels=True,
|
15 |
+
show_conf=True,
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|