HOG人员使用WEBCAM检测opencv

时间:2017-02-15 09:58:11

标签: python opencv detection

我正在尝试使用网络摄像头检测人员。
我已经尝试使用视频检测人,但它确实有效。
当我将其从视频更改为网络摄像头时,检测不起作用。

为了支持网络摄像头应该怎么做?

    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__':

hog = cv2.HOGDescriptor()
hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )
cap = cv2.VideoCapture(0)
while True:
    ret,frame=cap.read()
    found,w=hog.detectMultiScale(frame, winStride=(8,8), padding=(32,32), scale=1.05)
    draw_detections(frame,found)
    cv2.imshow('feed',frame)
    if cv2.waitKey(1) & 0xFF == ord('e'):
           break
cap.release()
cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:1)

如果它适用于普通视频,我无法理解为什么它不适用于网络摄像头,除非处理过的帧变化很大(即噪音太大,人太多等)。

您是否从网络摄像头获取图像(我的代码中没有看到它)?如果它是USB网络摄像头,很可能一切都可以使用OpenCV VideoReader功能。您只需在循环中一个接一个地从网络摄像头获取帧。它在本教程中有详细描述: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

答案 1 :(得分:1)

尝试一下: 如果您使用的是笔记本电脑的内置网络摄像头,则在

中输入值0
Frame=cv2.VideoCapture(0)

如果您使用的是外部网络摄像头,则将值1放入

Frame=cv2.VideoCapture(1)

这里是完整代码:

from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import imutils
import cv2

Frame=cv2.VideoCapture(0)

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

while True:
    ret,image=Frame.read()
    image = imutils.resize(image, width=min(350, image.shape[1]))
    orig = image.copy()


    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),padding=(8, 8), scale=1.10)

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


    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(image, (xA, yA), (xB, yB), (0, 255, 0), 2)


    cv2.imshow("Body Detection", image)
    cv2.waitKey(1)
相关问题