Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,13 @@ def detect_objects(image):
|
|
| 45 |
for i in range(predictions.shape[1]): # 遍历每个预测
|
| 46 |
score = predictions[0, i, -1].item() # 假设最后一个维度是分数
|
| 47 |
if score > confidence_threshold: # 使用新的阈值过滤低置信度的预测
|
|
|
|
| 48 |
box = predictions[0, i, :-1].tolist() # 获取边界框坐标(假设在前面)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
detections.append({
|
| 50 |
"label": f"Object {i}", # 根据实际情况替换标签
|
| 51 |
"confidence": round(score, 3),
|
|
@@ -53,7 +59,8 @@ def detect_objects(image):
|
|
| 53 |
})
|
| 54 |
# 绘制边界框和标签到原始图像上
|
| 55 |
draw = ImageDraw.Draw(original_image)
|
| 56 |
-
draw.rectangle(box, outline="red", width=3) #
|
|
|
|
| 57 |
draw.text((box[0], box[1]), f"{detections[-1]['label']} ({detections[-1]['confidence']})", fill="red") # 添加标签
|
| 58 |
|
| 59 |
return original_image, detections
|
|
|
|
| 45 |
for i in range(predictions.shape[1]): # 遍历每个预测
|
| 46 |
score = predictions[0, i, -1].item() # 假设最后一个维度是分数
|
| 47 |
if score > confidence_threshold: # 使用新的阈值过滤低置信度的预测
|
| 48 |
+
|
| 49 |
box = predictions[0, i, :-1].tolist() # 获取边界框坐标(假设在前面)
|
| 50 |
+
box[0] *= original_image.width / 800 # 将坐标缩放回原始图像尺寸
|
| 51 |
+
box[1] *= original_image.height / 800
|
| 52 |
+
box[2] *= original_image.width / 800
|
| 53 |
+
box[3] *= original_image.height / 800
|
| 54 |
+
|
| 55 |
detections.append({
|
| 56 |
"label": f"Object {i}", # 根据实际情况替换标签
|
| 57 |
"confidence": round(score, 3),
|
|
|
|
| 59 |
})
|
| 60 |
# 绘制边界框和标签到原始图像上
|
| 61 |
draw = ImageDraw.Draw(original_image)
|
| 62 |
+
draw.rectangle(box[:4], outline="red", width=3) # 绘制矩形框,确保只取前四个坐标
|
| 63 |
+
|
| 64 |
draw.text((box[0], box[1]), f"{detections[-1]['label']} ({detections[-1]['confidence']})", fill="red") # 添加标签
|
| 65 |
|
| 66 |
return original_image, detections
|