python - 如何在opencv中正确使用peopledetect.py?

标签 python opencv

我是 opencv 的新手,我确实需要在某些图像中检测人/人,我找到了名为 peopleDetect.py 的 python 接口(interface),并且我像这样查看代码..

#!/usr/bin/env python

import numpy as np
import cv2

help_message = '''
USAGE: peopledetect.py <image_names> ...

Press any key to continue, ESC to stop.
'''

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

def draw_detections(img, rects, thickness = 1):
    for x, y, w, h in rects:
        # the HOG detector returns slightly larger rectangles than the real objects.
        # so we slightly shrink the rectangles to get a nicer output.
        pad_w, pad_h = int(0.15*w), int(0.05*h)
        cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)


if __name__ == '__main__':
    import sys
    from glob import glob
    import itertools as it

    print help_message

    hog = cv2.HOGDescriptor()
    hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )

    for fn in it.chain(*map(glob, sys.argv[1:])):
        print fn, ' - ',
        try:
            img = cv2.imread(fn)
        except:
            print 'loading error'
            continue

        found, w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
        found_filtered = []
        for ri, r in enumerate(found):
            for qi, q in enumerate(found):
                if ri != qi and inside(r, q):
                    break
            else:
                found_filtered.append(r)
        draw_detections(img, found)
        draw_detections(img, found_filtered, 3)
        print '%d (%d) found' % (len(found_filtered), len(found))
        cv2.imshow('img', img)
        ch = 0xFF & cv2.waitKey()
        if ch == 27:
            break
    cv2.destroyAllWindows()

说实话,我不太明白HOG的原理。

我从opencv网站准备了一些图像,并尝试使用此代码进行一些基本测试,也许像这样..

$ ./peopledetect.py abba.png
$ ./peopledetect.py luna.jpg

但我没有看到在显示的代码中绘制任何矩形,也许我做错了..有人可以帮助我吗?非常感谢..

abba.png luan.jpg

最佳答案

此代码使用 OpenCV 的 HOG 检测器实现,请参阅 this tutorial对算法有很好的解释。 该分类器根据或多或少直立站立的人的全身图像进行训练,这就是它要检测的内容。 如果您想在可以看到人脸但看不到整个 body 的情况下检测到人,请查看 OpenCV's face detection algorithms相反。

关于python - 如何在opencv中正确使用peopledetect.py?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476343/

相关文章:

python - Scrapy 无法从 URL 下载图像

python - 从 python 子进程模块启动模块 shell 命令

python - 将 2D 数据帧转换为 3D 数据帧

c++ - 用户自定义类型的 OpenCV 矩阵

c++ - 来自视频文件的垫子数组 - opencv

python - 使用 Python 运行 SPSS 分析并检索值 (Python -> Spss -> Python)

opencv - 什么最适合您的视频跟踪?为什么?

numpy - opencv-python 在苹果 m1 芯片中编译失败

python - 我如何使用 colorchecker 在 opencv 中进行颜色校准?

Python 剥离 url 并替换正斜杠