博客
关于我
使用Python+OpenCV+DVG-Utils实现人群计数与目标跟踪
阅读量:264 次
发布时间:2019-03-01

本文共 2490 字,大约阅读时间需要 8 分钟。

dvg-utils是一组强大的工具包,旨在帮助开发者快速构建高效的图像和视频处理管道。过去一年,我在多个计算机视觉项目中重复编写相同的代码块,这些块包括设置日志、读取配置文件、捕获图像或视频流、处理数据、可视化结果、保存结果以及收集性能指标。为了减少代码冗余,我与Sławomir Gilewski合作开发了dvg-utils Python包。

安装指南

在开始使用前,建议使用Python虚拟环境。更多关于虚拟环境的信息,请参考RealPython上的文章:

  • https://realpython.com/python-virtual-environments-a-primer/

假设你使用的是Linux或MacOS系统(我尚未对Windows进行测试,但理论上应该可以运行)。如果你使用Jetson设备,Jetpack SDK已经预装了OpenCV,可以跳过安装opencv-contrib-python的步骤。如果你使用树莓派,以下文章提供了安装OpenCV的详细指南:

  • https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/

为了浏览库和查看用法示例,请执行以下命令:

git clone https://github.com/jagin/dvg-utils.gitcd ./dvg-utilspip install tqdm pyyaml numpypip install opencv-contrib-pythonpip install dvg-utils

命令行工具

安装完成后,你可以通过命令行访问dvg-utils工具。例如:

dvg-utils v2i -i assets/videos/faces.mp4 -o output --display

这将将视频文件转换为一组图像。另一个示例用于将图像转换为视频文件:

dvg-utils i2v -i output -o output/my_new_file.avi --display

对于参数描述,请运行以下命令:

dvg-utils v2i -hdvg-utils i2v -hdvg-utils pm -h

视频捕获

dvg-utils支持从OpenCV的VideoCapture和树莓派相机等多种源捕获视频流。例如:

video_capture = VideoCapture(conf["videoCapture"]).open()

配置文件中的videoCapture属性可以定义不同的源(如filecamerapiCamerastream)。例如:

videoCapture:  capture:    file: assets/videos/cars_driving.mp4    start_frame: 100    end_frame: 500    camera: 0    fourcc: MJPG    resolution: [640, 480]    fps: 30    piCamera:      resolution: [640, 480]      framerate: 30      settings:        rotation: 180        stream:          # Jetson Nano camera stream          sensor-id: 0          sensor_mode: 3

你可以运行以下命令查看视频捕获进度:

python ./examples/capture_video.py

图像捕获

dvg-utils同样支持图像捕获。你可以运行以下命令:

python ./examples/capture_image.py

通过配置文件config/capture_image.yml,你可以指定图像的路径和转换参数,例如:

imageCapture:  path: assets/images/friends  valid_ext: jpg  transform:    resize:      width: 320    flip: 1

示例应用

dvg-utils包含丰富的示例,展示如何构建图像或视频处理管道。例如:

  • 对象检测:运行以下命令检测行人:
python ./examples/detect_object_video.py
  • 对象跟踪:使用OpenCV或Dlib算法跟踪对象。例如:
python ./examples/track_object_video.py -cfo objectTracker.tracker=dlib
  • 对象计数:基于行检测的计数器:
python ./examples/count_object_video.py

指标收集

dvg-utils支持收集性能指标,例如迭代时间和帧率。你可以运行以下命令查看详细指标:

python ./examples/count_object_video.py --metrics output/count_object_video.csv

使用命令行工具dvg-utils pm可以绘制性能图表:

dvg-utils pm -i output/count_object_video.csv -c ips

项目总结

dvg-utils的优势在于它允许你在不同平台和设备上(如树莓派、Jetson等)快速测试和部署计算机视觉管道。你只需通过配置文件选择源(如文件、摄像头或流媒体),无需修改代码即可运行。dvg-utils为OpenCV开发者提供了强大的工具,帮助他们专注于算法创新,而非基础配置。

参考资源

  • PyImageSearch:https://www.pyimagesearch.com/
  • Learn OpenCV:https://www.learnopencv.com/

转载地址:http://jvov.baihongyu.com/

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>