Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,10 @@ import cv2
|
|
3 |
import numpy as np
|
4 |
from ultralytics import YOLO
|
5 |
|
6 |
-
#
|
7 |
-
model = YOLO('yolo11s-earth.pt') #
|
8 |
|
9 |
-
#
|
10 |
default_classes = [
|
11 |
'airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge',
|
12 |
'chimney', 'dam', 'Expressway-Service-area', 'Expressway-toll-station',
|
@@ -16,29 +16,24 @@ default_classes = [
|
|
16 |
]
|
17 |
|
18 |
def process_frame(frame, classes_input):
|
19 |
-
#
|
20 |
-
if classes_input
|
21 |
classes_list = [cls.strip() for cls in classes_input.split(',')]
|
22 |
-
model.set_classes(classes_list) #
|
23 |
else:
|
24 |
-
#
|
25 |
model.set_classes(default_classes)
|
26 |
|
27 |
-
#
|
28 |
frame = frame.copy()
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
new_size = (640, int(h * (640 / w))) if w > h else (int(w * (640 / h)), 640)
|
33 |
-
resized_frame = cv2.resize(frame, new_size)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
|
38 |
-
#
|
39 |
-
results = model.predict(rgb_frame)
|
40 |
-
|
41 |
-
# Draw detection results
|
42 |
for result in results:
|
43 |
boxes = result.boxes
|
44 |
for box in boxes:
|
@@ -47,26 +42,20 @@ def process_frame(frame, classes_input):
|
|
47 |
cls = box.cls[0]
|
48 |
class_name = model.names[int(cls)]
|
49 |
|
50 |
-
#
|
51 |
-
x1
|
52 |
-
|
53 |
-
x2 = int(x2 * w / new_size[0])
|
54 |
-
y2 = int(y2 * h / new_size[1])
|
55 |
-
|
56 |
-
# Draw bounding box and label
|
57 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
58 |
-
cv2.putText(frame, f'{class_name}:{conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
59 |
|
60 |
return frame
|
61 |
|
62 |
def main():
|
63 |
-
#
|
64 |
with gr.Blocks() as demo:
|
65 |
-
gr.Markdown("#
|
66 |
with gr.Row():
|
67 |
-
cam_input = gr.Image(type="numpy", sources=["webcam"], streaming=True, label="
|
68 |
-
classes_input = gr.Textbox(label="
|
69 |
-
output = gr.Image(label="
|
70 |
|
71 |
cam_input.stream(
|
72 |
process_frame,
|
@@ -74,7 +63,7 @@ def main():
|
|
74 |
outputs=output
|
75 |
)
|
76 |
|
77 |
-
#
|
78 |
demo.launch()
|
79 |
|
80 |
if __name__ == "__main__":
|
|
|
3 |
import numpy as np
|
4 |
from ultralytics import YOLO
|
5 |
|
6 |
+
# 加载YOLO模型
|
7 |
+
model = YOLO('yolo11s-earth.pt') # 加载你的模型
|
8 |
|
9 |
+
# 默认类别
|
10 |
default_classes = [
|
11 |
'airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge',
|
12 |
'chimney', 'dam', 'Expressway-Service-area', 'Expressway-toll-station',
|
|
|
16 |
]
|
17 |
|
18 |
def process_frame(frame, classes_input):
|
19 |
+
# 将输入的类别字符串转为列表
|
20 |
+
if classes_input:
|
21 |
classes_list = [cls.strip() for cls in classes_input.split(',')]
|
22 |
+
model.set_classes(classes_list) # 设置模型的类别
|
23 |
else:
|
24 |
+
# 如果没有输入,则使用默认类别
|
25 |
model.set_classes(default_classes)
|
26 |
|
27 |
+
# 复制帧为可写数组
|
28 |
frame = frame.copy()
|
29 |
|
30 |
+
# 转换图像格式
|
31 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
32 |
|
33 |
+
# 使用模型进行检测
|
34 |
+
results = model.predict(rgb_frame, imgsz=800)
|
35 |
|
36 |
+
# 绘制检测结果
|
|
|
|
|
|
|
37 |
for result in results:
|
38 |
boxes = result.boxes
|
39 |
for box in boxes:
|
|
|
42 |
cls = box.cls[0]
|
43 |
class_name = model.names[int(cls)]
|
44 |
|
45 |
+
# 绘制边界框和标签
|
46 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
|
47 |
+
cv2.putText(frame, f'{class_name}:{conf:.2f}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
return frame
|
50 |
|
51 |
def main():
|
52 |
+
# 创建Gradio界面
|
53 |
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# YOLOv11s-Earth 实时检测")
|
55 |
with gr.Row():
|
56 |
+
cam_input = gr.Image(type="numpy", sources=["webcam"], streaming=True, label="摄像头输入")
|
57 |
+
classes_input = gr.Textbox(label="输入类别(逗号分隔)", placeholder="例如:0,1,2")
|
58 |
+
output = gr.Image(label="检测结果", type="numpy")
|
59 |
|
60 |
cam_input.stream(
|
61 |
process_frame,
|
|
|
63 |
outputs=output
|
64 |
)
|
65 |
|
66 |
+
# 启动Gradio应用
|
67 |
demo.launch()
|
68 |
|
69 |
if __name__ == "__main__":
|