如何在深度学习目标检测中建立基于YOLOv8和DeepSort的智能寻人人员检测系统,行人重识别(ReID)和目标跟踪,QT界面进行展示

基于YOLOv5+Deepsort的智能寻人系统 行人重识别+目标跟踪 QT界面


在这里插入图片描述
1
在这里插入图片描述
1
在这里插入图片描述
构建基于YOLOv8和DeepSort的智能寻人系统,包括行人重识别(ReID)和目标跟踪,使用QT界面进行展示,

以下是一个完整的流程,从安装依赖、准备数据集、配置YOLOv8、训练模型、评估模型、推理代码到构建GUI应用程序。
仅供参考,

1. 安装依赖

确保已经安装了必要的Python库:

pip install ultralytics opencv-python-headless numpy torch torchvision scikit-learn

2. 准备数据集

数据集转换:VOC格式 → YOLO格式

假设你已经有了标注好的数据集,以下是转换脚本:

import os
import xml.etree.ElementTree as ET

def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

def convert_annotation(xml_file, output_file, classes):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    with open(output_file, 'w') as out_file:
        for obj in root.iter('object'):
            cls = obj.find('name').text
            if cls not in classes:
                continue
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text),
                 float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
            bb = convert((w, h), b)
            out_file.write(f"{cls_id} {' '.join(map(str, bb))}\n")

# 配置路径和类别
classes = ['person']
xml_dir = './path/to/voc/annotations'
output_dir = './path/to/yolo/labels'

os.makedirs(output_dir, exist_ok=True)

for xml_file in os.listdir(xml_dir):
    if xml_file.endswith('.xml'):
        convert_annotation(os.path.join(xml_dir, xml_file),
                           os.path.join(output_dir, xml_file.replace('.xml', '.txt')),
                           classes)
创建data.yaml配置文件

创建一个data.yaml文件,描述数据集路径和类别信息:

train: ./path/to/train/images
val: ./path/to/val/images
test: ./path/to/test/images

nc: 1
names: ['person']

3. 配置YOLOv8并训练模型

加载YOLOv8模型并开始训练
from ultralytics import YOLO

# 加载YOLOv8模型
model = YOLO('yolov8n.yaml')  # 或者使用预训练权重:'yolov8n.pt'

# 开始训练
results = model.train(data='./path/to/data.yaml',
                      epochs=100,
                      imgsz=640,  # 输入图像大小
                      batch=16)   # 批次大小
模型评估

训练完成后,可以对验证集进行评估:

metrics = model.val()  # 使用验证集评估模型
print(f"Validation mAP: {metrics.box.map}")

4. 推理代码

编写推理代码,用于检测单张图片中的行人:

from PIL import Image
import cv2
import torch
from deep_sort.deep_sort import DeepSort

def detect_image(image_path, model, deepsort):
    results = model.predict(source=image_path, save=False)  # 进行推理
    image = cv2.imread(image_path)
    detections = []
    for pred in results:
        boxes = pred.boxes.xyxy.cpu().numpy()
        confs = pred.boxes.conf.cpu().numpy()
        cls_ids = pred.boxes.cls.cpu().numpy()
        for box, conf, cls_id in zip(boxes, confs, cls_ids):
            if conf > 0.5:  # 设置置信度阈值
                detections.append((*box, conf))

    # 使用DeepSort进行跟踪
    outputs = deepsort.update(detections, image)

    for track in outputs:
        x1, y1, x2, y2, id = track
        label = f'ID: {int(id)}'
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    
    return image

# 示例用法
image_path = './path/to/test/image.jpg'
model = YOLO('./path/to/best.pt')  # 加载训练好的模型
deepsort = DeepSort("deep_sort/deep/checkpoint/ckpt.t7")  # 初始化DeepSort

