-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunCar.py
151 lines (122 loc) · 4.4 KB
/
runCar.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
import cv2
import numpy as np
import serial
import scipy.io, scipy.special
cam = cv2.VideoCapture(1)
cam.set(3,160)
cam.set(4,120)
ser = serial.Serial(
port='COM7',
baudrate=9600,
timeout=0,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
weights = scipy.io.loadmat('network.mat')
theta1,theta2 = weights['theta1'], weights['theta2']
def capture():
while(1):
minRed = 127
maxRed = 127
minRedGreenRange = 127
maxRedGreenRange = 127
minRedBlueRange = 127
maxRedBlueRange = 127
minLineLength = 80
maxLineGap = 20
ret,frame = cam.read()
height, width, channels = frame.shape
opImage = np.zeros((height,width,1),np.uint8)
#finding threshols for image segmentation
for i in range(100,height):
for j in range(70,width-70):
blue = frame[i,j,0]
green = frame[i,j,1]
red = frame[i,j,2]
if blue==0 and red==0 and green==0:
continue
if red < minRed:
minRed = red
if red > maxRed:
maxRed = red
redGreenRange = int(red)-int(green)
redBlueRange = int(red)-int(blue)
if redGreenRange < minRedGreenRange:
minRedGreenRange = redGreenRange
if redGreenRange > maxRedGreenRange:
maxRedGreenRange = redGreenRange
if redBlueRange < minRedBlueRange:
minRedBlueRange = redBlueRange
if redBlueRange > maxRedBlueRange:
maxRedBlueRange = redBlueRange
#colour segmentation based on calculated thresholds
for i in range(height):
for j in range(width):
blue = frame[i,j,0]
green = frame[i,j,1]
red = frame[i,j,2]
redGreen = int(red)-int(green)
redBlue = int(red)-int(blue)
if red > minRed and red < maxRed and redGreen > minRedGreenRange and redGreen < maxRedGreenRange and redBlue > minRedBlueRange and redBlue < maxRedBlueRange:
opImage[i,j] = 255
else:
opImage[i,j] = 0
#edge detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, minLineLength, maxLineGap)
if lines != None:
for i in range(len(lines[0])):
points=[0 for j in range(4)]
if lines[0,i,0] < lines[0,i,2]:
points[0] = (lines[0,i,0],0)
points[1] = (lines[0,i,2],0)
points[2] = (lines[0,i,2],lines[0,i,3])
points[3] = (lines[0,i,0],lines[0,i,1])
else:
points[0] = (lines[0,i,2],0)
points[1] = (lines[0,i,0],0)
points[2] = (lines[0,i,0],lines[0,i,1])
points[3] = (lines[0,i,2],lines[0,i,3])
polyPoints = np.array([tuple(i) for i in points],np.int32)
cv2.fillConvexPoly(opImage, polyPoints, [0,0,0])
cv2.imshow('output',opImage)
#cv2.imshow('input',frame)
img = cv2.resize(opImage,(40,30),interpolation = cv2.INTER_AREA)
img = img.flatten()
for j in range(len(img)):
img[j]=(float(img[j])/255)
img =np.array(img,dtype='float32')
tx = predict(img,theta1,theta2)
if tx == 4:
ser.write("\x34")
elif tx == 6:
ser.write("\x36")
elif tx ==8:
ser.write("\x38")
elif tx == 5:
ser.write("\x35")
elif tx == 2:
ser.write("\x32")
print tx
k=cv2.waitKey(1)
if k == 27: ## 27 - ASCII for escape key
break
def sigmoid(p):
return scipy.special.expit(p)
def predict(X,theta1,theta2):
a1 = np.r_[np.ones((1,1)), X.reshape(X.shape[0],1)]
z2 = theta1.dot(a1)
a2 = sigmoid(z2)
a2 = np.r_[np.ones((1,1)), a2]
z3 = theta2.dot(a2)
a3 = sigmoid(z3)
#the indice corresponding to maximum argument will be the prediction
return np.argmax(z3) + 1
def main():
output = capture()
if __name__=="__main__":
main()
cam.release()
cv2.destroyAllWindows()