deepsort显示自己的label
(写了限定词 但是无论识别啥都是显示ID等问题)
今天有空,之前做的一个小项目用到了可达鸭大佬的项目中的代码,最近看到一些人问怎么在自己的项目中显示自己的标签,那么我今天也来斗胆分享下我的代码思路,希望对你们有帮助。
AIDetector_pytorch.py文件
- 首先更改这部分代码
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 ['smoke', 'phone', 'drink']:
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
将lbl的列表 ['smoke', 'phone', 'drink']:
改为你的项目的标签。
tracker.py文件
2. 同样地,更改检测器中的lbl列表
def plot_bboxes(image, bboxes, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(
0.002 * (image.shape[0] + image.shape[1]) / 2) + 1 # line/font thickness
for (x1, y1, x2, y2, cls_id, pos_id) in bboxes:
if cls_id in ['smoke', 'phone','drink']:
最关键的步骤来了
3. 在deepsort检测器检测的返回结果中添加我们的label
首先创建一个字符型变量 track_cls=''
confs = []
bboxes2draw = []
face_bboxes = []
track_cls=''
然后遍历pytorch检测器的结果列表
for x1, y1, x2, y2, lbl, conf in bboxes:
obj = [
int((x1+x2)/2), int((y1+y2)/2),
x2-x1, y2-y1
]
bbox_xywh.append(obj)
confs.append(conf)
track_cls = lbl
把我们的标签传给 track_cls
最后在deepsort检测器的结果中添加track_cls
for value in list(outputs):
x1,y1,x2,y2,track_id = value
bboxes2draw.append(
(x1, y1, x2, y2, track_cls, track_id)
)
到此我们就能运行demo.py文件看见我们的label显示了
最后分享一下我做的小项目
欢迎star!
基于深度学习的分心驾驶行为预警系统
=========================================
显示多个标签
可以通过更改源代码的方法把检测到的cls塞进去,具体的方法是,在deepsort文件里,加入一个cls的序列然后一同塞进track类初始化,在该类下增加属性cls然后在后续confirm匹配之后update 把当前帧的 cls 赋给track对象的cls做更新。