-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
203 lines (165 loc) · 5.87 KB
/
main.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
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
# -*- coding: utf-8 -*-
import logging
from YOLO_small_tf import YOLO_TF
from api_caller import APICaller
from image_func import get_img
import move_enum
logging.basicConfig(filename='app.log', level=logging.DEBUG)
api = APICaller()
yolo = YOLO_TF()
yolo.imshow = False
bottle_class = 'bottle'
degres = 120
def calculate_percentage(x_start, x_stop, img_w):
middle = ((x_stop - x_start) / 2) + x_start
logging.debug('Percentage: ' + str(middle / img_w))
return middle / img_w
def calculate_left_deg(percentage):
degres_half = degres / 2
percentage_calculation = 1.0 - (percentage * 2)
logging.debug('calculate_left_deg: ' + str(degres_half * percentage_calculation))
return degres_half * percentage_calculation
def calculate_right_deg(percentage):
degres_half = degres / 2
percentage = percentage - 0.50
logging.debug('calculate_right_deg: ' + str(degres_half * (percentage * 2)))
return degres_half * (percentage * 2)
def process_bottle(data):
"""
param:
data: [1619.5781, 2630.89, 1888.886, 2985.486]
[X start, X stop, Y - start, Y - stop]
output of yolo detector without class and percentage
"""
img_w = yolo.w_img
img_h = yolo.h_img
logging.debug('result (process_bottle): ')
logging.debug(data)
percentage = calculate_percentage(data[0], data[1], img_w)
print('Percentage: ' + str(percentage))
deviation = 0.07
if 0.50 + deviation >= percentage >= 0.50 - deviation:
print('Forward pass')
return api.move_forward(30)
elif percentage > 0.50:
print('Right rotate: ' + str(calculate_right_deg(percentage)))
api.turn_right(calculate_right_deg(percentage))
return False
else:
print('Left rotate: ' + str(calculate_left_deg(percentage)))
api.turn_left(calculate_left_deg(percentage))
return False
return False
def filter_results(results):
result = []
for res in results:
if res[0] == bottle_class:
result.append(res)
return result
def detect(filename):
import time
s = time.time()
yolo.detect_from_file(filename)
print('Detection time: ' + str(time.time()-s))
result = filter_results(yolo.result)
print('result')
print(result)
"""
RESULT:
[['bottle', 1619.5781, 2630.89, 1888.886, 2985.486, 0.7833293080329895]]
"""
if len(result) > 1:
print('Multiple results !!!')
return;
if len(result) == 0:
print('Oh f*ck ... there is no bottle')
return;
process_bottle(result[0][1:-1])
def navigate_to_bottle(result):
logging.debug('inside navigate_to_bottle func')
logging.debug('result of detection: ')
logging.debug(result)
process_result = process_bottle(result[0][1:-1])
if process_result != move_enum.FORWARD:
api.move_forward(500)
raise ValueError('Sorry, model hasn´t been trained pretty well, we cannot continue ...')
while True:
img = get_img()
yolo.detect_from_cvmat(img)
vision_result = filter_results(yolo.result)
logging.debug('result of detection: ')
logging.debug(vision_result)
if len(vision_result) == 0: # no catch
result = api.move_forward(20)
if result:
api.pick_up()
break
else:
logging.debug('I can see a bottle')
result = process_bottle(vision_result[0][1:-1])
if result:
api.pick_up()
break
def ultimate_finding_cycle():
print('Running ...')
num_of_turns = 0
while True:
img = get_img()
yolo.detect_from_cvmat(img)
result = filter_results(yolo.result)
if len(result) == 1:
print('Woooow, I got an image')
logging.debug('Woooow, I got an image')
navigate_to_bottle(result)
break
else:
for i in range(2):
api.turn_right(10)
img = get_img()
yolo.detect_from_cvmat(img)
result = filter_results(yolo.result)
if len(result) == 1:
print('Woooow, I got an image')
logging.debug('Woooow, I got an image')
navigate_to_bottle(result)
break
api.move_forward(10)
img = get_img()
yolo.detect_from_cvmat(img)
result = filter_results(yolo.result)
if len(result) == 1:
print('Woooow, I got an image')
logging.debug('Woooow, I got an image')
navigate_to_bottle(result)
break
for i in range(4):
api.turn_left(10)
img = get_img()
yolo.detect_from_cvmat(img)
result = filter_results(yolo.result)
if len(result) == 1:
print('Woooow, I got an image')
logging.debug('Woooow, I got an image')
navigate_to_bottle(result)
break
api.move_forward(10)
img = get_img()
yolo.detect_from_cvmat(img)
result = filter_results(yolo.result)
if len(result) == 1:
print('Woooow, I got an image')
logging.debug('Woooow, I got an image')
navigate_to_bottle(result)
#O num_of_turns += 1
# logging.debug('ultimate_finding_cycle can´t see anythink')
# if num_of_turns < 4:
# api.turn_right(10)
# else:
# api.turn_left(10)
# if num_of_turns == 6:
# num_of_turns = 0
#api.move_forward(1)
def main():
ultimate_finding_cycle()
if __name__ == '__main__':
main()