首页 文章

OpenCV 3 Python - 部分人体检测?

提问于
浏览
0

我正在使用Python使用OpenCV进行人体检测程序 . 我看到了this very good example,我把它放在它的样品上 . 它可以检测人员,无论他们面对的是什么,并且具有适当的重叠检测以及运动模糊 .

然而,当我在一些图像上运行它时(大多数是膝盖向上,腰部以及胸部照片的人),我发现该软件并不能完全发现人 .

你可以得到photos from this link . 这是我正在使用的代码:

# 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识别出可能只有部分身体(膝盖向上,腰部向上,胸部向上)出现在视频中的人?在我的用例场景中,我不认为寻找手臂和腿是至关重要的,只要躯干和头部存在,我应该能够看到它 .

1 回答

  • 0

    我找到了haar上身级联 . 虽然它可能总是不起作用(我会发布一个关于此的新问题),但这是一个好的开始 .

    这是代码:

    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解决方案那样精致 .

相关问题