Spaces:
Sleeping
Sleeping
Delete Yolov5_Deepsort/README.md
Browse files- Yolov5_Deepsort/README.md +0 -139
Yolov5_Deepsort/README.md
DELETED
@@ -1,139 +0,0 @@
|
|
1 |
-
# 本文禁止转载!
|
2 |
-
|
3 |
-
|
4 |
-
本文地址:[https://blog.csdn.net/weixin_44936889/article/details/112002152](https://blog.csdn.net/weixin_44936889/article/details/112002152)
|
5 |
-
|
6 |
-
# 项目简介:
|
7 |
-
使用YOLOv5+Deepsort实现车辆行人追踪和计数,代码封装成一个Detector类,更容易嵌入到自己的项目中。
|
8 |
-
|
9 |
-
代码地址(欢迎star):
|
10 |
-
|
11 |
-
[https://github.com/Sharpiless/yolov5-deepsort/](https://github.com/Sharpiless/yolov5-deepsort/)
|
12 |
-
|
13 |
-
最终效果:
|
14 |
-
![在这里插入图片描述](https://github.com/Sharpiless/Yolov5-Deepsort/blob/main/image.png)
|
15 |
-
# YOLOv5检测器:
|
16 |
-
|
17 |
-
```python
|
18 |
-
class Detector(baseDet):
|
19 |
-
|
20 |
-
def __init__(self):
|
21 |
-
super(Detector, self).__init__()
|
22 |
-
self.init_model()
|
23 |
-
self.build_config()
|
24 |
-
|
25 |
-
def init_model(self):
|
26 |
-
|
27 |
-
self.weights = 'weights/yolov5m.pt'
|
28 |
-
self.device = '0' if torch.cuda.is_available() else 'cpu'
|
29 |
-
self.device = select_device(self.device)
|
30 |
-
model = attempt_load(self.weights, map_location=self.device)
|
31 |
-
model.to(self.device).eval()
|
32 |
-
model.half()
|
33 |
-
# torch.save(model, 'test.pt')
|
34 |
-
self.m = model
|
35 |
-
self.names = model.module.names if hasattr(
|
36 |
-
model, 'module') else model.names
|
37 |
-
|
38 |
-
def preprocess(self, img):
|
39 |
-
|
40 |
-
img0 = img.copy()
|
41 |
-
img = letterbox(img, new_shape=self.img_size)[0]
|
42 |
-
img = img[:, :, ::-1].transpose(2, 0, 1)
|
43 |
-
img = np.ascontiguousarray(img)
|
44 |
-
img = torch.from_numpy(img).to(self.device)
|
45 |
-
img = img.half() # 半精度
|
46 |
-
img /= 255.0 # 图像归一化
|
47 |
-
if img.ndimension() == 3:
|
48 |
-
img = img.unsqueeze(0)
|
49 |
-
|
50 |
-
return img0, img
|
51 |
-
|
52 |
-
def detect(self, im):
|
53 |
-
|
54 |
-
im0, img = self.preprocess(im)
|
55 |
-
|
56 |
-
pred = self.m(img, augment=False)[0]
|
57 |
-
pred = pred.float()
|
58 |
-
pred = non_max_suppression(pred, self.threshold, 0.4)
|
59 |
-
|
60 |
-
pred_boxes = []
|
61 |
-
for det in pred:
|
62 |
-
|
63 |
-
if det is not None and len(det):
|
64 |
-
det[:, :4] = scale_coords(
|
65 |
-
img.shape[2:], det[:, :4], im0.shape).round()
|
66 |
-
|
67 |
-
for *x, conf, cls_id in det:
|
68 |
-
lbl = self.names[int(cls_id)]
|
69 |
-
if not lbl in ['person', 'car', 'truck']:
|
70 |
-
continue
|
71 |
-
x1, y1 = int(x[0]), int(x[1])
|
72 |
-
x2, y2 = int(x[2]), int(x[3])
|
73 |
-
pred_boxes.append(
|
74 |
-
(x1, y1, x2, y2, lbl, conf))
|
75 |
-
|
76 |
-
return im, pred_boxes
|
77 |
-
|
78 |
-
```
|
79 |
-
|
80 |
-
调用 self.detect 方法返回图像和预测结果
|
81 |
-
|
82 |
-
# DeepSort追踪器:
|
83 |
-
|
84 |
-
```python
|
85 |
-
deepsort = DeepSort(cfg.DEEPSORT.REID_CKPT,
|
86 |
-
max_dist=cfg.DEEPSORT.MAX_DIST, min_confidence=cfg.DEEPSORT.MIN_CONFIDENCE,
|
87 |
-
nms_max_overlap=cfg.DEEPSORT.NMS_MAX_OVERLAP, max_iou_distance=cfg.DEEPSORT.MAX_IOU_DISTANCE,
|
88 |
-
max_age=cfg.DEEPSORT.MAX_AGE, n_init=cfg.DEEPSORT.N_INIT, nn_budget=cfg.DEEPSORT.NN_BUDGET,
|
89 |
-
use_cuda=True)
|
90 |
-
```
|
91 |
-
|
92 |
-
调用 self.update 方法更新追踪结果
|
93 |
-
|
94 |
-
# 运行demo:
|
95 |
-
|
96 |
-
```bash
|
97 |
-
python demo.py
|
98 |
-
```
|
99 |
-
|
100 |
-
# 训练自己的模型:
|
101 |
-
参考我的另一篇博客:
|
102 |
-
|
103 |
-
[【小白CV】手把手教你用YOLOv5训练自己的数据集(从Windows环境配置到模型部署)](https://blog.csdn.net/weixin_44936889/article/details/110661862)
|
104 |
-
|
105 |
-
训练好后放到 weights 文件夹下
|
106 |
-
|
107 |
-
# 调用接口:
|
108 |
-
|
109 |
-
## 创建检测器:
|
110 |
-
|
111 |
-
```python
|
112 |
-
from AIDetector_pytorch import Detector
|
113 |
-
|
114 |
-
det = Detector()
|
115 |
-
```
|
116 |
-
|
117 |
-
## 调用检测接口:
|
118 |
-
|
119 |
-
```python
|
120 |
-
result = det.feedCap(im)
|
121 |
-
```
|
122 |
-
|
123 |
-
其中 im 为 BGR 图像
|
124 |
-
|
125 |
-
返回的 result 是字典,result['frame'] 返回可视化后的图像
|
126 |
-
|
127 |
-
# 联系作者:
|
128 |
-
|
129 |
-
> B站:[https://space.bilibili.com/470550823](https://space.bilibili.com/470550823)
|
130 |
-
|
131 |
-
> CSDN:[https://blog.csdn.net/weixin_44936889](https://blog.csdn.net/weixin_44936889)
|
132 |
-
|
133 |
-
> AI Studio:[https://aistudio.baidu.com/aistudio/personalcenter/thirdview/67156](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/67156)
|
134 |
-
|
135 |
-
> Github:[https://github.com/Sharpiless](https://github.com/Sharpiless)
|
136 |
-
|
137 |
-
遵循 GNU General Public License v3.0 协议,标明目标检测部分来源:https://github.com/ultralytics/yolov5/
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|