-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain3.py
More file actions
250 lines (205 loc) · 7.69 KB
/
main3.py
File metadata and controls
250 lines (205 loc) · 7.69 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
'''
This code is written by vishnu deo gupta. It is for NSSC challenge.
'''
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpim
import serial
import bluetooth
#Threshold for the given image..
thres1 = 80
thres2 = 100
#Threshold for the distance before we want to break
#Check the value during competetion.
distance_thresh = 20
#Threshold of angle, it is the most crucial part to set these threshold right
#the current value is set arbitraly, because i don't have arena to test.
angle_thresh1 = 6
angle_thresh2 = 6
## set the path of video here eg video.mp4
FILE_PATH = 0
#Starting communication with ardino
#ser = serial.Serial(port = 'COM3', baudrate = 9600, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, timeout = 1)
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
#thres1 = 100
#thres2 = 200
def remove_duplicate(l): #takes the input as a list
l = list(set(l))
return l
'''
def approximate(l): #approximate the list
for i in range(l):
for j in range(l[0]):
return l
'''
#img = cv2.imread('perfect1.jpg')
def detect_edge(img):
processed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#cv2.imwrite('.jpeg', processed_img)
#cv2.imshow('gray_image', processed_img)
processed_img = cv2.Canny(processed_img, threshold1=thres1, threshold2=thres2)
processed_img = cv2.GaussianBlur(processed_img,(5, 5), 0)
return processed_img
def detect_bot(img):
processed_img = detect_edge(img)
#_, thresh = cv2.threshold(processed_img, thresh1,thresh2,0)
(im2, conts, _) = cv2.findContours(processed_img.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cx = int(M['m10']/M['m00'])
#cy = int(M['m01']/M['m00'])
for c in conts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * perimeter, True)
if(len(approx) == 4):
M = cv2.moments(c)
print('Bot Found')
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#print(cx, cy)
return (cx, cy)
def detect_planet(img):
processed_img = detect_edge(img)
#_, thresh = cv2.threshold(processed_img, thresh1,thresh2,0)
(im2, conts, _) = cv2.findContours(processed_img.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cx = int(M['m10']/M['m00'])
#cy = int(M['m01']/M['m00'])
centroids = []
for c in conts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * perimeter, True)
if(len(approx) == 4):
pass
else:
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
centroids.append((cx,cy))
centroids = remove_duplicate(centroids)
print(centroids)
#print(centroids[0][0])
return centroids #list of tuples of all the centroids...
def euclidian_distance(x1, y1, x2, y2):
return (np.sqrt((x1-x2)**2 + (y1-y2)**2))
#calculating distances of all the planet from bot and storing in a list which is sorted
def distances(x, y, l):
distance = []
for i in l:
distance.append(euclidian_distance(x,y,i[0],i[1]))
distance.sort()
return distance
#center_of_planets = detect_planet(img)
#bot_center_x, bot_center_y = detect_bot(img)
#print(distances(bot_center_x, bot_center_y, center_of_planets))
#visited = [0 for _ in range(8)]
#print(visited)
#(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
def calc_angle(img):
processed_img = detect_edge(img)
#_, thresh = cv2.threshold(processed_img, thresh1,thresh2,0)
(im2, conts, _) = cv2.findContours(processed_img.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cx = int(M['m10']/M['m00'])
#cy = int(M['m01']/M['m00'])
for c in conts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * perimeter, True)
if(len(approx) == 4):
(x,y),(MA,ma),angle = cv2.fitEllipse(c)
#print(angle)
#print(cx, cy)
#angle = angle * np.pi / 180.0
return (angle)
def get_angle(img):
return (calc_angle(img)* np.pi / 180.0)
def desired_angle(x1,y1,x2,y2):
return np.arctan((y2-y1)/(x2-x1))
##def sum_vis(l):
## s = 0
## for i in l:
## s += i
## return s
def remove_close_value(l):
d = []
for i in range(len(l)):
if i == 0:
if l[i][0] > l[i+1][0] - 5 and l[i][0] < l[i+1][0] + 5:
d.append(l[i])
elif l[i][1] > l[i+1][1] - 5 and l[i][1] < l[i+1][1] + 5:
d.append(l[i])
elif i == len(l):
if l[i][0] > l[i-1][0] - 5 and l[i][0] < l[i-1][0] + 5:
d.append(l[i])
elif l[i][1] > l[i-1][1] - 5 and l[i][1] < l[i-1][1] + 5:
d.append(l[i])
else:
if ((l[i][0] > l[i+1][0] - 5 and l[i][0] < l[i+1][0] + 5) or (l[i][0] > l[i-1][0] - 5 and l[i][0] < l[i-1][0] + 5)):
d.append(l[i])
elif ((l[i][0] > l[i+1][0] - 5 and l[i][0] < l[i+1][0] + 5) or (l[i][0] > l[i-1][0] - 5 and l[i][0] < l[i-1][0] + 5)):
d.append(l[i])
return d
#print(get_angle(img))
##center_of_planets = detect_planet(img)
##bot_center_x, bot_center_y = detect_bot(img)
##distance = distances(bot_center_x, bot_center_y, center_of_planets)
##visited = [0 for _ in range(len(center_of_planets))]
##The main algorithm...
cap = cv2.VideoCapture(FILE_PATH)
i = 0
while(True):
ret, frame = cap.read()
#cv2.imshow('video', frame)
#getting centers of planets
center_of_planets = detect_planet(frame)
#removing the chance of duplicasy or close value that can come due to faulty edge detection
center_of_planets = remove_close_value(center_of_planets)
#creating a list to track the nth planet
visited = [0 for _ in range(len(center_of_planets))]
#calculating bot centroid
bot_center_x, bot_center_y = detect_bot(frame)
#calculating bot to nearest planet
distance = distances(bot_center_x, bot_center_y, center_of_planets)
#calculating bot orientation
bot_angle = get_angle(frame)
#calculating bot to planet angle
bot_to_planet_angle = desired_angle(bot_center_x, bot_center_y, center_of_planets[i][0], center_of_planets[i][1])
if sum(visited) == len(center_of_planets):
break
else:
while(distance[i] > distance_thresh):
if (bot_angle > bot_to_planet_angle + angle_thresh1):
#give command to turn left
sock.send('l')
elif (bot_angle < bot_to_planet_angle - angle_thresh2):
#give command to turn right
sock.send('r')
else:
sock.send('f')
#give command to go straight
visited[i] = 1
sock.send(i)
i += 1
cv2.imshow('At Arena', frame)
#sock.send(i)
#pass the value of i in arduino
sock.close()
cv2.waitKey(0)
cv2.destroyAllWindows()
#end of the code
'''
for i in range(len(distance)):
while(distance[i]>distance_thresh):
if ((get_angle(img))>left_angle_low_thresh and get_angle(img) < left_angle_high_thresh):
#give turn left command
else if ((get_angle(img))>right_angle_low_thresh and get_angle(img) < right_angle_high_thresh):
#give turn right command
#give forward command
else:
#give stop command
#give the value of i to arduino
#print(bot_center_x, bot_center_y)
#processed_img = detect_edge(img)
#cv2.imshow('image', processed_img)
#cv2.imwrite('processed.jpeg',processed_img)
#plt.figure()
#plt.imshow(gray_image)
#plt.show()
'''