解决时间:3月4日
接到一个业务需求做公交车上下人头计数的活儿,任何移动目标都可以进行检测,这里跑的是人头检测数据集,待有功夫的时候进行开源。
开源人头检测数据集
训练自己的数据集的源码,这里数据集需要从voc转换为yolo数据集。
请跳往这里下载源码训练自己的数据集。
计数函数
class counter_up_down_vehicles:
def __init__(self, outputs, line_pixel, dividing_pixel, counter_recording,up_counter,down_counter):
self.outputs = outputs
self.line_pixel = line_pixel ###像素检测线
self.dividing_pixel = dividing_pixel ###分隔左转右转的像素线
self.counter_recording = counter_recording ###记录已经计过数的车辆
self.up_counter = up_counter
self.down_counter = down_counter
def counter_vehicles(self):
for i, each_box in enumerate(self.outputs):
box_centers = []
###求得每个框的中心点
box_centers[i,:] = [(each_box[0]+each_box[2])//2, (each_box[1]+each_box[3])//2, each_box[4], each_box[5]]
for box_center in box_centers:
for n in self.counter_recording:
if n == box_center[2]: ###判断该车辆是否已经记过数
continue
if box_center[0] <= self.dividing_pixel and box_center[1]>=self.line_pixel:###向下移动区域判断
self.down_counter += 1 ###向下计数+1
self.counter_recording.append(box_center[2])
elif box_center[0] > self.dividing_pixel and box_center[1] < self.line_pixel:###向上移动区域判断
self.up_counter += 1
self.counter_recording.append(box_center[2])
return self.counter_recording, self.up_counter, self.down_counter
画出两条线,用作上下区域划分。
# draw boxes for visualization
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -2]
classes2 = outputs[:, -1]
draw_boxes(im0, bbox_xyxy, [names[i] for i in classes2], classes2, identities)
draw_up_down_counter(im0, up_counter, down_counter,frame_fature, names)
cv2.line(im0, (0, line_pixel), (frame_fature[0], line_pixel),(0, 0, 255), 2)
cv2.line(im0, (dividing_pixel,0),(dividing_pixel,frame_fature[1]),(255,255,0),2)
参考资料
YOLOv5+DeepSORT多目标跟踪与计数精讲(含行人计数、车辆计数) 原创//课程
YOLOv5+DeepSORT多目标跟踪与计数精讲(含行人计数、车辆计数)_bai666的技术博客_51CTO博客
yolov5 deepsort 行人车辆 双向计数 跟踪检测//代码
yolov5 deepsort 行人车辆 双向计数 跟踪检测_中科院AI算法工程师的博客-CSDN博客
GitHub - dyh/unbox_yolov5_deepsort_counting: yolov5 deepsort 行人 车辆 跟踪 检测 计数
手把手教你搭建自己的yolov5目标检测平台(炮哥)
炮哥博客