forked from maurehur/Stop-Sign-detection_OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_sign_detection_parse&save_video.py
70 lines (52 loc) · 1.67 KB
/
stop_sign_detection_parse&save_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import numpy as np
def define_region(image):
### black region
if (len(image.shape) == 3):
height, length, _ = image.shape
else:
height, length = image.shape
# Vertices array of the unwanted area --> bottom 1/3 of the picture
region = [np.array([(0, height), (0, height//3), (length, height//3), (length, height)])]
return region
def crop_frame(image):
if (len(image.shape) == 3):
height, length, _ = image.shape
else:
height, length = image.shape
return image[0: height//3*2, 0: length]
def mask_image(image, vertices):
# fill unwanted area with zeros
cv2.fillPoly(image, vertices, 0)
return image
if __name__ == '__main__':
# video path
filepath = 'stop_sign_videos\7.mp4'
# load classifier
stop_sign_cascade = cv2.CascadeClassifier('stop_sign_classifier_2.xml')
cap = cv2.VideoCapture(filepath)
# save frames to video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter('stop_sign_videos\out\6.avi', fourcc, 20, (1080, 1920))
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame', 360, 640)
while (cap.isOpened()):
ret, frame = cap.read()
if (ret):
cropped_frame = crop_frame(frame)
img_filter = cv2.GaussianBlur(cropped_frame, (5, 5), 0)
gray_filered = cv2.cvtColor(img_filter, cv2.COLOR_BGR2GRAY)
stop_signs = stop_sign_cascade.detectMultiScale(gray_filered, scaleFactor=1.05, minNeighbors=15, minSize=(30, 30))
for (x,y,w,h) in stop_signs:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 3)
video.write(frame)
cv2.imshow('frame', frame)
cv2.waitKey(1)
else:
break
k=cv2.waitKey(1) & 0xFF
if k==27:
break
video.release()
cap.release()
cv2.destroyAllWindows()