简单行人检测
import datetime
import cv2 as cv
import numpy as np
# initialize the HOG descriptor/person detector
hog = cv.HOGDescriptor()
hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
capture = cv.VideoCapture(0)
ret , image_base = capture.read()
(h,w) = image_base.shape[:2]
while(ret):
ret, image_now = capture.read()
if cv.waitKey(1) == ord('q'):
break
# detect people in the image
start = datetime.datetime.now()
image = cv.absdiff(image_now,image_base)
winStride = (4, 4)
padding = (16, 16)
scale = 1.05
meanShift = 0
(rects, weights) = hog.detectMultiScale(image, winStride=winStride, padding=padding, scale=scale,
useMeanshiftGrouping=meanShift)
print("[INFO] detection took: {}s".format(
(datetime.datetime.now() - start).total_seconds()))
# draw the original bounding boxes
if len(rects) != 0 :
for (x, y, w, h) in rects:
cv.rectangle(image_now, (x, y), (x + w, y + h), (0, 255, 0), 2)
ret,image_base = capture.read()
else :
image_base = image_now
image3 = np.vstack([image_now,image])
cv.imshow("Detections and diff", image3)
cv.waitKey(0)