이재빈

Human Detect Module

1 +# import the necessary packages
2 +import numpy as np
3 +import cv2
4 +import time
5 +# initialize the HOG descriptor/person detector
6 +hog = cv2.HOGDescriptor()
7 +hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
8 +
9 +cv2.startWindowThread()
10 +fname = "./croppedimg/"
11 +# open webcam video stream
12 +cap = cv2.VideoCapture(0)
13 +i = 0
14 +while (True):
15 + # Capture frame-by-frame
16 + start = time.time()
17 + ret, frame = cap.read()
18 + # resizing for faster detection[240,160] [320 * 240]
19 + frame = cv2.resize(frame, (240, 160))
20 + # using a greyscale picture, also for faster detection
21 + gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
22 +
23 + # detect people in the image
24 + # returns the bounding boxes for the detected objects
25 + boxes, weights = hog.detectMultiScale(frame, winStride=(8, 8))
26 +
27 + boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes])
28 + for (xA, yA, xB, yB) in boxes:
29 + # display the detected boxes in the colour picture
30 + cv2.rectangle(frame, (xA, yA), (xB, yB),
31 + (0, 255, 0), 2)
32 + if(i%10 == 0):
33 + cropped = frame[yA:yB,xA:xB]
34 + s = fname + str(i)+'.jpg'
35 + cv2.imwrite(s, cropped) # IMG File Write
36 + print("time :", time.time() - start)
37 + print("Human Detect!") #Alert
38 + if(i > 200):
39 + i=0
40 + i= i+1
41 + # Display the resulting frame
42 +
43 +
44 + cv2.imshow('frame', frame)
45 + if cv2.waitKey(1) & 0xFF == ord('q'):
46 + break
47 +
48 +# When everything done, release the capture
49 +cap.release()
50 +# and release the output
51 +# finally, close the window
52 +cv2.destroyAllWindows()
53 +cv2.waitKey(1)
...\ No newline at end of file ...\ No newline at end of file