/
record_data.py
80 lines (70 loc) · 2.17 KB
/
record_data.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
71
72
73
74
75
76
77
78
79
80
# - import python library
import os
import cv2
import time
from color_code.color import *
# - variables
VIDEO = 0
DATASET = "dataset/"
TERM_SIZE = os.get_terminal_size().columns
# - capture img program
def record_data(vehicle):
index = 1
print(
f"{CYA}{BOL}[INFORMT]{RES} ",
f"Capture-img process has been started at:",
"\n",
f"{CYA}{BOL} {RES} ",
f"{time.time()}"
)
# Start video capture
cap = cv2.VideoCapture(VIDEO)
# Verify that the camera is available
if not cap.isOpened():
print(
f"{RED}{BOL}[FAILURE]{RES} ",
f"Can't open camera. Please check the connection!"
)
return
try:
while True:
rst, frame = cap.read()
frame = cv2.flip(frame, -1)
steering = vehicle.get_steering_raw_data()
direction = 0
if (steering > 6500):
direction = 4
elif (steering > 5500):
direction = 3
elif (steering > 4000):
direction = 2
elif (steering < 3000):
direction = 1
if not rst:
print(
f"{RED}{BOL}[FAILURE]{RES} ",
f"Can't read the frame.",
"\n",
f"{RED}{BOL} {RES} ",
f"Please follow steps to solve the problem.",
"\n",
f"{RED}{BOL} {RES} ",
f" - Check camera connection and drivers.",
"\n",
f"{RED}{BOL} {RES} ",
f" - Check is another application using camera."
)
break
cv2.imwrite(f'{DATASET}/frame10_{index:04d}_{direction:04d}.jpg', frame)
index += 1
except Exception as exception:
print(
f"{RED}{BOL}[FAILURE]{RES} ",
f"Unexpected exception has occured.\n",
'-'*TERM_SIZE, "\n",
exception, "\n",
'-'*TERM_SIZE,
)
finally:
cap.release()
cv2.destroyAllWindows()