如何使用OpenCV检测和跟踪人员?

时间:2021-08-04 00:39:53

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.

我有一个静止的相机,指向室内区域。人们将走过相机,距离它约5米。使用OpenCV,我想检测走过的人 - 我理想的回归是一组检测到的个体,带有边界矩形。

I've looked at several of the built-in samples:

我查看了几个内置示例:

  • None of the Python samples really apply
  • 没有Python样本真正适用
  • 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.
  • C blob跟踪示例看起来很有前景,但不接受实时视频,这使测试变得困难。它也是最复杂的样本,使得提取相关知识并将其转换为Python API存在问题。
  • 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.
  • C'motempl'样本也看起来很有前途,因为它可以计算后续视频帧的轮廓。据推测,我可以使用它来查找强连接组件并提取单个blob及其边界框 - 但我仍然试图找出一种方法来识别后续帧中发现的blob作为相同的blob。

Is anyone able to provide guidance or samples for doing this - preferably in Python?

是否有人能够提供指导或样本 - 最好是在Python中?

4 个解决方案

#1


27  

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:

OpenCV的最新SVN版本包含基于HOG的行人检测的(未记录的)实现。它甚至配备了预先训练好的探测器和python包装器。基本用法如下:

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.

因此,您可以在每个帧中运行检测器而不是跟踪,而是直接使用其输出。

See src/cvaux/cvhog.cpp for the implementation and samples/python/peopledetect.py for a more complete python example (both in the OpenCV sources).

有关实现,请参阅src / cvaux / cvhog.cpp,有关更完整的python示例,请参阅samples / python / peopledetect.py(两者都在OpenCV源中)。

#2


5  

Nick,

缺口,

What you are looking for is not people detection, but motion detection. If you tell us a lot more about what you are trying to solve/do, we can answer better. Anyway, there are many ways to do motion detection depending on what you are going to do with the results. Simplest one would be differencing followed by thresholding while a complex one could be proper background modeling -> foreground subtraction -> morphological ops -> connected component analysis, followed by blob analysis if required. Download the opencv code and look in samples directory. You might see what you are looking for. Also, there is an Oreilly book on OCV.

您正在寻找的不是人物检测,而是运动检测。如果您告诉我们更多关于您要解决/做的事情,我们可以更好地回答。无论如何,根据您要对结果做什么,有很多方法可以进行运动检测。最简单的一个是差分,然后进行阈值处理,而复杂的一个可以是适当的背景建模 - >前景减法 - >形态学操作 - >连通成分分析,然后根据需要进行斑点分析。下载opencv代码并查看samples目录。你可能会看到你在寻找什么。此外,还有一本关于OCV的Oreilly书。

Hope this helps, Nand

希望这有帮助,Nand

#3


4  

This is clearly a non-trivial task. You'll have to look into scientific publications for inspiration (Google Scholar is your friend here). Here's a paper about human detection and tracking: Human tracking by fast mean shift mode seeking

这显然是一项非常重要的任务。您必须查看科学出版物的灵感(Google Scholar是您的朋友)。这是一篇关于人体检测和跟踪的论文:通过快速平均移位模式搜索进行人体跟踪

#4


1  

This is similar to a project we did as part of a Computer Vision course, and I can tell you right now that it is a hard problem to get right.

这类似于我们作为计算机视觉课程的一部分所做的项目,我现在可以告诉你,要做到正确是一个难题。

You could use foreground/background segmentation, find all blobs and then decide that they are a person. The problem is that it will not work very well since people tend to go together, go past each other and so on, so a blob might very well consist of two persons and then you will see that blob splitting and merging as they walk along.

您可以使用前景/背景分割,找到所有斑点,然后确定它们是一个人。问题是它不会很好地工作,因为人们往往会一起走,走过彼此等等,所以一个blob很可能由两个人组成,然后你会看到blob在他们走过时分裂和合并。

You will need some method of discriminating between multiple persons in one blob. This is not a problem I expect anyone being able to answer in a single SO-post.

你需要一些方法来区分一个blob中的多个人。这不是一个问题,我希望有人能够在一个SO帖子中回答。

