如何使用 OpenCV 检测和跟踪人员?
问题描述
我有一个固定的相机,指向室内区域.人们会在距离它约 5 米的范围内经过摄像头.使用 OpenCV,我想检测走过的人 - 我的理想返回是检测到的个人数组,带有边界矩形.
I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using OpenCV, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.
我查看了几个内置示例:
I've looked at several of the built-in samples:
- 没有一个 Python 示例真正适用
- C blob 跟踪示例看起来很有希望,但不接受实时视频,这使得测试变得困难.它也是样本中最复杂的,使得提取相关知识并将其转换为 Python API 存在问题.
- C 'motempl' 示例看起来也很有前景,因为它会根据后续视频帧计算轮廓.大概我可以使用它来找到强连接的组件并提取单个 blob 及其边界框 - 但我仍然试图找出一种方法来将在后续帧中发现的 blob 识别为同一个 blob.
- None of the Python samples really apply
- The C blob tracking sample looks promising, but doesn't accept live video, which makes testing difficult. It's also the most complicated of the samples, making extracting the relevant knowledge and converting it to the Python API problematic.
- The C 'motempl' sample also looks promising, in that it calculates a silhouette from subsequent video frames. Presumably I could then use that to find strongly connected components and extract individual blobs and their bounding boxes - but I'm still left trying to figure out a way to identify blobs found in subsequent frames as the same blob.
有没有人可以提供指导或示例 - 最好是在 Python 中?
Is anyone able to provide guidance or samples for doing this - preferably in Python?
解决方案
最新的 SVN 版本的 OpenCV 包含一个(未记录的)基于 HOG 的行人检测的实现.它甚至带有一个预训练的检测器和一个 python 包装器.基本用法如下:
The latest SVN version of OpenCV contains an (undocumented) implementation of HOG-based pedestrian detection. It even comes with a pre-trained detector and a python wrapper. The basic usage is as follows:
from cv import *
storage = CreateMemStorage(0)
img = LoadImage(file) # or read from camera
found = list(HOGDetectMultiScale(img, storage, win_stride=(8,8),
padding=(32,32), scale=1.05, group_threshold=2))
因此,您可以在每一帧中运行检测器并直接使用其输出,而不是跟踪.
So instead of tracking, you might just run the detector in each frame and use its output directly.
请参阅 src/cvaux/cvhog.cpp
了解实现,参阅 samples/python/peopledetect.py
了解更完整的 Python 示例(均在 OpenCV 源代码中).
See src/cvaux/cvhog.cpp
for the implementation and samples/python/peopledetect.py
for a more complete python example (both in the OpenCV sources).
相关文章