2017-03-29 64 views
0

大家好,我正在使用OpenCV使用Python开发人体检测程序。我看到了this very good example,我在它的样品上运行了它。它可以检测到人们,无论他们面对的是什么,并且具有良好的重叠检测以及模糊的运动。但是,当我在一些图像上运行它时(大多是膝盖向上,腰部向上和胸部照片的人),我发现该软件并不能检测到人。OpenCV 3 Python - 部分人体检测?

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

任何铅将非常感激。

+0

您可以使用哈尔级联,它已经被训练用于上身检测。见[这里](http://stackoverflow.com/a/31834603/5008845)及其链接 – Miki

+0

@Miki它似乎哈尔瀑布训练有素的上身检测只能检测到上半身,如果整个身体看到?我可能正在寻找一个不同的haar级联xml文件。 – Razgriz

+0

这对我来说没什么意义......但是我从来没有用过它们,所以我无法确定 – Miki

回答

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的解决方案细化。