避免重复计数讲解: 每个被候选框检测到的车辆会拥有自己唯一的id和类别信息,每一帧所有的车辆被检测到,并且通过满足相应上行、下行规则,在第一个循环判断中,满足条件的车辆其id会被记录下来放在一个数组,并且其相应的种类会被进行计数,每次进行规则判断决定是否计数之前,都会判断该检测框的id与之前出现过的id进行对比,判断是否重新计数
def counter_vehicles(outputs, line_pixel, dividing_pixel, counter_recording,up_counter,down_counter):
box_centers = []
for i, each_box in enumerate(outputs):
###求得每个框的中心点
box_centers.append([(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:
id_recorded = False
if len(counter_recording)==0:
if box_center[0] <= dividing_pixel and box_center[1] >= line_pixel:
down_counter[box_center[3]] += 1
counter_recording.append(box_center[2])
continue
elif box_center[0] > dividing_pixel and box_center[1] < line_pixel:
up_counter[box_center[3]] += 1
counter_recording.append(box_center[2])
continue
if len(counter_recording)>0:
for n in counter_recording:
if n == box_center[2]: ###判断该车辆是否已经记过数
id_recorded = True
break
if id_recorded:
continue
if box_center[0] <= dividing_pixel and box_center[1] >= line_pixel:
down_counter[box_center[3]] += 1
counter_recording.append(box_center[2])
continue
elif box_center[0] > dividing_pixel and box_center[1] < line_pixel:
up_counter[box_center[3]] += 1
counter_recording.append(box_center[2])
continue
return counter_recording, up_counter, down_counter