My advice is to dive into the available research and see if you can find anything there. The problem is not unsolvavble considering that there exists products which do this: Autoliv has a product to detect pedestrians using an IR-camera on a car, and I have seen other products which deal with counting customers entering and exiting stores.

我的建议是深入研究可用的研究,看看你能在那里找到任何东西。考虑到存在这样的产品,问题并非无法解决:Autoliv有一种产品可以使用汽车上的红外摄像头检测行人,我看到其他产品涉及计算客户进出商店的数量。

#1


27  

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:

OpenCV的最新SVN版本包含基于HOG的行人检测的(未记录的)实现。它甚至配备了预先训练好的探测器和python包装器。基本用法如下:

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.

因此,您可以在每个帧中运行检测器而不是跟踪,而是直接使用其输出。

See src/cvaux/cvhog.cpp for the implementation and samples/python/peopledetect.py for a more complete python example (both in the OpenCV sources).

有关实现,请参阅src / cvaux / cvhog.cpp,有关更完整的python示例,请参阅samples / python / peopledetect.py(两者都在OpenCV源中)。

#2


5  

Nick,

缺口,

What you are looking for is not people detection, but motion detection. If you tell us a lot more about what you are trying to solve/do, we can answer better. Anyway, there are many ways to do motion detection depending on what you are going to do with the results. Simplest one would be differencing followed by thresholding while a complex one could be proper background modeling -> foreground subtraction -> morphological ops -> connected component analysis, followed by blob analysis if required. Download the opencv code and look in samples directory. You might see what you are looking for. Also, there is an Oreilly book on OCV.

您正在寻找的不是人物检测,而是运动检测。如果您告诉我们更多关于您要解决/做的事情,我们可以更好地回答。无论如何,根据您要对结果做什么,有很多方法可以进行运动检测。最简单的一个是差分,然后进行阈值处理,而复杂的一个可以是适当的背景建模 - >前景减法 - >形态学操作 - >连通成分分析,然后根据需要进行斑点分析。下载opencv代码并查看samples目录。你可能会看到你在寻找什么。此外,还有一本关于OCV的Oreilly书。

Hope this helps, Nand

希望这有帮助,Nand

#3


4  

This is clearly a non-trivial task. You'll have to look into scientific publications for inspiration (Google Scholar is your friend here). Here's a paper about human detection and tracking: Human tracking by fast mean shift mode seeking

这显然是一项非常重要的任务。您必须查看科学出版物的灵感(Google Scholar是您的朋友)。这是一篇关于人体检测和跟踪的论文:通过快速平均移位模式搜索进行人体跟踪

#4


1  

This is similar to a project we did as part of a Computer Vision course, and I can tell you right now that it is a hard problem to get right.

这类似于我们作为计算机视觉课程的一部分所做的项目,我现在可以告诉你,要做到正确是一个难题。

You could use foreground/background segmentation, find all blobs and then decide that they are a person. The problem is that it will not work very well since people tend to go together, go past each other and so on, so a blob might very well consist of two persons and then you will see that blob splitting and merging as they walk along.

您可以使用前景/背景分割,找到所有斑点,然后确定它们是一个人。问题是它不会很好地工作,因为人们往往会一起走,走过彼此等等,所以一个blob很可能由两个人组成,然后你会看到blob在他们走过时分裂和合并。

You will need some method of discriminating between multiple persons in one blob. This is not a problem I expect anyone being able to answer in a single SO-post.

你需要一些方法来区分一个blob中的多个人。这不是一个问题,我希望有人能够在一个SO帖子中回答。

My advice is to dive into the available research and see if you can find anything there. The problem is not unsolvavble considering that there exists products which do this: Autoliv has a product to detect pedestrians using an IR-camera on a car, and I have seen other products which deal with counting customers entering and exiting stores.

我的建议是深入研究可用的研究,看看你能在那里找到任何东西。考虑到存在这样的产品,问题并非无法解决:Autoliv有一种产品可以使用汽车上的红外摄像头检测行人,我看到其他产品涉及计算客户进出商店的数量。