forked from Timthony/self_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zth_collect_data.py
138 lines (123 loc) · 4.89 KB
/
zth_collect_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 收集数据,赛道照片和对应的前、后、左、右、4停
# 对应图片和相应的标签值
import io
import zth_car_control
import os
os.environ['SDL_VIDEODRIVE'] = 'x11'
import pygame # 检测模块
from time import ctime,sleep,time
import threading
import numpy as np
import picamera
import picamera.array
global train_labels, train_img, is_capture_running, key
class SplitFrames(object):
def __init__(self):
self.frame_num = 0
self.output = None
# 处理图像的函数write
# 对视频拍摄的每一帧进行处理,构造一个自定义输出类,每拍摄一帧都会进来write处理
def write(self, buf):
global key
if buf.startswith(b'\xff\xd8'): # 代表一个JPG图片的开始,新照片的开头
# Start of new frame; close the old one (if any) and
# open a new output
if self.output:
self.output.close()
self.frame_num += 1
self.output = io.open('%s_image%s.jpg' % (key,time()), 'wb') # 改变格式为jpg
self.output.write(buf)
def pi_capture():
global train_img, is_capture_running,train_labels,key
#init the train_label array
print("Start capture")
is_capture_running = True
with picamera.PiCamera(resolution=(160, 120), framerate=30) as camera:
# 根据摄像头实际情况判断是否要加这句上下翻转
# camera.vflip = True
# Give the camera some warm-up time
sleep(2)
output = SplitFrames()
start = time()
camera.start_recording(output, format='mjpeg')
camera.wait_recording(120)
camera.stop_recording()
finish = time()
print('Captured %d frames at %.2ffps' % (
output.frame_num,
output.frame_num / (finish - start)))
print("quit pi capture")
is_capture_running = False
def my_car_control():
global is_capture_running, key
key = 4
pygame.init()
pygame.display.set_mode((1,1)) # 窗口
zth_car_control.car_stop()
sleep(0.1)
print("Start control!")
while is_capture_running:
# get input from human driver
#
for event in pygame.event.get():
# 判断事件是不是按键按下的事件
if event.type == pygame.KEYDOWN:
key_input = pygame.key.get_pressed() # 可以同时检测多个按键
print(key_input[pygame.K_w], key_input[pygame.K_a], key_input[pygame.K_d])
# 按下前进,保存图片以2开头
if key_input[pygame.K_w] and not key_input[pygame.K_a] and not key_input[pygame.K_d]:
print("Forward")
key = 2
zth_car_control.car_move_forward()
# 按下左键,保存图片以0开头
elif key_input[pygame.K_a]:
print("Left")
zth_car_control.car_turn_left()
sleep(0.1)
key = 0
# 按下d右键,保存图片以1开头
elif key_input[pygame.K_d]:
print("Right")
zth_car_control.car_turn_right()
sleep(0.1)
key = 1
# 按下s后退键,保存图片为3开头
elif key_input[pygame.K_s]:
print("Backward")
zth_car_control.car_move_backward()
key = 3
# 按下k停止键,停止
elif key_input[pygame.K_k]:
zth_car_control.car_stop()
# 检测按键是不是抬起
elif event.type == pygame.KEYUP:
key_input = pygame.key.get_pressed()
# w键抬起,轮子回正
if key_input[pygame.K_w] and not key_input[pygame.K_a] and not key_input[pygame.K_d]:
print("Forward")
key = 2
zth_car_control.car_turn_straight()
zth_car_control.car_move_forward()
# s键抬起
elif key_input[pygame.K_s] and not key_input[pygame.K_a] and not key_input[pygame.K_d]:
print("Backward")
key = 3
zth_car_control.car_move_backward()
else:
print("Stop")
zth_car_control.car_stop()
#car_control.cleanGPIO()
zth_car_control.clean_GPIO()
if __name__ == '__main__':
global train_labels, train_img, key
print("capture thread")
print('-' * 50)
capture_thread = threading.Thread(target=pi_capture,args=()) # 开启线程
capture_thread.setDaemon(True)
capture_thread.start()
my_car_control()
while is_capture_running:
pass
print("Done!")
zth_car_control.car_stop()
zth_car_control.clean_GPIO()