Giter VIP home page Giter VIP logo

yolov5-deepsort-inference's Introduction

本文地址:https://blog.csdn.net/weixin_44936889/article/details/112002152

注意:

本项目使用Yolov5 3.0版本,最新版本5.0请移步:

https://github.com/Sharpiless/yolov5-deepsort

注:新版本添加了类别显示功能

项目简介:

使用YOLOv5+Deepsort实现车辆行人追踪和计数,代码封装成一个Detector类,更容易嵌入到自己的项目中。

代码地址(欢迎star):

https://github.com/Sharpiless/Yolov5-deepsort-inference

最终效果: 在这里插入图片描述

YOLOv5检测器:

class Detector(baseDet):

    def __init__(self):
        super(Detector, self).__init__()
        self.init_model()
        self.build_config()

    def init_model(self):

        self.weights = 'weights/yolov5m.pt'
        self.device = '0' if torch.cuda.is_available() else 'cpu'
        self.device = select_device(self.device)
        model = attempt_load(self.weights, map_location=self.device)
        model.to(self.device).eval()
        model.half()
        # torch.save(model, 'test.pt')
        self.m = model
        self.names = model.module.names if hasattr(
            model, 'module') else model.names

    def preprocess(self, img):

        img0 = img.copy()
        img = letterbox(img, new_shape=self.img_size)[0]
        img = img[:, :, ::-1].transpose(2, 0, 1)
        img = np.ascontiguousarray(img)
        img = torch.from_numpy(img).to(self.device)
        img = img.half()  # 半精度
        img /= 255.0  # 图像归一化
        if img.ndimension() == 3:
            img = img.unsqueeze(0)

        return img0, img

    def detect(self, im):

        im0, img = self.preprocess(im)

        pred = self.m(img, augment=False)[0]
        pred = pred.float()
        pred = non_max_suppression(pred, self.threshold, 0.4)

        pred_boxes = []
        for det in pred:

            if det is not None and len(det):
                det[:, :4] = scale_coords(
                    img.shape[2:], det[:, :4], im0.shape).round()

                for *x, conf, cls_id in det:
                    lbl = self.names[int(cls_id)]
                    if not lbl in ['person', 'car', 'truck']:
                        continue
                    x1, y1 = int(x[0]), int(x[1])
                    x2, y2 = int(x[2]), int(x[3])
                    pred_boxes.append(
                        (x1, y1, x2, y2, lbl, conf))

        return im, pred_boxes

调用 self.detect 方法返回图像和预测结果

DeepSort追踪器:

deepsort = DeepSort(cfg.DEEPSORT.REID_CKPT,
                    max_dist=cfg.DEEPSORT.MAX_DIST, min_confidence=cfg.DEEPSORT.MIN_CONFIDENCE,
                    nms_max_overlap=cfg.DEEPSORT.NMS_MAX_OVERLAP, max_iou_distance=cfg.DEEPSORT.MAX_IOU_DISTANCE,
                    max_age=cfg.DEEPSORT.MAX_AGE, n_init=cfg.DEEPSORT.N_INIT, nn_budget=cfg.DEEPSORT.NN_BUDGET,
                    use_cuda=True)

调用 self.update 方法更新追踪结果

运行demo:

python demo.py

训练自己的模型:

参考我的另一篇博客:

【小白CV】手把手教你用YOLOv5训练自己的数据集(从Windows环境配置到模型部署)

训练好后放到 weights 文件夹下

调用接口:

创建检测器:

from AIDetector_pytorch import Detector

det = Detector()

调用检测接口:

func_status = {}
func_status['headpose'] = None

result = det.feedCap(im, func_status)

其中 im 为 BGR 图像

返回的 result 是字典,result['frame'] 返回可视化后的图像

关注我的公众号:

感兴趣的同学关注我的公众号——可达鸭的深度学习教程:

在这里插入图片描述

联系作者:

B站:https://space.bilibili.com/470550823

CSDN:https://blog.csdn.net/weixin_44936889

