-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_raw_data.py
80 lines (63 loc) · 1.64 KB
/
collect_raw_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 numpy as np
import cv2
import time
import os
import can
import signal
import sys
bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
cap = cv2.VideoCapture(0)
cap.set(3,300) # set height
cap.set(4,200) # set width
raw_data = []
def signal_handler(signal, frame):
name = str(time.time())
file_name = "/raw_data/" + name + ".npy"
np.save(file_name,raw_data)
print("saved {} frame, name : {}".format(len(raw_data),name))
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def get_frame():
# read the frame from webcam
_, frame = cap.read()
# change the color to gray
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# change the resolution (256*141), later cut the sky
frame = cv2.resize(frame,(0,0),fx=0.8, fy=0.8)
return frame
def get_angle():
while(True):
# get one can data
can_data = str(bus.recv())
# check it is steering angle, if yes then stop the loop
if(can_data.find("ID: 0025") > 0):
break
return can_data
def main():
global raw_data
# discard first 40 frames to give time for webcam to get the proper exposure
for i in range(40):
get_angle()
get_frame()
print(i, "discarded")
while True:
angle = get_angle()
frame = get_frame()
raw_data.append([frame, angle])
if len(raw_data) % 1000 == 0:
print(len(raw_data))
'''
if len(raw_data) == 5000:
pid = os.fork()
if pid == 0:
name = str(time.time())
file_name = "/raw_data/" + name + ".npy"
np.save(file_name,raw_data)
print("saved {} frame, name : {}".format(len(raw_data),name))
exit(0)
else:
raw_data = []
'''
main()
cap.release()
# cv2.destroyAllWindows()