-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaptop_server.py.py
167 lines (140 loc) · 5.85 KB
/
laptop_server.py.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
160
161
162
163
164
165
166
167
__author__ = 'zhengwang'
import numpy as np
import cv2
import serial
import pygame
from pygame.locals import *
import socket
import time
import os
#from time import *
from struct import *
class CollectTrainingData(object):
def __init__(self):
# accept a single connection
self.server_socket = socket.socket()
self.server_socket.bind(('0.0.0.0', 8000))
self.server_socket.listen(0)
self.connection = self.server_socket.accept()[0].makefile('rb')
# connect to a seral port
# self.ser = serial.Serial('/dev/tty.usbmodem1421', 115200, timeout=1)
self.send_inst = True
# 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')
pygame.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')
# stream video frames one by one
try:
stream_bytes = ' '
frame = 1
direction=1
move="Forward"
s = socket.socket()
host = "192.168.43.223"
port = 8001
s.connect((host,port))
print("connection established")
# while self.send_inst:
while (frame < 200):
stream_bytes += self.connection.read(1024)
first = stream_bytes.find('\xff\xd8')
last = stream_bytes.find('\xff\xd9')
if first != -1 and last != -1:
jpg = stream_bytes[first:last + 2]
stream_bytes = stream_bytes[last + 2:]
image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
# select lower half of the image
roi = image[120:240, :]
# save streamed images
#cv2.imwrite('training_images/frame{:>05}_'+move+'.jpg'.format(frame), image)
cv2.imwrite('training_images/frame{:>05}.jpg'.format(frame), image)
#nameim='frame{:>05}'+ str(direction)
#cv2.imwrite('training_images/'+nameim+'.jpg'.format(frame), image)
#cv2.imshow('roi_image', roi)
cv2.imshow('image', image)
# reshape the roi image into one row array
temp_array = roi.reshape(1, 38400).astype(np.float32)
frame += 1
total_frame += 1
print(frame)
#inpt = raw_input("Enter the Direction : ")
#if inpt == '':
# inpt = 1
#num = int(inpt)
num = int(raw_input("Enter the Direction : ") or "1")
val = pack('!i', num)
s.send(val)
direction = num
# simple orders
if direction == 1:
move="Forward"
print(move)
saved_frame += 1
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[2]))
#self.ser.write(chr(1))
elif direction == 2:
move="Reverse"
print(move)
saved_frame += 1
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[3]))
#self.ser.write(chr(2))
elif direction == 3:
move="Left"
print(move)
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[0]))
saved_frame += 1
#self.ser.write(chr(3))
elif direction == 4:
move="Right"
print(move)
image_array = np.vstack((image_array, temp_array))
label_array = np.vstack((label_array, self.k[1]))
saved_frame += 1
#self.ser.write(chr(4))
else:
print("invalid input Usage:1-Forward 2-Reverse 3-Left 4-Right")
# in_data=raw_input(" Enter the direction > ")
# self.server_socket.send(in_data.encode())
# save training images and labels
print 'training started'
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"
print(directory)
print(file_name)
#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
finally:
self.connection.close()
self.server_socket.close()
if __name__ == '__main__':
CollectTrainingData()