-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_on_raspberry pi.py
137 lines (113 loc) · 3.43 KB
/
run_on_raspberry pi.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
import cv2
import RPi.GPIO as GPIO
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
import time
PWMA = 18
AIN1 = 22
AIN2 = 27
PWMB = 23
BIN1 = 25
BIN2 = 24
def motor_back(speed):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,True) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,True) #BIN1
def motor_go(speed):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,True)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,True)#BIN2
GPIO.output(BIN1,False) #BIN1
def motor_stop():
L_Motor.ChangeDutyCycle(0)
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(0)
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,False) #BIN1
def motor_right(speed):
L_Motor.ChangeDutyCycle(speed*1.5)
GPIO.output(AIN2,True)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(speed/2)#
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,True) #BIN1
def motor_left(speed):
L_Motor.ChangeDutyCycle(speed/2)#
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,True) #AIN1
R_Motor.ChangeDutyCycle(speed*1.5)
GPIO.output(BIN2,True)#BIN2
GPIO.output(BIN1,False) #BIN1
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(AIN2,GPIO.OUT)
GPIO.setup(AIN1,GPIO.OUT)
GPIO.setup(PWMA,GPIO.OUT)
GPIO.setup(BIN1,GPIO.OUT)
GPIO.setup(BIN2,GPIO.OUT)
GPIO.setup(PWMB,GPIO.OUT)
L_Motor= GPIO.PWM(PWMA,500)
L_Motor.start(0)
R_Motor = GPIO.PWM(PWMB,500)
R_Motor.start(0)
speedSet = 20
def img_preprocess(image):
height, _, _ = image.shape
image = image[int(height/2):,:,:]
image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
image = cv2.GaussianBlur(image, (3,3), 0)
image = cv2.resize(image, (100,33))
image = image / 255
return image
def main():
camera = cv2.VideoCapture(-1)
camera.set(3, 640)
camera.set(4, 480)
model_path = '/home/pi/AI_CAR/model/lane_navigation_final.h5'
model = load_model(model_path, compile=False)
carState = "stop"
keValue = int(input());
steering_angle = 0
while( camera.isOpened()):
#start = time.time()
#keValue = cv2.waitKey(1)
#if keValue == ord('q') :
# break
if keValue == 82 :
#print("go")
carState = "go"
#elif keValue == 84 :
#print("stop")
# carState = "stop"
_, image = camera.read()
image = cv2.flip(image,-1)
#cv2.imshow('Original', image)
preprocessed = img_preprocess(image)
#cv2.imshow('pre', preprocessed)
X = np.asarray([preprocessed])
steering_angle = int(np.argmax(model.predict(X), axis = -1))
#print("predict angle:",steering_angle)
if carState == "go":
if steering_angle == 1:
#print("go")
motor_go(speedSet)
elif steering_angle == 2:
#print("right")
motor_right(speedSet)
elif steering_angle == 0:
#print("left")
motor_left(speedSet)
#elif carState == "stop":
# motor_stop()
#print("time:", time.time() - start)
#cv2.destroyAllWindows()
if __name__ == '__main__':
main()
GPIO.cleanup()