Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -44,22 +44,31 @@ def predict(img):
|
|
44 |
info = {
|
45 |
"detected": len(result.boxes) > 0,
|
46 |
"count": len(result.boxes),
|
47 |
-
"
|
48 |
}
|
49 |
|
50 |
if info["detected"]:
|
51 |
-
#
|
52 |
for box in result.boxes:
|
53 |
conf = float(box.conf[0])
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# 生成输出文本
|
57 |
output_text = f"""检测结果:
|
58 |
- 是否检测到目标: {'是' if info['detected'] else '否'}
|
59 |
- 检测到的目标数量: {info['count']}"""
|
60 |
|
61 |
-
if info["
|
62 |
-
output_text +=
|
|
|
|
|
63 |
|
64 |
return result_img, output_text
|
65 |
|
|
|
44 |
info = {
|
45 |
"detected": len(result.boxes) > 0,
|
46 |
"count": len(result.boxes),
|
47 |
+
"detections": [] # 存储每个检测目标的详细信息
|
48 |
}
|
49 |
|
50 |
if info["detected"]:
|
51 |
+
# 获取每个检测框的信息
|
52 |
for box in result.boxes:
|
53 |
conf = float(box.conf[0])
|
54 |
+
cls = int(box.cls[0])
|
55 |
+
cls_name = result.names[cls] # 获取类别名称
|
56 |
+
|
57 |
+
detection_info = {
|
58 |
+
"class": cls_name,
|
59 |
+
"confidence": f"{conf:.2%}"
|
60 |
+
}
|
61 |
+
info["detections"].append(detection_info)
|
62 |
|
63 |
# 生成输出文本
|
64 |
output_text = f"""检测结果:
|
65 |
- 是否检测到目标: {'是' if info['detected'] else '否'}
|
66 |
- 检测到的目标数量: {info['count']}"""
|
67 |
|
68 |
+
if info["detections"]:
|
69 |
+
output_text += "\n- 详细信息:"
|
70 |
+
for idx, det in enumerate(info["detections"], 1):
|
71 |
+
output_text += f"\n 目标 {idx}: {det['class']} (置信度: {det['confidence']})"
|
72 |
|
73 |
return result_img, output_text
|
74 |
|