python
import argparse
from ultralytics import YOLO
import cv2
import os
from pathlib import Path
import re
import random # 用于生成随机颜色
def transform_string(input_str):
"""
将类似 "['Fire', ['Fire', 'fire']] 0.83" 的字符串转换为 "Fire 0.83"
参数:
input_str (str): 输入的字符串
返回:
str: 转换后的字符串
"""
pattern = r"\['(\w+)',\s*\[.*\]\]\s*([\d.]+)"
match = re.match(pattern, input_str)
if match:
word = match.group(1)
number = match.group(2)
return f"{word} {number}"
else:
return input_str
def parse_args():
parser = argparse.ArgumentParser(description='YOLOv8 Object Detection')
parser.add_argument('--modelname', type=str, required=True, help='Path to YOLOv8 model')
parser.add_argument('--image_path', type=str, required=True, help='Path to image folder')
return parser.parse_args()
def detect_objects(model_path, image_folder):
model_path = model_path.replace(" ", "")
image_folder = image_folder.replace(" ", "")
result_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.webp']
for ext in result_extensions:
result_images = list(Path(image_folder).glob(f'*_result{ext}'))
result_images.extend(Path(image_folder).glob(f'*_result{ext.upper()}'))
for img_path in result_images:
try:
os.remove(img_path)
print(f"已删除旧的结果图片: {img_path}")
except Exception as e:
print(f"无法删除文件 {img_path}: {e}")
model = YOLO(model_path)
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.webp']
image_files = []
for ext in image_extensions:
image_files.extend(Path(image_folder).glob(f'*{ext}'))
image_files.extend(Path(image_folder).glob(f'*{ext.upper()}'))
for img_path in image_files:
if '_result' in img_path.stem:
continue
img = cv2.imread(str(img_path))
if img is None:
print(f"无法读取图片: {img_path}")
continue
if "垃圾检测" in str(img_path):
results = model.predict(img, save=False, conf=0.1, iou=0.3)
elif "水面漂浮物" in str(img_path):
results = model.predict(img, save=False, conf=0.1, iou=0.3)
else:
results = model(img)
if "吸烟" in str(img_path):
if results[0].boxes.shape[0] < 1:
results = model.predict(img, save=True, classes=0, conf=0.02, iou=0.2)
else:
results = model.predict(img, save=True, classes=0, conf=0.28, iou=0.5)
if "打电话" in str(img_path):
if results[0].boxes.shape[0] < 1:
results = model.predict(img, conf=0.03, save=True, classes=0, iou=0.1)
else:
results = model.predict(img, conf=0.47, save=True, classes=0, iou=0.7)
result = results[0]
# 获取图像尺寸
img_height, img_width = img.shape[:2]
# 为每个类别分配颜色
class_colors = {}
colors_list = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 青色
(255, 0, 255), # 品红
(0, 255, 255), # 黄色
(128, 0, 0), # 深红
(0, 128, 0), # 深绿
(0, 0, 128), # 深蓝
(128, 128, 0), # 橄榄
(128, 0, 128), # 紫色
(0, 128, 128), # 深青
(128, 128, 128), # 灰色
(0, 0, 0), # 黑色
(255, 255, 255) # 白色
]
for idx, cls_id in enumerate(result.names.keys()):
class_colors[cls_id] = colors_list[idx % len(colors_list)]
# 如果是人员统计场景,初始化计数器
if "人员统计" in str(img_path):
person_count = 0
vehicle_count = 0
# 绘制边界框和标签
for box in result.boxes:
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
conf = float(box.conf)
cls = int(box.cls)
class_name = result.names[cls]
if isinstance(class_name, list):
class_name = class_name[0]
elif not isinstance(class_name, str):
class_name = str(class_name)
label = f"{class_name} {conf:.2f}"
label = transform_string(label)
# 如果是人员统计场景,需要处理特殊逻辑
if "人员统计" in str(img_path):
# 对于 Person 类别,置信度小于 0.4 时,不绘制框和标签
if 'Person' in class_name:
if conf < 0.3:
continue # 跳过该目标,不绘制,不计数
else:
person_count += 1 # 计数
elif 'Vehicle' in class_name:
if conf > 0.4:
vehicle_count += 1 # 计数
# 获取对应类别的颜色
color = class_colors.get(cls, (0, 255, 0))
# 绘制边界框
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
# 计算文本尺寸
(text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
# 文本初始位置
text_x = x1
text_y = y1 - 10
# 调整文本位置,确保不超出右边界
if text_x + text_width > img_width:
text_x = img_width - text_width - 1 # 距离右边框留1像素
# 调整文本位置,确保不超出上边界
if text_y - text_height < 0:
text_y = y1 + text_height + 10 # 放到边界框下方
# 确保文本位置在图像内
text_x = max(0, text_x)
text_y = max(text_height, text_y)
text_y = min(img_height - 1, text_y) # 确保不超出下边界
# 绘制文本背景框
rect_x1 = text_x
rect_y1 = text_y - text_height - baseline
rect_x2 = text_x + text_width
rect_y2 = text_y + baseline
# 确保背景框位置在图像内
rect_x1 = max(0, rect_x1)
rect_y1 = max(0, rect_y1)
rect_x2 = min(img_width - 1, rect_x2)
rect_y2 = min(img_height - 1, rect_y2)
cv2.rectangle(img, (rect_x1, rect_y1), (rect_x2, rect_y2), color, -1)
# 绘制文本
cv2.putText(img, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# 如果是人员统计场景,在右上角显示统计结果
if "人员统计" in str(img_path):
# 准备统计文本
stats_text = f"Result: {person_count} Person, {vehicle_count} Vehicle"
# 获取文本大小
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
thickness = 2
(text_width, text_height), _ = cv2.getTextSize(stats_text, font, font_scale, thickness)
# 计算文本位置(右上角,留出一定边距)
text_x = img_width - text_width - 10 # 距离右边缘10像素
text_y = text_height + 10 # 距离上边缘10像素
# 确保文本不超出图片范围
text_x = max(text_x, 0)
text_y = max(text_y, text_height + 10)
# 绘制统计结果
cv2.putText(img, stats_text, (text_x, text_y), font, font_scale, (0, 0, 255), thickness)
output_path = img_path.parent / f"{img_path.stem}_result{img_path.suffix}"
cv2.imwrite(str(output_path), img)
print(f"处理完成: {img_path} -> {output_path}")
def main():
args = parse_args()
detect_objects(args.modelname, args.image_path)
if __name__ == "__main__":
main()
通过以上修改,文本标签将在图像内部显示,不会因为超出图像边界而看不见。同时,不同类别使用不同的颜色,便于区分。虽然完全避免标签之间的重叠需要更加复杂的逻辑,但通过调整文本的位置,已经尽量减少了重叠的可能性。