detected_image = detect_image(image_path, model, deepsort)
cv2.imshow('Detected Image', detected_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. 构建GUI应用程序

使用PyQt5构建一个简单的GUI应用程序,用于选择图片并展示检测结果:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QFileDialog
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import cv2
from ultralytics import YOLO
from deep_sort.deep_sort import DeepSort

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

        # 加载模型
        self.model = YOLO('./path/to/best.pt')
        self.deepsort = DeepSort("deep_sort/deep/checkpoint/ckpt.t7")

    def initUI(self):
        self.setWindowTitle('智能寻人系统')

        layout = QVBoxLayout()

        self.image_label = QLabel(self)
        layout.addWidget(self.image_label)

        self.select_button = QPushButton('选择图片', self)
        self.select_button.clicked.connect(self.select_image)
        layout.addWidget(self.select_button)

        self.setLayout(layout)
        self.show()

    def select_image(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Image Files (*.jpg *.jpeg *.png)", options=options)
        if file_name:
            self.detect_and_show(file_name)

    def detect_and_show(self, image_path):
        image = cv2.imread(image_path)
        detections = []
        results = self.model.predict(source=image_path, save=False)
        for pred in results:
            boxes = pred.boxes.xyxy.cpu().numpy()
            confs = pred.boxes.conf.cpu().numpy()
            cls_ids = pred.boxes.cls.cpu().numpy()
            for box, conf, cls_id in zip(boxes, confs, cls_ids):
                if conf > 0.5:
                    detections.append((*box, conf))

        outputs = self.deepsort.update(detections, image)

        for track in outputs:
            x1, y1, x2, y2, id = track
            label = f'ID: {int(id)}'
            cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

        height, width, channel = image.shape
        bytes_per_line = 3 * width
        q_img = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
        pixmap = QPixmap.fromImage(q_img)
        self.image_label.setPixmap(pixmap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

6. 运行

  1. 确保所有路径和参数正确无误。
  2. 运行上述代码,启动GUI应用程序。
  3. 在GUI中选择一张测试图片,程序会自动检测行人并显示结果。

总结 仅供参考

安装依赖、准备数据集、配置YOLOv8、训练模型、评估模型、推理代码到构建GUI应用程序的完整流程。同学,长点心,请根据实际需求调整路径、参数和代码细节。

仅供参考。

### 使用 YOLOv5 DeepSort 实现多目标检测与跟踪 #### 一、环境准备 为了成功运行YOLOv5DeepSort,需先安装必要的库。这通常涉及Python及其科学计算栈的设置。 对于YOLOv5而言,其依赖项包括但不限于PyTorch框架以及一些用于图像处理其他辅助功能的标准库[^1]。而针对DeepSort,则除了上述提到的内容外还需要额外配置Kalman滤波器相关的组件支持ReID特性的预训练模型文件等资源[^3]。 #### 二、集成YOLOv5与DeepSort 当两个系统的开发环境都准备好之后,下一步就是将它们结合起来工作。具体来说,在完成对象检测后得到的结果会被传递给DeepSort模块作为输入来初始化或更新轨迹状态;与此同时,通过设定合适的阈值参数可以控制匹配过程中的不确定性程度从而提高整体性能表现。 ```python from yolov5.models.experimental import attempt_load from deep_sort.utils.parser import get_config from deep_sort.deep_sort import DeepSort # 加载yolo模型权重路径 weights_path = 'path/to/yolov5/weight' device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = attempt_load(weights=weights_path, map_location=device) deepsort_cfg = get_config() deepsort_cfg.merge_from_file("path/to/deepsort/config.yaml") deepsort = DeepSort(cfg=deepsort_cfg, use_cuda=torch.cuda.is_available()) ``` #### 三、执行检测与跟踪流程 一旦完成了前期准备工作,就可以进入核心环节——实时视频流上的多目标检测与跟踪了。这里的关键在于如何有效地衔接两者之间的交互逻辑并确保整个系统能够稳定高效地运作下去。 每次接收到新的帧时,首先利用YOLOv5进行快速准确的目标定位;接着把获得的信息传送给DeepSort做进一步分析处理以维持连续稳定的踪迹记录直至结束条件满足为止。 ```python def detect_and_track(frame): results = model(frame) bboxes = [] confidences = [] class_ids = [] for *xyxy, conf, cls in reversed(results.xyxy[0]): bbox = [int(x.item()) for x in xyxy] confidence = float(conf.item()) class_id = int(cls.item()) bboxes.append(bbox) confidences.append(confidence) class_ids.append(class_id) outputs = deepsort.update(bboxes=bboxes, confidences=confidences, classes=class_ids, ori_img=frame) return outputs ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值