测试文件提交

This commit is contained in:
ray
2025-11-04 14:47:28 +08:00
parent e6cd3658d0
commit d0e74f49d0
2 changed files with 281 additions and 1 deletions

View File

@@ -161,6 +161,9 @@ def yolo_shibie(im_PIL, detections, model, enhance_enabled=False, enhance_params
# ✅ 提取检测信息
if result.boxes is not None and len(result.boxes.xyxy) > 0:
# 用于存储多个候选npc4如果检测到多个
npc4_candidates = []
for i in range(len(result.boxes.xyxy)):
try:
left = float(result.boxes.xyxy[i][0])
@@ -169,11 +172,37 @@ def yolo_shibie(im_PIL, detections, model, enhance_enabled=False, enhance_params
bottom = float(result.boxes.xyxy[i][3])
cls_id = int(result.boxes.cls[i])
label = result.names[cls_id]
# 获取置信度(如果可用)
confidence = float(result.boxes.conf[i]) if hasattr(result.boxes, 'conf') and len(result.boxes.conf) > i else 1.0
if label in ['center', 'next', 'npc1', 'npc2', 'npc3', 'npc4', 'boss', 'zhaozi']:
# npc1-npc4 使用底部位置与main.py保持一致
if label in ['npc1', 'npc2', 'npc3', 'npc4']:
player_x = int(left + (right - left) / 2)
player_y = int(bottom) + 30 # 使用底部位置与main.py保持一致
position = [player_x, player_y]
# 特殊处理npc4如果检测到多个收集所有候选
if label == 'npc4':
npc4_candidates.append({
'position': position,
'confidence': confidence,
'box': [left, top, right, bottom],
'area': (right - left) * (bottom - top) # 检测框面积
})
else:
# npc1-npc3直接赋值如果已经有值保留置信度更高的
if detections[label] is None or (hasattr(result.boxes, 'conf') and
confidence > 0.5):
detections[label] = position
# 其他目标使用中心点
elif label in ['center', 'next', 'boss', 'zhaozi']:
player_x = int(left + (right - left) / 2) + 3
player_y = int(top + (bottom - top) / 2) + 40
detections[label] = [player_x, player_y]
# 道具和怪物可以多个
elif label in ['daojv', 'gw']:
player_x = int(left + (right - left) / 2) + 3
player_y = int(top + (bottom - top) / 2) + 40
@@ -181,9 +210,34 @@ def yolo_shibie(im_PIL, detections, model, enhance_enabled=False, enhance_params
if label not in detections:
detections[label] = []
detections[label].append([player_x, player_y])
except Exception as e:
print(f"⚠️ 处理检测框时出错: {e}")
continue
# 处理npc4如果检测到多个选择最合适的
if npc4_candidates:
# 按置信度排序,选择置信度最高的
npc4_candidates.sort(key=lambda x: x['confidence'], reverse=True)
# 选择最佳候选(置信度最高且面积合理)
best_npc4 = None
for candidate in npc4_candidates:
# 置信度阈值至少0.3(可根据实际情况调整)
if candidate['confidence'] >= 0.3:
# 检查检测框面积是否合理(避免过小的误检)
area = candidate['area']
if area > 100: # 最小面积阈值
best_npc4 = candidate
break
if best_npc4:
detections['npc4'] = best_npc4['position']
# 可选:输出调试信息
# print(f"✅ 检测到npc4: 位置={best_npc4['position']}, 置信度={best_npc4['confidence']:.2f}")
elif len(npc4_candidates) == 1:
# 如果只有一个候选,即使置信度较低也使用
detections['npc4'] = npc4_candidates[0]['position']
except Exception as e:
print(f"⚠️ YOLO检测出错: {e}")