AI Studio:https://aistudio.baidu.com/aistudio/personalcenter/thirdview/67156

Github:https://github.com/Sharpiless

遵循 GNU General Public License v3.0 协议,标明目标检测部分来源:https://github.com/ultralytics/yolov5/

yolov5-deepsort-inference's People

Contributors

sharpiless avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yolov5-deepsort-inference's Issues

只有cpu的话是不是跑不了demo

运行报错 RuntimeError: "slow_conv2d_cpu" not implemented for 'Half'
查了下是Half这个变量没法在cpu上跑,所以想问下win10+cpu是不是无法泡桐该项目

为什么会有这个问题?

Traceback (most recent call last):
File "E:/TuXiangChuLi/week14/Yolov5-deepsort-inference/demo.py", line 1, in
from AIDetector_pytorch import Detector
File "E:\TuXiangChuLi\week14\Yolov5-deepsort-inference\AIDetector_pytorch.py", line 6, in
from utils.BaseDetector import baseDet
File "E:\TuXiangChuLi\week14\Yolov5-deepsort-inference\utils\BaseDetector.py", line 1, in
from tracker import update_tracker
File "E:\TuXiangChuLi\week14\Yolov5-deepsort-inference\tracker.py", line 8, in
cfg.merge_from_file("E:/TuXiangChuLi/week14/Yolov5-deepsort-inference/deep_sort/configs/deep_sort.yaml")
File "E:\TuXiangChuLi\week14\Yolov5-deepsort-inference\deep_sort\utils\parser.py", line 23, in merge_from_file
self.update(yaml.load(fo.read()))
TypeError: load() missing 1 required positional argument: 'Loader'

Process finished with exit code 1

how to train the deep sort model?

hello! thanks for your open source.
but i want to know that how to train the deep sort model which can used in your repo?
Could you give a instruction to us?
THX

能否用于自定义的数据集

我已经用自定义的数据集训练了yolov5的模型,请问用deepsort还要再训练相应的模型权重么?训练的数据集格式是什么?

一个小问题

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
文件里没有这个 requirements.txt 啊 ,我就装不上了

error

RuntimeError: "unfolded2d_copy" not implemented for 'Half'

RuntimeError: "upsample_nearest2d_channels_last" not implemented for 'Half'

我上網查了,好像跟torch版本有關,但現在只能裝2.x版而已,請問一下該如何解決?
Traceback (most recent call last):
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\demo.py", line 58, in
main()
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\demo.py", line 32, in main
result = det.feedCap(im, func_status)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\utils\BaseDetector.py", line 35, in feedCap
im, faces, face_bboxes = update_tracker(self, im)
^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\tracker.py", line 42, in update_tracker
_, bboxes = target_detector.detect(image)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\AIDetector_pytorch.py", line 47, in detect
pred = self.m(img, augment=False)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\models\yolo.py", line 112, in forward
return self.forward_once(x, profile) # single-scale inference, train
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\yolov5_deepsort_infer\Yolov5-deepsort-inference\models\yolo.py", line 128, in forward_once
x = m(x) # run
^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\modules\upsampling.py", line 157, in forward
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\yolov5_deepsort_infer\yolov5_infer\Lib\site-packages\torch\nn\functional.py", line 4001, in interpolate
return torch._C._nn.upsample_nearest2d(input, output_size, scale_factors)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: "upsample_nearest2d_channels_last" not implemented for 'Half'

数据集问题

请问车辆在水平方向转弯成垂直的时候用Reid也能跟踪到吗?如果能,训练集水平和垂直的车辆是作如何处理的?是统一resize成正方形吗?

執行速度

作者您好

我參照您的程式碼,我也更改了模型,執行官方的yolov5s模型,速度依然無法突破30fps

同機器使用官方yolov5m檢測時速度與官方給的執行速度也差很多

作者有發現這問題嘛?

謝謝

报错

This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

算法评估

如何评估算法性能,是否有相关的代码

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.