-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive_test.py
144 lines (121 loc) · 4.42 KB
/
drive_test.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
import detect
import argparse
import base64
import time
import socket
import cv2
import numpy as np
from io import BytesIO
import utils
import tensorflow
from keras.models import load_model
from PIL import Image
from numpy import interp
import sys
from gps_utils import rdp
import mplleaflet
import sqlite3 as sql
import os
import matplotlib.pyplot as plt
##url = 'http://192.168.43.1:8080/video'
url = 'http://192.168.43.1:8080/video'
bufferSize = 1024
UDP_IP = '192.168.43.157' # Vehicle IP address
UDP_PORT = 3000 # This port match the ones using on other scripts
cap = cv2.VideoCapture(url)
model = None
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(0.001)
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# this triggers the driving mode on the server
sock.sendto(bytes('drive', 'utf-8'), (UDP_IP,UDP_PORT ))
def recieve_data():
## print('data recieved from the server:{}'.format(sock.recvfrom(bufferSize)))
try:
m = sock.recvfrom(bufferSize)[0].decode("utf-8")
print(m)
except:
pass
def send_control(values):
values = [values[0], 1020-int(interp(round(values[1], 4), [-1,1], [0,1020]))]
sock.sendto(bytes(str(values), 'utf-8'), (UDP_IP,UDP_PORT ))
print('data sent to the vehicle:{}'.format(values))
try:
recieve_data()
except:
pass
## print('data recieved from the server:{}'.format(sock.recvfrom(bufferSize)[0]))
def put_text(frame,steering_angle):
return cv2.putText(frame,'Throttle:-1, Steering angle:'+str(round(steering_angle, 4)),(10,310), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(200,255,155),1 )
def predict_steer():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
steering_angle = 0
while True:
_,Frame = cap.read()
# gets data in the form of pandas dataframe of image , throttle and steering angle
if _:
frame = Frame.copy()
frame = put_text(frame, steering_angle)
# this is to detect the pedrestrians
cv2.imshow('image', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
stop_all()
Frame = cv2.cvtColor(Frame, cv2.COLOR_BGR2RGB)
# image = Image.open(BytesIO(base64.b64decode(Frame)))
image = Frame
try:
image = np.asarray(image)
image = utils.preprocess(image)
image = np.array([image])
steering_angle = float(model.predict(image ,batch_size = 1))
throttle = 0 # set throttle to maximum in our test case where our speed and mass is not high enough to create large Inertia
send_control([throttle, steering_angle])
except Exception as e:
print(e)
else:
print('no frames recieved!\nplease make sure the video server is running!!')
stop_all()
def stop_all():
dat = str([511,511])
sock.sendto(bytes(dat, 'utf-8'), (UDP_IP,UDP_PORT ))
sock.sendto(bytes('disconnect', 'utf-8'), (UDP_IP, UDP_PORT))
sock.close()
sys.exit("pressed home button. Shutting down!!")
def generate_map():
conn = sql.connect("geocoordinates1.db")
c = conn.cursor()
x = c.execute("SELECT lon,lat FROM GeoCoordinates ")
list_data = x.fetchall()
x = []
y = []
for data in list_data:
x.append(data[0])
y.append(data[1])
list_data = np.array(list_data)
plt.plot(list_data[:,0],list_data[:,1])
list_data = rdp(list_data, epsilon = 1e-4)
fig = plt.figure()
plt.plot(list_data[:, 0], list_data[:, 1], "r*")
mplleaflet.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Remote Driving')
parser.add_argument(
'model',
type=str,
help='Path to model h5 file. Model should be on the same path.'
)
parser.add_argument(
'image_folder',
type=str,
nargs='?',
default='',
help='Path to image folder. This is where the images from the run will be saved.'
)
args = parser.parse_args()
model = load_model(args.model)
try:
generate_map()
predict_steer()
except KeyboardInterrupt:
stop_all()