问题
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()
回答1:
If it works with an ordinary video, I can't see why it wouldn't work with a webcam unless the processed frames vary significantly (i.e. too much noise, too large people etc.).
Are you getting image from the webcam at all (I don't see it in your code)? If it's a USB webcam, it's very likely that everything will work using OpenCV VideoReader functionality. You simply get frames from your webcam one after another in a loop. It's described in details in this tutorial: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html
回答2:
Try this: if your are using Laptop's Internal web Cam then put value 0 in
Frame=cv2.VideoCapture(0)
if you are using External Webcam then put value 1 in
Frame=cv2.VideoCapture(1)
Here is Full code:
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)
来源:https://stackoverflow.com/questions/42246042/hog-people-detection-opencv-using-webcam