Showing
1 changed file
with
44 additions
and
6 deletions
... | @@ -7,36 +7,74 @@ hog = cv2.HOGDescriptor() | ... | @@ -7,36 +7,74 @@ hog = cv2.HOGDescriptor() |
7 | hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) | 7 | hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) |
8 | 8 | ||
9 | cv2.startWindowThread() | 9 | cv2.startWindowThread() |
10 | -fname = "./croppedimg/" | 10 | +fname = "./croppedimg/human/" |
11 | # open webcam video stream | 11 | # open webcam video stream |
12 | cap = cv2.VideoCapture(0) | 12 | cap = cv2.VideoCapture(0) |
13 | i = 0 | 13 | i = 0 |
14 | +def draw_left_path(img,x,y,w,h): | ||
15 | + start_point = x+w | ||
16 | + cv2.line(img, (320-2*w,480), (start_point, y+int(h)), (255,0,0), 8) #4픽셀 선 그리기 | ||
17 | + cv2.line(img, (start_point, y+int(h)), (start_point+int(w/5),y+int(h-20)), (255, 0, 0), 10) | ||
18 | + start_point = x+2*w | ||
19 | + cv2.line(img, (540,480), (start_point, y+int(h)), (255,0,0), 8) #4픽셀 선 그리기 | ||
20 | + # cv2.line(img, (start_point, y+int(h)), (start_point+int(w/5),y+int(h-20)), (255, 0, 0), 10) | ||
21 | + return img | ||
22 | +def draw_right_path(img, x, y, w, h): | ||
23 | + start_point = x | ||
24 | + cv2.line(img, (320+2*w, 480), (start_point, y+int(h)), (255, 0, 0), 8) # 8픽셀 선 그리기 | ||
25 | + cv2.line(img, (start_point, y+int(h)), (start_point - int(w/5), y+int(h-20)), (255, 0, 0), 8) | ||
26 | + start_point = x-2*w | ||
27 | + cv2.line(img, (100, 480), (start_point, y + int(h)), (255, 0, 0), 8) # 8픽셀 선 그리기 | ||
28 | + # cv2.line(img, (start_point, y + int(h)), (start_point - int(w / 5), y + int(h-20)), (255, 0, 0), 8) | ||
29 | + return img | ||
14 | while (True): | 30 | while (True): |
15 | # Capture frame-by-frame | 31 | # Capture frame-by-frame |
16 | start = time.time() | 32 | start = time.time() |
17 | ret, frame = cap.read() | 33 | ret, frame = cap.read() |
18 | # resizing for faster detection[240,160] [320 * 240] | 34 | # resizing for faster detection[240,160] [320 * 240] |
19 | - frame = cv2.resize(frame, (240, 160)) | 35 | + frame = cv2.resize(frame, (640, 480)) |
20 | # using a greyscale picture, also for faster detection | 36 | # using a greyscale picture, also for faster detection |
21 | gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) | 37 | gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) |
22 | 38 | ||
23 | # detect people in the image | 39 | # detect people in the image |
24 | # returns the bounding boxes for the detected objects | 40 | # returns the bounding boxes for the detected objects |
25 | boxes, weights = hog.detectMultiScale(frame, winStride=(8, 8)) | 41 | boxes, weights = hog.detectMultiScale(frame, winStride=(8, 8)) |
26 | - | 42 | + detectCount = 0 |
27 | boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes]) | 43 | boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes]) |
28 | for (xA, yA, xB, yB) in boxes: | 44 | for (xA, yA, xB, yB) in boxes: |
29 | # display the detected boxes in the colour picture | 45 | # display the detected boxes in the colour picture |
30 | cv2.rectangle(frame, (xA, yA), (xB, yB), | 46 | cv2.rectangle(frame, (xA, yA), (xB, yB), |
31 | (0, 255, 0), 2) | 47 | (0, 255, 0), 2) |
48 | + cv2.putText(frame, "Detect", (xA - 50, yA - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1) | ||
49 | + detectCount = detectCount+1 | ||
50 | + if(detectCount>1): | ||
51 | + print("Waiting...") | ||
52 | + else : | ||
32 | if(i%10 == 0): | 53 | if(i%10 == 0): |
33 | cropped = frame[yA:yB,xA:xB] | 54 | cropped = frame[yA:yB,xA:xB] |
55 | + #print("xA : {0}, xB : {1}, yA : {2}, yB : {3}".format(xA, xB,yA,yB)) # Print Width, Height of Cropped Area | ||
56 | + i=0 | ||
57 | + if(xB < 380 and xA<260): | ||
58 | + print("Left Side Detect.") | ||
59 | + try: | ||
60 | + frame = draw_left_path(frame, xA,yA,xB-xA,yB-yA) | ||
61 | + except: | ||
62 | + pass | ||
63 | + elif(xA>260 and xB>380): | ||
64 | + print("Right Side Detect") | ||
65 | + try: | ||
66 | + frame = draw_right_path(frame, xA, yA, xB - xA, yB - yA) | ||
67 | + except: | ||
68 | + pass | ||
69 | + else: | ||
70 | + try: | ||
71 | + frame = draw_right_path(frame, xA, yA, xB - xA, yB - yA) | ||
72 | + except: | ||
73 | + pass | ||
74 | + print("Center Side Detect") | ||
34 | s = fname + str(i)+'.jpg' | 75 | s = fname + str(i)+'.jpg' |
35 | cv2.imwrite(s, cropped) # IMG File Write | 76 | cv2.imwrite(s, cropped) # IMG File Write |
36 | print("time :", time.time() - start) | 77 | print("time :", time.time() - start) |
37 | - print("Human Detect!") #Alert | ||
38 | - if(i > 200): | ||
39 | - i=0 | ||
40 | i= i+1 | 78 | i= i+1 |
41 | # Display the resulting frame | 79 | # Display the resulting frame |
42 | 80 | ... | ... |
-
Please register or login to post a comment