Function introduction
- Realized the separate counting of the local area's exit/entry.
- Display the detection category and the number of IDs.
- The default is south/north direction detection, if you want to detect different positions and directions, you need to modify it
- Click to run in count_car/traffic.py
- Default detection categories: pedestrian, bicycle, car, motorcycle, bus, truck, boat.
- The detection class can be modified in the objdetector.py file.
code run
$ git clone 追踪代码
Therefore, the repo contains files such as weights and mp4. If the speed of git clone is slow, you can directly download the zip
enter directory
$ cd unbox_yolov5_deepsort_counting
Create a python virtual environment
$ python3 -m venv venv
Activate the virtual environment
$ source venv/bin/activate
upgrade pip
$ python -m pip install --upgrade pip
Install pytorch
According to your operating system, installation tool and CUDA version, find the corresponding installation command at https://pytorch.org/get-started/locally/. My environment is ubuntu 18.04.5, pip, CUDA 11.0. ![Insert picture description here](https://img-blog.csdnimg.cn/77d60150764741b0bafa0927e1042a46.png
$ pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
install package
$ pip install -r requirements.txt
In the demo.py file, set the video file path to be detected, the default is'./video/test.mp4'
capture = cv2.VideoCapture(‘./video/test.mp4’)
run the program
python count.py
demo code
detector = Detector()
# 打开视频
capture = cv2.VideoCapture(VIDEO_PATH)
while True:
# 读取每帧图片
_, im = capture.read()
if im is None:
break
# 缩小尺寸
im = cv2.resize(im, (width//2, height//2))
list_bboxs = []
# 更新跟踪器
output_image_frame, list_bboxs = objtracker.update(detector, im)
# 输出图片
output_image_frame = cv2.add(output_image_frame, color_polygons_image)
if len(list_bboxs) > 0:
# ----------------------判断撞线----------------------
for item_bbox in list_bboxs:
x1, y1, x2, y2, _, track_id = item_bbox
# 撞线检测点,(x1,y1),y方向偏移比例 0.0~1.0
y1_offset = int(y1 + ((y2 - y1) * 0.6))
# 撞线的点
y = y1_offset
x = x1
if polygon_mask_blue_and_yellow[y, x] == 1:
# 如果撞 蓝polygon
if track_id not in list_overlapping_blue_polygon:
list_overlapping_blue_polygon.append(track_id)
# 判断 黄polygon list里是否有此 track_id
# 有此track_id,则认为是 UP (上行)方向
if track_id in list_overlapping_yellow_polygon:
# 上行+1
up_count += 1
print('up count:', up_count, ', up id:', list_overlapping_yellow_polygon)
# 删除 黄polygon list 中的此id
list_overlapping_yellow_polygon.remove(track_id)
elif polygon_mask_blue_and_yellow[y, x] == 2:
# 如果撞 黄polygon
if track_id not in list_overlapping_yellow_polygon:
list_overlapping_yellow_polygon.append(track_id)
# 判断 蓝polygon list 里是否有此 track_id
# 有此 track_id,则 认为是 DOWN(下行)方向
if track_id in list_overlapping_blue_polygon:
# 下行+1
down_count += 1
print('down count:', down_count, ', down id:', list_overlapping_blue_polygon)
# 删除 蓝polygon list 中的此id
list_overlapping_blue_polygon.remove(track_id)
# ----------------------清除无用id----------------------
list_overlapping_all = list_overlapping_yellow_polygon + list_overlapping_blue_polygon
for id1 in list_overlapping_all:
is_found = False
for _, _, _, _, _, bbox_id in list_bboxs:
if bbox_id == id1:
is_found = True
if not is_found:
# 如果没找到,删除id
if id1 in list_overlapping_yellow_polygon:
list_overlapping_yellow_polygon.remove(id1)
if id1 in list_overlapping_blue_polygon:
list_overlapping_blue_polygon.remove(id1)
list_overlapping_all.clear()
# 清空list
list_bboxs.clear()
else:
# 如果图像中没有任何的bbox,则清空list
list_overlapping_blue_polygon.clear()
list_overlapping_yellow_polygon.clear()
# 输出计数信息
text_draw = 'DOWN: ' + str(down_count) + \
' , UP: ' + str(up_count)
output_image_frame = cv2.putText(img=output_image_frame, text=text_draw,
org=draw_text_postion,
fontFace=font_draw_number,
fontScale=0.75, color=(0, 0, 255), thickness=2)
cv2.imshow('Counting Demo', output_image_frame)
cv2.waitKey(1)
capture.release()
cv2.destroyAllWindows()
Result display
Various tracking, ranging, attitude estimation, target detection, counting, and speed measurement functions have been realized, welcome to communicate!
See the homepage for more projects!