diff --git a/README.md b/README.md index e823d60d6273d49404ead0bf24f5bd219c2a9200..dbb4a4261d27aeeb95a67e81dae266fb4b2f6f1f 100644 --- a/README.md +++ b/README.md @@ -1,148 +1,31 @@ -本文地址:[https://blog.csdn.net/weixin_44936889/article/details/112002152](https://blog.csdn.net/weixin_44936889/article/details/112002152) +本文地址:[https://blog.csdn.net/qq_43521665/article/details/117223138?spm=1001.2014.3001.5501](https://blog.csdn.net/qq_43521665/article/details/117223138?spm=1001.2014.3001.5501) # 注意: 本项目使用Yolov5 3.0版本,4.0版本需要替换掉models和utils文件夹 # 项目简介: -使用YOLOv5+Deepsort实现车辆行人追踪和计数,代码封装成一个Detector类,更容易嵌入到自己的项目中。 +使用YOLOv5+Deepsort+REID实现车辆行人追踪和徘徊检测,代码封装成一个Detector类,更容易嵌入到自己的项目中。车辆检测是理论上的,主要实现的是行人检测。注意这里因为项目背景,这里只一次识别3个行人,1辆卡车和1辆汽车。 代码地址(欢迎star): -[https://github.com/Sharpiless/Yolov5-deepsort-inference](https://github.com/Sharpiless/Yolov5-deepsort-inference) +[https://codechina.csdn.net/qq_43521665/loitering-detection](https://codechina.csdn.net/qq_43521665/loitering-detection) -最终效果: -![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231090541223.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkzNjg4OQ==,size_16,color_FFFFFF,t_70) -# YOLOv5检测器: +# 项目逻辑: +使用yolov5进行物体的识别和筛选。将信息导入到deep_sort中进行分配跟踪id,并进行行人重识别,以保证重识别到的行人的id保持为之前使用的id.(如果对deep_sort分配id的方法没有概念,可以先看一下deep_sort单独对视频的处理。)最后将保存到的物体的移动轨迹进行简单的徘徊检测判断,也就是依据路程和位移的关系(所以真的是简单检测),当然你可以参考一些资料,实现更合理更精确的徘徊检测算法实现。 -```python -class Detector(baseDet): +# 项目运行: +首先需要进行人物特征的导入,这是因为我临时加上了行人重识别,可一般的重识别模型都是在有参考视频的情况下进行的,所以……只好……运行PS/query_get进行图片的保存。截取的人物图片会保存在'PS/query'文件夹下,具体的配置文件在config中。之后运行demo即可。注意这里只允许不大于三人进行特征提取,其实这里应该不限制人数,但后来的徘徊检测已经固定死了。考虑到之后deep_sort还会为每个行人分配id,为了区分重识别人物的id,所以用负数和0进行标记。可以看到,这里将目标识别人物进行0,-1,-2标记。 + feats = {} + feat = temp.get_feats() + feats[0] = feat[0:2] + feats[-1] = feat[2:4] + feats[-2] = feat[4:6] - def __init__(self): - super(Detector, self).__init__() - self.init_model() - self.build_config() +PS下的temp文件是我将重识别模型的特征提取和特征匹配两个方法提取出来,整合成一个文件,这里也就是行人充实别的接口。 +newtracker文件是徘徊检测问价,也是最核心的文件。这里是逻辑的集大成者,也就是yolov5,deep_sort,reid和徘徊检测算法都在这里进行。 +plot_bboxes方法实现框和轨迹的绘制,update_tracker实现track_id的分配,并将结果输入到plot_bboxes方法,该方法中引用get_distance方法进行路程和位移的计算,并进行轨迹的保存,之后方便进行轨迹的绘画,当然轨迹是在提出警告后才开始进行保存绘制的。具体的内容还是参考[https://blog.csdn.net/qq_43521665/article/details/117223138?spm=1001.2014.3001.5501]这篇代码中也有一些细节,例如超出视频范围内的轨迹预测,这里用到的就是离开的帧数和运动的速度,在使用过程中也要记得速度的更新。 - 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追踪器: - -```python -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: - -```bash -python demo.py -``` - -# 训练自己的模型: -参考我的另一篇博客: - -[【小白CV】手把手教你用YOLOv5训练自己的数据集(从Windows环境配置到模型部署)](https://blog.csdn.net/weixin_44936889/article/details/110661862) - -训练好后放到 weights 文件夹下 - -# 调用接口: - -## 创建检测器: - -```python -from AIDetector_pytorch import Detector - -det = Detector() -``` - -## 调用检测接口: - -```python -func_status = {} -func_status['headpose'] = None - -result = det.feedCap(im, func_status) -``` - -其中 im 为 BGR 图像 - -返回的 result 是字典,result['frame'] 返回可视化后的图像 - -# 关注我的公众号: - -感兴趣的同学关注我的公众号——可达鸭的深度学习教程: - -![在这里插入图片描述](https://img-blog.csdnimg.cn/20210127153004430.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkzNjg4OQ==,size_16,color_FFFFFF,t_70) - - -# 联系作者: - -> B站:[https://space.bilibili.com/470550823](https://space.bilibili.com/470550823) - -> CSDN:[https://blog.csdn.net/weixin_44936889](https://blog.csdn.net/weixin_44936889) - -> AI Studio:[https://aistudio.baidu.com/aistudio/personalcenter/thirdview/67156](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/67156) - -> Github:[https://github.com/Sharpiless](https://github.com/Sharpiless) 遵循 GNU General Public License v3.0 协议,标明目标检测部分来源:https://github.com/ultralytics/yolov5/