Python OpenCV中的实时人类检测

2 投票
1 回答
7849 浏览
提问于 2025-04-17 22:39

我现在正在用Python2.7和openCV写一个实时人类检测程序。
我写的代码运行得非常慢,参考了这个[示例代码]

我写的代码如下。

from cv import *

def inside(r, q):
    (rx, ry), (rw, rh) = r
    (qx, qy), (qw, qh) = q
    return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh

NamedWindow("people detection demo", 1)
storage = CreateMemStorage(0) 
capture = CaptureFromCAM(0)
SetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 600)
SetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 450)
while True:
    img = QueryFrame(capture)
    found = list(HOGDetectMultiScale(img, storage, win_stride=(8,8),
                                     padding=(32,32), scale=1.05, group_threshold=2))
    found_filtered = []
    for r in found:
        insidef = False
        for q in found:
            if inside(r, q):
                insidef = True
                break
        if not insidef:
            found_filtered.append(r)
    for r in found_filtered:
        (rx, ry), (rw, rh) = r
        tl = (rx + int(rw*0.1), ry + int(rh*0.07))
        br = (rx + int(rw*0.9), ry + int(rh*0.87))
        Rectangle(img, tl, br, (0, 255, 0), 3)

    ShowImage("people detection demo", img)
    if WaitKey(10) == ord('q'):
        break

目前只能处理一张图片,但我想实现实时检测。
有没有人知道怎么加快实时人类检测程序的速度?

1 个回答

1

调整缩放参数。这是指探测器如何调整跟踪窗口的大小,也就是说,它从一个固定的大小开始,如果没有找到目标,就会按照缩放因子来增加窗口的大小等等。这个缩放因子不一定要是1.05,你可以设置成1.1、1.2,甚至更高,以此来平衡时间消耗和检测率(但不要设置得太高,因为检测率会迅速下降)。

如果你不一定要使用Python,可以考虑使用C++的接口。它支持GPU,这样可以让处理速度快上好几倍。

撰写回答