首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >OpenCV 3中的部分人体检测

OpenCV 3中的部分人体检测
EN

Stack Overflow用户
提问于 2017-03-30 00:48:21
回答 1查看 3.5K关注 0票数 0

我正在开发一个使用OpenCV和Python的人体检测程序。我看到了this very good example然后对它的样本进行了测试。它可以检测到人,无论他们面对的是哪里,并具有良好的重叠检测以及模糊运动。

然而,当我在我拥有的一些图像上运行它时(主要是膝盖以上、腰部以上和胸部向上的人的照片),我发现该软件并不能很好地检测人。

你可以拿到photos from this link。这是我使用的代码:

代码语言:javascript
复制
    # import the necessary packages
    from __future__ import print_function
    from imutils.object_detection import non_max_suppression
    from imutils import paths
    import numpy as np
    import argparse
    import imutils
    import cv2

    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--images", required=True, help="path to images directory")
    args = vars(ap.parse_args())

    # initialize the HOG descriptor/person detector
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

    # loop over the image paths
    imagePaths = list(paths.list_images(args["images"]))
    for imagePath in imagePaths:
            # load the image and resize it to (1) reduce detection time
            # and (2) improve detection accuracy
            image = cv2.imread(imagePath)
            image = imutils.resize(image, width=min(400, image.shape[1]))
            orig = image.copy()

            # detect people in the image
            (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
                    padding=(8, 8), scale=1.05)

            # draw the original bounding boxes
            for (x, y, w, h) in rects:
                    cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)

            # apply non-maxima suppression to the bounding boxes using a
            # fairly large overlap threshold to try to maintain overlapping
            # boxes that are still people
            rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
            pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)

            # draw the final bounding boxes
            for (xA, yA, xB, yB) in pick:
                    cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)

            # show some information on the number of bounding boxes
            filename = imagePath[imagePath.rfind("/") + 1:]
            print("[INFO] {}: {} original boxes, {} after suppression".format(
                    filename, len(rects), len(pick)))

            # show the output images
            cv2.imshow("Before NMS", orig)
            cv2.imshow("After NMS", image)
            cv2.waitKey(0)

这很简单。它遍历图像,找到其中的人物,然后绘制边界矩形。如果矩形重叠,它们会连接在一起,以防止假阳性和在单个人中检测到超过1个人。

然而,正如我在上面提到的,如果人们的脚的某些部分不存在,代码将无法识别他们。

有没有办法让OpenCV识别那些在视频中可能只有部分身体(膝盖以上,腰部以上,胸部向上)的人?在我的用例场景中,我认为寻找手臂和腿并不重要,只要躯干和头部存在,我就应该能够看到它。

EN

回答 1

Stack Overflow用户

发布于 2017-04-01 23:09:47

我发现了哈尔上半身的瀑布。虽然它可能并不总是有效的(我会发布一个关于这个问题的新问题),但这是一个很好的开始。

代码如下:

代码语言:javascript
复制
import numpy as np
import cv2

img = cv2.imread('path/to/img.jpg',0)

upperBody_cascade = cv2.CascadeClassifier('../path/to/haarcascade_upperbody.xml')    

arrUpperBody = upperBody_cascade.detectMultiScale(img)
if arrUpperBody != ():
        for (x,y,w,h) in arrUpperBody:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        print 'body found'

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

但它并不像我从pyimagesearch中拿来的解决方案那样精致。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43099540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档