-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_4.py
122 lines (102 loc) · 2.94 KB
/
10_4.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
import threading
import time
import mycamera
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from gpiozero import DigitalOutputDevice
from gpiozero import PWMOutputDevice
PWMA = PWMOutputDevice(18)
AIN1 = DigitalOutputDevice(22)
AIN2 = DigitalOutputDevice(27)
PWMB = PWMOutputDevice(23)
BIN1 = DigitalOutputDevice(25)
BIN2 = DigitalOutputDevice(24)
def motor_go(speed):
AIN1.value = 0
AIN2.value = 1
PWMA.value = speed
BIN1.value = 0
BIN2.value = 1
PWMB.value = speed
def motor_back(speed):
AIN1.value = 1
AIN2.value = 0
PWMA.value = speed
BIN1.value = 1
BIN2.value = 0
PWMB.value = speed
def motor_left(speed):
AIN1.value = 1
AIN2.value = 0
PWMA.value = 0.0
BIN1.value = 0
BIN2.value = 1
PWMB.value = speed
def motor_right(speed):
AIN1.value = 0
AIN2.value = 1
PWMA.value = speed
BIN1.value = 1
BIN2.value = 0
PWMB.value = 0.0
def motor_stop():
AIN1.value = 0
AIN2.value = 1
PWMA.value = 0.0
BIN1.value = 1
BIN2.value = 0
PWMB.value = 0.0
speedSet = 0.4
def img_preprocess(image):
height, _, _ = image.shape
image = image[int(height/2):,:,:]
image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
image = cv2.resize(image, (200,66))
image = cv2.GaussianBlur(image,(5,5),0)
_,image = cv2.threshold(image,100,255,cv2.THRESH_BINARY_INV)
image = image / 255
return image
camera = mycamera.MyPiCamera(640,480)
def main():
model_path = '/home/pi/AI_CAR/model/lane_navigation_final.h5'
model = load_model(model_path)
carState = "stop"
try:
while True:
keyValue = cv2.waitKey(1)
if keyValue == ord('q') :
break
elif keyValue == 82 :
print("go")
carState = "go"
elif keyValue == 84 :
print("stop")
carState = "stop"
_, image = camera.read()
image = cv2.flip(image,-1)
preprocessed = img_preprocess(image)
cv2.imshow('pre', preprocessed)
X = np.asarray([preprocessed])
steering_angle = int(model.predict(X)[0])
print("predict angle:",steering_angle)
if carState == "go":
if steering_angle >= 70 and steering_angle <= 110:
print("go")
motor_go(speedSet)
elif steering_angle > 111:
print("right")
motor_right(speedSet)
elif steering_angle < 71:
print("left")
motor_left(speedSet)
elif carState == "stop":
motor_stop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()
cv2.destroyAllWindows()
PWMA.value = 0.0
PWMB.value = 0.0