-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
159 lines (124 loc) · 4.96 KB
/
camera.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
import pygame
import os
import signal
import motors
import sys
class CollectTrainingData(object):
def __init__(self):
# create labels
self.k = np.zeros((4, 4), 'float')
for i in range(4):
self.k[i, i] = 1
self.temp_label = np.zeros((1, 4), 'float')
os.environ["SDL_VIDEODRIVER"] = "dummy"
signal.signal(signal.SIGINT, signal.default_int_handler)
pygame.init()
os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()
pygame.joystick.quit()
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
if joystick_count < 1:
print("No joysticks found.")
sys.exit()
print("Found " + str(joystick_count) + " joysticks.")
joy = pygame.joystick.Joystick(0)
joy.init()
self.collect_image()
def collect_image(self):
saved_frame = 0
total_frame = 0
# collect images for training
print("Start collecting images...")
e1 = cv2.getTickCount()
image_array = np.zeros((1, 38400))
label_array = np.zeros((1, 4), 'float')
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 20
raw_capture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(2)
# capture frames from the camera
for frame in camera.capture_continuous(raw_capture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# select lower half of the image
roi = image[120:240, :]
# reshape the roi image into one row array
temp_array = roi.reshape(1, 38400).astype(np.float32)
cv2.imwrite('training_images/frame{:>05}.jpg'.format(frame), image)
frame += 1
total_frame += 1
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# get input from human driver
# complex orders
if joy.get_hat(0) == (1, 1):
print("Forward Right")
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[1]))
saved_frame += 1
# motor forward right
elif joy.get_hat(0) == (1, -1):
print("Forward Left")
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[0]))
saved_frame += 1
elif joy.get_hat(0) == (-1, 1):
print("Reverse Right")
elif joy.get_hat(0) == (-1, -1):
print("Reverse Left")
# simple orders
elif joy.get_hat(0) == (1, 0):
print("Forward")
saved_frame += 1
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[2]))
elif joy.get_hat(0) == (-1, 0):
print("Reverse")
saved_frame += 1
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[3]))
elif joy.get_hat(0) == (0, 1):
print("Right")
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[1]))
saved_frame += 1
elif joy.get_hat(0) == (0, -1):
print("Left")
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[0]))
saved_frame += 1
elif key_input[pygame.K_x] or key_input[pygame.K_q]:
print("exit")
# save training images and labels
train = image_array[1:, :]
train_labels = label_array[1:, :]
# save training data as a numpy file
file_name = str(int(time.time()))
directory = "training_data"
if not os.path.exists(directory):
os.makedirs(directory)
try:
np.savez(directory + '/' + file_name + '.npz', train=train, train_labels=train_labels)
except IOError as e:
print(e)
e2 = cv2.getTickCount()
# calculate streaming duration
time0 = (e2 - e1) / cv2.getTickFrequency()
print("Streaming duration:", time0)
print(train.shape)
print(train_labels.shape)
print("Total frame:", total_frame)
print("Saved frame:", saved_frame)
print("Dropped frame", total_frame - saved_frame)