-
Notifications
You must be signed in to change notification settings - Fork 7
/
collect.py
102 lines (77 loc) · 2.54 KB
/
collect.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from joystick import Joystick
from car import Car
from picamera.array import PiRGBArray
from picamera import PiCamera
import threading
import time
import cv2
map = None
class Camera(threading.Thread):
global map
def __init__(self, *args, **kwargs):
super(Camera, self).__init__(*args, **kwargs)
self.__flag = threading.Event() # resume/pause thread event
self.__running = threading.Event() # start/stop thread event
self.number = 0
self.camera = PiCamera()
self.camera.resolution = (320, 160)
self.camera.framerate = 10
self.camera.rotation = 180
self.rawCapture = PiRGBArray(self.camera, size=(320, 160))
self.map = map
time.sleep(1)
print('thread ok!!!')
def run(self):
self.__running.set() # set True
with open('content.csv', 'w') as f:
f.write('image,steer\n')
for frame in self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True):
if self.__running.isSet():
# True return
# False block
self.__flag.wait()
if not self.__running.isSet(): # double check
break
self.number += 1
path = 'IMG/%06d.jpg' % self.number
line = '%s,%f' % (path, map.rx)
cv2.imwrite(path, frame.array)
f.write(line + '\n')
print(line)
self.rawCapture.truncate(0) # clear buffer
def pause(self):
self.__flag.clear() # set False
def resume(self):
self.__flag.set() # set True
def stop(self):
self.__flag.set() # resume from pause
self.__running.clear()
def running(self):
return self.__flag.isSet()
if __name__ == '__main__':
js = Joystick()
car = Car()
camera = Camera()
camera.start()
try:
while True:
map = js.run()
x = map.rx
car.turn(x)
# y = map.ly
# car.turn(y)
if map.menu == 1:
if camera.running():
car.gogo(0)
camera.pause()
print('camera pause')
else:
car.gogo(-2000)
camera.resume()
print('camera resume')
except KeyboardInterrupt:
js.stop()
car.stop()
camera.stop()
time.sleep(1)
print('Exit...')