Opencv VideoCapture 人检测速度慢

Opencv VideoCapture speed slow for people detecting

我正在使用 Python 2.7.11opencv 2.4.9。我有两个视频程序 人脸检测人物检测。不过,人脸检测还算顺利,但人脸检测就慢了。

人脸检测:

faceCascade = cv2.CascadeClassifier('C:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()

    faces = faceCascade.detectMultiScale(
        frame,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

人员检测:

hog = cv2.HOGDescriptor()
hog.setSVMDetectorcv2.HOGDescriptor_getDefaultPeopleDetector())
video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()    

    (rects, weights) = hog.detectMultiScale(
        frame, 
        winStride=(4, 4),
        padding=(8, 8), 
        scale=1.05
    )

    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)


    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)

    cv2.imshow("Before NMS", frame)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

video_capture.release()
cv2.destroyAllWindows()

其实人体检测是一个非常耗时的算法。您详细检查算法here。我们可以更改传递给 hog 函数的参数。像 winStridepaddingscale 一样,它改变了算法的速度。只能进行微调,否则会影响结果。

或者您可以在人员检测之前再执行一个步骤。像运动检测一样,如果有任何运动发生,则只检查人。您可以找到用于运动检测 here 的 python 代码。所以它消除了人们不必要的检查。