Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -66,12 +66,11 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
|
|
66 |
(width, height)
|
67 |
)
|
68 |
|
69 |
-
#
|
70 |
-
# 基于视频分辨率计算合适的点大小
|
71 |
base_size = min(width, height)
|
72 |
-
point_size = max(1, int(base_size * 0.005)) # 0.5%
|
73 |
-
line_thickness = max(1, int(base_size * 0.002)) # 0.2%
|
74 |
-
|
75 |
# 设置推理参数并处理视频
|
76 |
results = model.predict(
|
77 |
source=video_path,
|
@@ -80,12 +79,14 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
|
|
80 |
save=False,
|
81 |
show=False,
|
82 |
stream=True,
|
83 |
-
line_width=line_thickness, #
|
84 |
show_boxes=True,
|
85 |
show_labels=True,
|
86 |
show_conf=True,
|
87 |
vid_stride=1,
|
88 |
max_det=max_det,
|
|
|
|
|
89 |
)
|
90 |
|
91 |
# 处理结果
|
@@ -96,19 +97,6 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
|
|
96 |
# 获取绘制了预测结果的帧
|
97 |
frame = r.plot()
|
98 |
|
99 |
-
# 如果需要自定义绘制姿态点
|
100 |
-
if hasattr(r, 'keypoints') and r.keypoints is not None:
|
101 |
-
for kpts in r.keypoints:
|
102 |
-
for x, y, conf in kpts:
|
103 |
-
if conf > conf_threshold:
|
104 |
-
cv2.circle(
|
105 |
-
frame,
|
106 |
-
(int(x), int(y)),
|
107 |
-
point_size, # 使用计算出的点大小
|
108 |
-
(0, 255, 0),
|
109 |
-
-1 # -1 表示填充圆
|
110 |
-
)
|
111 |
-
|
112 |
# 收集检测信息
|
113 |
frame_info = {
|
114 |
"frame": frame_count + 1,
|
@@ -143,13 +131,11 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
|
|
143 |
- 置信度阈值: {conf_threshold:.2f}
|
144 |
- 最大检测数量: {max_det}
|
145 |
- 处理时长: {process_seconds}秒
|
146 |
-
|
147 |
分析结果:
|
148 |
- 处理帧数: {frame_count}
|
149 |
- 平均每帧检测到的老鼠数: {np.mean([info['count'] for info in detection_info]):.1f}
|
150 |
- 最大检测数: {max([info['count'] for info in detection_info])}
|
151 |
- 最小检测数: {min([info['count'] for info in detection_info])}
|
152 |
-
|
153 |
置信度分布:
|
154 |
{np.histogram([float(det['confidence'].strip('%'))/100 for info in detection_info for det in info['detections']], bins=5)[0].tolist()}
|
155 |
"""
|
@@ -189,7 +175,7 @@ with gr.Blocks() as demo:
|
|
189 |
max_det = gr.Slider(
|
190 |
minimum=1,
|
191 |
maximum=10,
|
192 |
-
value=
|
193 |
step=1,
|
194 |
label="最大检测数量",
|
195 |
info="每帧最多检测的目标数量"
|
|
|
66 |
(width, height)
|
67 |
)
|
68 |
|
69 |
+
# 计算基于分辨率的点大小和线宽
|
|
|
70 |
base_size = min(width, height)
|
71 |
+
point_size = max(1, int(base_size * 0.005)) # 0.5% 的最小边长
|
72 |
+
line_thickness = max(1, int(base_size * 0.002)) # 0.2% 的最小边长
|
73 |
+
|
74 |
# 设置推理参数并处理视频
|
75 |
results = model.predict(
|
76 |
source=video_path,
|
|
|
79 |
save=False,
|
80 |
show=False,
|
81 |
stream=True,
|
82 |
+
line_width=line_thickness, # 线宽
|
83 |
show_boxes=True,
|
84 |
show_labels=True,
|
85 |
show_conf=True,
|
86 |
vid_stride=1,
|
87 |
max_det=max_det,
|
88 |
+
kpt_radius=point_size, # 关键点半径
|
89 |
+
kpt_line_thickness=line_thickness, # 关键点连接线宽度
|
90 |
)
|
91 |
|
92 |
# 处理结果
|
|
|
97 |
# 获取绘制了预测结果的帧
|
98 |
frame = r.plot()
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# 收集检测信息
|
101 |
frame_info = {
|
102 |
"frame": frame_count + 1,
|
|
|
131 |
- 置信度阈值: {conf_threshold:.2f}
|
132 |
- 最大检测数量: {max_det}
|
133 |
- 处理时长: {process_seconds}秒
|
|
|
134 |
分析结果:
|
135 |
- 处理帧数: {frame_count}
|
136 |
- 平均每帧检测到的老鼠数: {np.mean([info['count'] for info in detection_info]):.1f}
|
137 |
- 最大检测数: {max([info['count'] for info in detection_info])}
|
138 |
- 最小检测数: {min([info['count'] for info in detection_info])}
|
|
|
139 |
置信度分布:
|
140 |
{np.histogram([float(det['confidence'].strip('%'))/100 for info in detection_info for det in info['detections']], bins=5)[0].tolist()}
|
141 |
"""
|
|
|
175 |
max_det = gr.Slider(
|
176 |
minimum=1,
|
177 |
maximum=10,
|
178 |
+
value=8,
|
179 |
step=1,
|
180 |
label="最大检测数量",
|
181 |
info="每帧最多检测的目标数量"
|