-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainCar.py
143 lines (116 loc) · 4.4 KB
/
trainCar.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
import cv2
import numpy as np
import time
import serial
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
)
def capture():
var=3405
a='5 '
f=open('trainingData/controls.txt','w')
k=53
while var > 0:
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
#line segmentation
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)
k=cv2.waitKey(1)
if k!= -1:
if k == 53 :
ser.write("\x35")
else:
resOutput = cv2.resize(opImage,(40,30),interpolation = cv2.INTER_AREA)
cv2.imwrite('testImages/test'+str(var)+'.jpg',resOutput)
cv2.imwrite('originalImages/original'+str(var)+'.jpg',frame)
var = var+1
if k == 27: ## 27 - ASCII for escape key
break
if k == 52:
ser.write("\x34")
a='4 '
elif k == 56:
a='8 '
ser.write("\x38")
elif k == 50:
a='2 '
ser.write("\x32")
elif k == 54:
a='6 '
ser.write("\x36")
f.write(a)
#wait for the two threads to complete
if __name__=="__main__":
capture()
cam.release()
cv2.destroyAllWindows()