上传文件至 /

这个提交包含在:
2025-09-26 06:21:14 +00:00
父节点 c9cfc3d3bc
当前提交 78c7a5e1ad
共有 3 个文件被更改,包括 595 次插入 和 0 次删除

24
config.yaml 普通文件
查看文件

@ -0,0 +1,24 @@
# YOLOv8检测器配置
camera:
id: 0
width: 1280
height: 720
fps: 30
detection:
model: yolov8n.pt
confidence_threshold: 0.5
iou_threshold: 0.5
use_tracking: true
output:
save_video: true
show_display: true
save_data: true
directory: detection_results
camera_parameters:
brightness: 50
contrast: 50
saturation: 50
hue: 0

571
yolov8_detector.py 普通文件
查看文件

@ -0,0 +1,571 @@
#!/usr/bin/env python3
"""
YOLOv8实时物体检测与追踪系统 - 标签显示在框内版本
修复了JSON序列化错误,并将标签显示在边界框内部
"""
import cv2
import numpy as np
import argparse
import time
import json
import csv
import os
import sys
from datetime import datetime
from collections import defaultdict, deque
# 检查并导入YOLOv8
try:
from ultralytics import YOLO
YOLO_AVAILABLE = True
except ImportError:
print("错误: 未找到ultralytics库。请使用以下命令安装: pip install ultralytics")
YOLO_AVAILABLE = False
sys.exit(1)
class YOLOv8Detector:
def __init__(self, model_path='yolov8n.pt', conf_threshold=0.5, iou_threshold=0.5):
"""
初始化YOLOv8检测器
"""
if not YOLO_AVAILABLE:
raise ImportError("YOLOv8库未安装")
# 加载模型
print(f"正在加载模型: {model_path}")
self.model = YOLO(model_path)
self.conf_threshold = conf_threshold
self.iou_threshold = iou_threshold
# 目标追踪相关变量
self.track_history = defaultdict(lambda: deque(maxlen=30))
self.object_counts = defaultdict(int)
# 统计信息
self.frame_count = 0
self.detection_count = 0
self.start_time = time.time()
# 颜色配置
self.colors = np.random.randint(0, 255, size=(1000, 3))
# 类别名称 (COCO数据集)
self.class_names = [
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck',
'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench',
'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse',
'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier',
'toothbrush'
]
# 摄像头和视频写入器
self.cap = None
self.video_writer = None
print("YOLOv8检测器初始化完成")
def setup_camera(self, camera_id=0, resolution=(1280, 720), fps=30):
"""
配置摄像头参数
"""
self.cap = cv2.VideoCapture(camera_id)
if not self.cap.isOpened():
raise ValueError(f"无法打开摄像头 {camera_id}")
# 设置摄像头参数
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, resolution[0])
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, resolution[1])
self.cap.set(cv2.CAP_PROP_FPS, fps)
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
# 获取实际参数
actual_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
actual_fps = self.cap.get(cv2.CAP_PROP_FPS)
print(f"摄像头配置成功:")
print(f" 分辨率: {actual_width}x{actual_height}")
print(f" 帧率: {actual_fps:.2f}")
return actual_width, actual_height, actual_fps
def setup_video_input(self, video_path):
"""
设置视频文件输入
"""
self.cap = cv2.VideoCapture(video_path)
if not self.cap.isOpened():
raise ValueError(f"无法打开视频文件 {video_path}")
# 获取视频属性
width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = self.cap.get(cv2.CAP_PROP_FPS)
frame_count = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"视频文件信息:")
print(f" 分辨率: {width}x{height}")
print(f" 帧率: {fps:.2f}")
print(f" 总帧数: {frame_count}")
return width, height, fps
def setup_video_output(self, output_path=None, resolution=(1280, 720), fps=30):
"""
设置视频输出
"""
if output_path is None:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_path = f"yolov8_detection_{timestamp}.mp4"
# 创建输出目录
os.makedirs(os.path.dirname(output_path) if os.path.dirname(output_path) else ".", exist_ok=True)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
self.video_writer = cv2.VideoWriter(output_path, fourcc, fps, resolution)
return output_path
def preprocess_frame(self, frame):
"""
图像预处理
"""
# YOLOv8内部会自动预处理,这里可以添加自定义预处理逻辑
return frame
def detect_objects(self, frame, use_tracking=True):
"""
使用YOLOv8进行物体检测
"""
# 预处理图像
processed_frame = self.preprocess_frame(frame)
if use_tracking:
# 使用追踪模式
results = self.model.track(
processed_frame,
conf=self.conf_threshold,
iou=self.iou_threshold,
persist=True,
verbose=False
)
else:
# 使用检测模式
results = self.model.predict(
processed_frame,
conf=self.conf_threshold,
iou=self.iou_threshold,
verbose=False
)
# 解析检测结果
detections = []
if results and len(results) > 0:
result = results[0]
# 检查是否有检测结果
if result.boxes is not None:
boxes = result.boxes.xyxy.cpu().numpy()
confidences = result.boxes.conf.cpu().numpy()
class_ids = result.boxes.cls.cpu().numpy().astype(int)
# 获取追踪ID(如果使用追踪)
track_ids = None
if use_tracking and result.boxes.id is not None:
track_ids = result.boxes.id.cpu().numpy().astype(int)
for i, (box, conf, class_id) in enumerate(zip(boxes, confidences, class_ids)):
if conf >= self.conf_threshold:
# 修复: 确保所有数据都是Python原生类型,避免JSON序列化问题
detection = {
"bbox": [float(x) for x in box], # 转换为Python float
"class_id": int(class_id), # 转换为Python int
"class_name": self.class_names[class_id] if class_id < len(
self.class_names) else f"class_{class_id}",
"confidence": float(conf), # 转换为Python float
"track_id": int(track_ids[i]) if track_ids is not None and i < len(track_ids) else None
}
detections.append(detection)
return detections
def draw_detections(self, frame, detections):
"""
在图像上绘制检测结果 - 修改版:标签显示在框内
"""
for det in detections:
bbox = det["bbox"]
class_name = det["class_name"]
confidence = det["confidence"]
track_id = det.get("track_id")
# 提取边界框坐标
x1, y1, x2, y2 = map(int, bbox)
# 根据track_id或class_id选择颜色
color_id = track_id if track_id is not None else det["class_id"]
color = tuple(map(int, self.colors[color_id % len(self.colors)]))
# 绘制边界框
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
# 创建标签文本
label = f"{class_name} {confidence:.2f}"
if track_id is not None:
label = f"ID:{track_id} {label}"
# 计算文本大小
(text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
# 确定标签背景框的位置(在检测框内部顶部)
# 确保标签不会超出图像边界
label_y1 = max(y1, 0)
label_y2 = label_y1 + text_height + 10
# 如果标签底部超出检测框顶部,调整位置到检测框内部底部
if label_y2 > y2:
label_y2 = min(y2, frame.shape[0])
label_y1 = label_y2 - (text_height + 10)
label_x1 = max(x1, 0)
label_x2 = min(x1 + text_width + 10, frame.shape[1])
# 绘制半透明标签背景
overlay = frame.copy()
cv2.rectangle(overlay, (label_x1, label_y1), (label_x2, label_y2), color, -1)
cv2.addWeighted(overlay, 0.6, frame, 0.4, 0, frame)
# 绘制标签文本
text_x = label_x1 + 5
text_y = label_y1 + text_height + 5
# 确保文本不会超出标签背景
text_x = min(text_x, label_x2 - 5)
text_y = min(text_y, label_y2 - 5)
cv2.putText(frame, label, (text_x, text_y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# 绘制追踪轨迹(如果有track_id)
if track_id is not None:
track = self.track_history[track_id]
track.append((int((x1 + x2) / 2), int((y1 + y2) / 2)))
# 绘制轨迹
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(frame, [points], isClosed=False, color=color, thickness=2)
return frame
def add_statistics_overlay(self, frame, detections, width, height):
"""
添加统计信息叠加层
"""
# 计算FPS
elapsed_time = time.time() - self.start_time
fps = self.frame_count / elapsed_time if elapsed_time > 0 else 0
# 统计各类别数量
class_counts = defaultdict(int)
for det in detections:
class_counts[det["class_name"]] += 1
# 添加统计信息文本
stats_text = [
f"FPS: {fps:.2f}",
f"帧数: {self.frame_count}",
f"检测数: {len(detections)}",
"物体统计:"
]
# 添加前5个最多的物体统计
sorted_counts = sorted(class_counts.items(), key=lambda x: x[1], reverse=True)
for i, (class_name, count) in enumerate(sorted_counts[:5]):
stats_text.append(f" {class_name}: {count}")
# 绘制文本背景
text_bg_height = len(stats_text) * 25 + 10
cv2.rectangle(frame, (10, 10), (300, text_bg_height), (0, 0, 0), -1)
cv2.rectangle(frame, (10, 10), (300, text_bg_height), (255, 255, 255), 1)
# 绘制文本
for i, text in enumerate(stats_text):
cv2.putText(frame, text, (15, 35 + i * 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
return frame
def save_detection_data(self, detections, timestamp, output_dir="detection_results"):
"""
保存检测数据到文件 - 修复JSON序列化问题
"""
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 保存到JSON文件
json_path = os.path.join(output_dir, f"detections_{datetime.now().strftime('%Y%m%d')}.json")
# 修复: 确保所有数据都是JSON可序列化的
serializable_detections = []
for det in detections:
serializable_det = {
"bbox": [float(x) for x in det["bbox"]],
"class_id": int(det["class_id"]),
"class_name": str(det["class_name"]),
"confidence": float(det["confidence"]),
"track_id": int(det["track_id"]) if det.get("track_id") is not None else None
}
serializable_detections.append(serializable_det)
json_data = {
"timestamp": str(timestamp),
"frame_number": int(self.frame_count),
"detections": serializable_detections
}
with open(json_path, "a", encoding='utf-8') as f:
f.write(json.dumps(json_data, ensure_ascii=False) + "\n")
# 保存到CSV文件
csv_path = os.path.join(output_dir, f"detections_{datetime.now().strftime('%Y%m%d')}.csv")
file_exists = os.path.isfile(csv_path)
with open(csv_path, "a", newline='', encoding='utf-8') as f:
writer = csv.writer(f)
if not file_exists:
writer.writerow(["timestamp", "frame_number", "class_id", "class_name",
"confidence", "x1", "y1", "x2", "y2", "track_id"])
for det in detections:
bbox = det["bbox"]
writer.writerow([
timestamp,
self.frame_count,
det["class_id"],
det["class_name"],
det["confidence"],
bbox[0], bbox[1], bbox[2], bbox[3],
det.get("track_id", "")
])
def export_summary_report(self, output_dir="detection_results"):
"""
导出检测摘要报告 - 修复JSON序列化问题
"""
os.makedirs(output_dir, exist_ok=True)
total_time = time.time() - self.start_time
avg_fps = self.frame_count / total_time if total_time > 0 else 0
# 修复: 确保所有数据都是JSON可序列化的
report = {
"检测摘要报告": {
"开始时间": datetime.fromtimestamp(self.start_time).strftime('%Y-%m-%d %H:%M:%S'),
"结束时间": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"总运行时间(秒)": float(round(total_time, 2)),
"处理总帧数": int(self.frame_count),
"平均FPS": float(round(avg_fps, 2)),
"总检测数": int(self.detection_count),
"物体分类统计": {k: int(v) for k, v in self.object_counts.items()} # 修复: 转换为标准字典和int
}
}
report_path = os.path.join(output_dir, f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
with open(report_path, "w", encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)
print(f"摘要报告已保存: {report_path}")
def run_detection(self, camera_id=0, video_path=None, resolution=(1280, 720), fps=30,
use_tracking=True, output_video=True, show_display=True,
save_data=True, output_dir="detection_results"):
"""
运行物体检测
"""
# 设置输入源
if video_path:
print(f"使用视频文件: {video_path}")
width, height, actual_fps = self.setup_video_input(video_path)
else:
print(f"使用摄像头: {camera_id}")
width, height, actual_fps = self.setup_camera(camera_id, resolution, fps)
# 设置视频输出
video_output_path = None
if output_video:
video_output_path = self.setup_video_output(resolution=(width, height), fps=actual_fps)
print(f"视频将保存至: {video_output_path}")
print("开始检测... (按 'q' 键退出, 按 's' 键保存截图, 按 'p' 键暂停/继续)")
paused = False
result_frame = None
try:
while True:
if not paused:
# 读取帧
ret, frame = self.cap.read()
if not ret:
print("无法读取帧,退出")
break
# 检测物体
detections = self.detect_objects(frame, use_tracking=use_tracking)
# 更新统计信息
self.frame_count += 1
self.detection_count += len(detections)
# 更新物体计数
for det in detections:
self.object_counts[det["class_name"]] += 1
# 绘制检测结果
result_frame = self.draw_detections(frame.copy(), detections)
# 添加统计信息叠加层
result_frame = self.add_statistics_overlay(result_frame, detections, width, height)
# 保存检测数据
if save_data:
timestamp = datetime.now().isoformat()
self.save_detection_data(detections, timestamp, output_dir)
# 写入视频文件
if output_video and self.video_writer is not None:
self.video_writer.write(result_frame)
# 显示结果
if show_display:
display_frame = result_frame if not paused and result_frame is not None else frame
if display_frame is not None:
status_text = "状态: 暂停" if paused else "状态: 运行中"
cv2.putText(display_frame, status_text, (width - 200, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255) if paused else (0, 255, 0), 2)
cv2.imshow('YOLOv8 Object Detection', display_frame)
# 处理按键
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
# 保存截图
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
screenshot_path = os.path.join(output_dir, f"screenshot_{timestamp}.jpg")
save_frame = result_frame if not paused and result_frame is not None else frame
if save_frame is not None:
cv2.imwrite(screenshot_path, save_frame)
print(f"截图已保存: {screenshot_path}")
elif key == ord('p'):
paused = not paused
print(f"检测已{'暂停' if paused else '继续'}")
except KeyboardInterrupt:
print("检测被用户中断")
except Exception as e:
print(f"检测过程中发生错误: {e}")
finally:
# 释放资源
if self.cap:
self.cap.release()
if output_video and self.video_writer:
self.video_writer.release()
cv2.destroyAllWindows()
# 导出摘要报告
if save_data:
self.export_summary_report(output_dir)
# 输出最终统计信息
total_time = time.time() - self.start_time
avg_fps = self.frame_count / total_time if total_time > 0 else 0
print(f"\n检测完成:")
print(f" 总运行时间: {total_time:.2f}秒")
print(f" 处理帧数: {self.frame_count}")
print(f" 平均FPS: {avg_fps:.2f}")
print(f" 总检测数: {self.detection_count}")
if self.object_counts:
print(" 物体分类统计:")
for obj_type, count in sorted(self.object_counts.items(), key=lambda x: x[1], reverse=True):
print(f" {obj_type}: {count}")
def main():
parser = argparse.ArgumentParser(description='YOLOv8实时物体检测与追踪系统')
parser.add_argument('--model', type=str, default='yolov8n.pt',
help='YOLOv8模型路径 (默认: yolov8n.pt)')
parser.add_argument('--camera', type=int, default=0,
help='摄像头ID (默认: 0)')
parser.add_argument('--video', type=str,
help='视频文件路径 (如果提供,则使用视频文件而非摄像头)')
parser.add_argument('--width', type=int, default=1280,
help='摄像头宽度 (默认: 1280)')
parser.add_argument('--height', type=int, default=720,
help='摄像头高度 (默认: 720)')
parser.add_argument('--fps', type=int, default=30,
help='帧率 (默认: 30)')
parser.add_argument('--conf', type=float, default=0.5,
help='置信度阈值 (默认: 0.5)')
parser.add_argument('--iou', type=float, default=0.5,
help='IOU阈值 (默认: 0.5)')
parser.add_argument('--no-tracking', action='store_true',
help='禁用目标追踪')
parser.add_argument('--no-video', action='store_true',
help='不保存视频')
parser.add_argument('--no-display', action='store_true',
help='不显示实时画面')
parser.add_argument('--no-save', action='store_true',
help='不保存检测数据')
parser.add_argument('--output-dir', type=str, default='detection_results',
help='输出目录 (默认: detection_results)')
args = parser.parse_args()
# 检查模型文件是否存在
if not os.path.exists(args.model):
print(f"警告: 模型文件 '{args.model}' 不存在,将尝试下载...")
# 这里可以添加自动下载逻辑,但需要用户确认
# 创建检测器实例
try:
detector = YOLOv8Detector(
model_path=args.model,
conf_threshold=args.conf,
iou_threshold=args.iou
)
# 运行检测
detector.run_detection(
camera_id=args.camera,
video_path=args.video,
resolution=(args.width, args.height),
fps=args.fps,
use_tracking=not args.no_tracking,
output_video=not args.no_video,
show_display=not args.no_display,
save_data=not args.no_save,
output_dir=args.output_dir
)
except Exception as e:
print(f"程序运行出错: {e}")
sys.exit(1)
if __name__ == "__main__":
main()

二进制
yolov8n.pt 普通文件

二进制文件未显示。