Created
June 25, 2020 12:11
-
-
Save dirkk0/b62a0faeb48bc62d8608bef9d420ecee to your computer and use it in GitHub Desktop.
opencv test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import the necessary packages | |
import numpy as np | |
import cv2 | |
# initialize the HOG descriptor/person detector | |
hog = cv2.HOGDescriptor() | |
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) | |
cv2.startWindowThread() | |
# open webcam video stream | |
cap = cv2.VideoCapture(1) | |
# the output will be written to output.avi | |
out = cv2.VideoWriter( | |
'output.avi', | |
cv2.VideoWriter_fourcc(*'MJPG'), | |
15., | |
(640,480)) | |
while(True): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
# resizing for faster detection | |
frame = cv2.resize(frame, (320, 240)) | |
# using a greyscale picture, also for faster detection | |
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) | |
# detect people in the image | |
# returns the bounding boxes for the detected objects | |
boxes, weights = hog.detectMultiScale(frame, winStride=(8,8) ) | |
print(boxes) | |
boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes]) | |
# print(boxes) | |
for (xA, yA, xB, yB) in boxes: | |
# display the detected boxes in the colour picture | |
print(xA, yA) | |
cv2.rectangle(frame, (xA, yA), (xB, yB), | |
(0, 255, 0), 2) | |
# Write the output video | |
out.write(frame.astype('uint8')) | |
# Display the resulting frame | |
cv2.imshow('frame',frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# When everything done, release the capture | |
cap.release() | |
# and release the output | |
out.release() | |
# finally, close the window | |
cv2.destroyAllWindows() | |
cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment