-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
120 lines (86 loc) · 2.83 KB
/
train.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
from threading import Thread
import time
from client_test import run_client, q
import cv2
import os
from io import BytesIO
from PIL import Image
import csv
import shutil
import sys
url = 'http://192.168.43.1:8080/video'
##url = 'http://192.168.43.157:8080/video'
cap = cv2.VideoCapture(url)
img_names = []
images = []
steer = []
throt = []
print('storing image in buffer')
# be stored in the csv file same as the image
def create_test_image(frame, count, steering_angle, throttle):
file = BytesIO()
## print(_)
# image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))# this is the image file to be loaded
# image.save(file, 'png')
frame =Image.fromarray(frame)
frame.save(file, 'jpeg')
file.name = 'centre_%d'%count
img_names.append('training_data/IMG/centre%d.jpeg'%count)
steer.append(steering_angle)
throt.append(throttle)
file.seek(0)
return file
folder_name = 'training_data'
def generate_data():
count = 0
now = time.time()
global folder_name
while True:
_,Frame = cap.read()
Frame = cv2.cvtColor(Frame, cv2.COLOR_BGR2RGB)
steering_angle, throttle , mode = q.get()
images.append(create_test_image(Frame, count, steering_angle, throttle))
count += 1
if mode == 1:
break
future = time.time()
print('Image FPS is:{}'.format(count/(future-now)))
print('all frames are now stored in buffer')
if not os.path.exists(folder_name):
os.mkdir(folder_name)
os.mkdir(folder_name+'/IMG')
print('no directory training_data present so creating one!!')##
else:
## shutil.rmtree(folder_name)
## print('deleting training_data and creating empty one!!')
dirs = os.listdir()
while 1:
if folder_name in dirs:
try:
folder_name = folder_name[:-1]+str(int(folder_name[-1])+1)
except:
folder_name = folder_name + str(1)
else:
break
os.mkdir(folder_name)
os.mkdir(folder_name+'/IMG')
print('retriving frames from the buffer to folder:{}'.format(folder_name))
# this runs after completion of while loop
for image in images:
index = images.index(image)
img = Image.open(image)
img.save('%s/IMG/centre_%d.jpeg'%(folder_name, index))
image.close()
with open('%s/driving_log.csv'%('training_data'), 'w+') as csvfile:
writer = csv.writer(csvfile)
for a, b,c in zip(img_names,steer, throt ):
data = [a,b,c]
writer.writerow(data)
print('process completed')
sys.exit()
thread1 = Thread(target=run_client)
thread2 = Thread(target = generate_data)
thread1.start()
thread2.start()
thread1.join()
thread2.join()