本文介绍了HOG人使用WEBCAM检测opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测使用网络摄像头的人.
我已经尝试使用视频检测人们,并且它可以正常工作.
当我将其从视频更改为网络摄像头时,检测不起作用.

I am trying to detect people using a webcam.
I have already tried detecting people using a video and it worked.
When I change it from video to webcam the detection does not work.

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

What should be done in order to support webcam?

    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()

推荐答案

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

Try this:if your are using Laptop's Internal web Cam then put value 0 in

Frame=cv2.VideoCapture(0)

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

if you are using External Webcam then put value 1 in

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)

这篇关于HOG人使用WEBCAM检测opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 00:42