-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
306 lines (243 loc) · 10.8 KB
/
__init__.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from cv2 import detail_ImageFeatures
from mycroft import MycroftSkill, intent_handler
from mycroft.util import LOG
from mycroft.skills.context import removes_context
from adapt.intent import IntentBuilder
import time
import cv2
import os
import sys
from multiprocessing import Process, Queue
import csv
import json
from .cvAPI import getDetail, getObjLabel
LOGSTR = '********************====================########## '
# mycroft-msm install https://github.com/ISA-SRBP/easy-shopping-skill.git
# 'NO TEST': use the image taken by the camera
# 'TEST': use the image in /photo folder,
# In both mode, camera will work normally, i.e. take the photo, save the photo
# MODE = 'PROD'
MODE = 'TEST'
# need to be changed
IMAGE_STORE_PATH = '/home/ai-user/mycroft-core/skills/easy-shopping-skill.isa-srbp/photo/'
# need to be changed
TEST_IMAGE_PATH_MULTI = '/home/ai-user/mycroft-core/skills/easy-shopping-skill.isa-srbp/testPhoto/multi.jpeg'
# need to be changed
TEST_IMAGE_PATH_HAND = '/home/ai-user/mycroft-core/skills/easy-shopping-skill.isa-srbp/testPhoto/2.jpeg'
def take_photo(img_queue):
'''
Do taking photo
'''
LOG.info(LOGSTR + 'take photo process start')
cap = cv2.VideoCapture(0)
img_name = 'cap_img_' + str(time.time()) + '.jpg'
img_path = IMAGE_STORE_PATH + img_name
#<-- Take photo in specific time duration -->
cout = 0
while True:
ret, frame = cap.read()
cv2.waitKey(1)
cv2.imshow('capture', frame)
cout += 1
if cout == 50:
img_queue.put(img_path)
cv2.imwrite(img_path, frame)
break
cap.release()
cv2.destroyAllWindows()
LOG.info(LOGSTR + 'take photo process end')
os._exit(0)
def generate_str(possible_list):
'''
Generate string for Mycroft to speak it
Args:
possible_list: array list with len = 3, each element is a string
Returns:
a string, e.g. possible_list = ['a', 'b', 'c'], res = 'a, b, and c'
'''
res = ''
if len(possible_list) == 3:
res = possible_list[0] + ' ' + \
possible_list[1] + ' and ' + possible_list[2]
elif len(possible_list) == 2:
res = possible_list[0] + ' and ' + possible_list[1]
elif len(possible_list) == 1:
res = possible_list[0]
return res
class EasyShopping(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
self.category_str = ''
self.color_str = ''
self.brand_str = ''
self.kw_str = ''
self.img_multi = ''
self.img_hand = ''
self.log.info(LOGSTR + "_init_ EasyShoppingSkill")
def initialize(self):
self.reload_skill = False
# ============================ use case 1 ============================
@intent_handler('view.goods.intent')
def handle_view_goods(self, message):
self.speak_dialog('take.photo')
self.img_multi = ''
self.img_hand = ''
#create another process to do the photo taking
img_queue = Queue()
take_photo_process = Process(target=take_photo, args=(img_queue,))
take_photo_process.daemon = True
take_photo_process.start()
take_photo_process.join()
self.img_multi = img_queue.get()
self.speak('I find some goods here, you can ask me whatever goods you want.', expect_response=True)
@intent_handler('is.there.any.goods.intent')
def handle_is_there_any_goods(self, message):
if self.img_multi == '':
self.handle_no_context1(message)
else:
# call cv api, and get result.
try:
self.log.info(LOGSTR + 'actual img path')
self.log.info(self.img_multi)
if MODE == 'TEST':
self.log.info(LOGSTR + 'testing mode, use another image')
self.img_multi = TEST_IMAGE_PATH_MULTI
objectlist = getObjLabel.getObjectsThenLabel(self.img_multi)
label_list = []
loc_list = []
detected = 0
category_label = message.data.get('category')
for obj in objectlist['objectList']:
label_list.append(obj['name'])
loc_list.append(obj['loc'])
if category_label:
for i in range(0,len(label_list)):
label_str = generate_str(label_list[i])
label_str = label_str.lower()
if category_label is not None:
if category_label in label_str:
self.speak_dialog('yes.goods',
{'category': category_label,
'location': loc_list[i]})
detected = 1
break
else:
continue
if detected == 0 and category_label:
self.speak_dialog('no.goods',
{'category': category_label})
except Exception as e:
self.log.error((LOGSTR + "Error: {0}").format(e))
self.speak_dialog(
"exception", {"action": "calling computer vision API"})
def handle_no_context1(self, message):
self.speak('Please let me have a look at what\'s in front of you first.')
take_photo = self.ask_yesno('do.you.want.to.take.a.photo')
if take_photo == 'yes':
self.handle_view_goods(message)
elif take_photo == 'no':
self.speak('OK. I won\'t take photo')
else:
self.speak('I cannot understand what you are saying')
# ============================ use case 2 ============================
def clear_all(self):
self.types_str = ''
self.color_str = ''
self.logo_str = ''
self.kw_str = ''
self.img_hand = ''
self.img_multi = ''
@intent_handler(IntentBuilder('ViewItemInHand').require('ViewItemInHandKeyWord'))
def handle_view_item_in_hand(self, message):
self.speak_dialog('take.photo')
self.img_multi = ''
self.img_hand = ''
# create another process to do the photo taking
img_queue = Queue()
take_photo_process = Process(target=take_photo, args=(img_queue,))
take_photo_process.daemon = True
take_photo_process.start()
take_photo_process.join()
self.img_hand = img_queue.get()
# call cv api, and get result.
try:
self.log.info(LOGSTR + 'actual img path')
self.log.info(self.img_hand)
if MODE == 'TEST':
self.log.info(LOGSTR + 'testing mode, use another image')
self.img_hand = TEST_IMAGE_PATH_HAND
detail = getDetail(self.img_hand)
self.detail = detail
self.category_str = generate_str(detail['objectLabel'])
if self.category_str != '':
self.set_context('getDetailContext')
self.speak_dialog(
'item.category', {'category': self.category_str}, expect_response=True)
self.brand_str = generate_str(detail['objectLogo'])
color_list = []
for color in detail['objectColor']:
color_list.append(color['colorName'])
self.color_str = generate_str(color_list)
self.kw_str = ' '.join(detail['objectText'])
else:
self.clear_all()
self.remove_context('getDetailContext')
self.speak(
'I cannot understand what is in your hand. Maybe turn around it and let me see it again', expect_response=True)
except Exception as e:
self.log.error((LOGSTR + "Error: {0}").format(e))
self.speak_dialog(
"exception", {"action": "calling computer vision API"})
def handle_ask_item_detail(self, detail, detail_str):
if detail_str == '':
self.speak_dialog(
'cannot.get', {'detail': detail}, expect_response=True)
else:
dialog_str = 'item.' + detail
self.speak_dialog(dialog_str, {detail: detail_str}, expect_response=True)
@intent_handler(IntentBuilder('AskItemCategory').require('Category').require('getDetailContext').build())
def handle_ask_item_category(self, message):
self.handle_ask_item_detail('category', self.category_str)
@intent_handler(IntentBuilder('AskItemColor').require('Color').require('getDetailContext').build())
def handle_ask_item_color(self, message):
self.handle_ask_item_detail('color', self.color_str)
@intent_handler(IntentBuilder('AskItemBrand').require('Brand').require('getDetailContext').build())
def handle_ask_item_brand(self, message):
self.handle_ask_item_detail('brand', self.brand_str)
@intent_handler(IntentBuilder('AskItemKw').require('Kw').require('getDetailContext').build())
def handle_ask_item_keywords(self, message):
self.handle_ask_item_detail('keyword', self.kw_str)
@intent_handler(IntentBuilder('AskItemInfo').require('Info').require('getDetailContext').build())
def handle_ask_item_complete_info(self, message):
self.speak_dialog('item.complete.info', {'category': self.category_str})
self.handle_ask_item_detail('color', self.color_str)
self.handle_ask_item_detail('brand', self.brand_str)
self.handle_ask_item_detail('keyword', self.kw_str)
@intent_handler('not.take.item.intent')
@removes_context('getDetailContext')
def handle_finish_current_item_not_take(self, message):
if self.img_hand != '':
self.speak('Don\'t put it into cart. Let\'s continue shopping!')
self.clear_all()
else:
self.speak('Sorry, I don\'t understand')
@intent_handler('take.item.intent')
@removes_context('getDetailContext')
def handle_finish_current_item_take(self, message):
if self.img_hand != '':
self.speak('I will put the item into cart. Let\'s continue shopping!')
self.clear_all()
else:
self.speak('Sorry, I don\'t understand')
@intent_handler(IntentBuilder('NoContext').one_of('Category', 'Color', 'Brand', 'Kw', 'Info'))
def handle_no_context2(self, message):
self.speak('Please let me have a look at what\'s on your hand first.')
take_photo = self.ask_yesno('do.you.want.to.take.a.photo')
if take_photo == 'yes':
self.handle_view_item_in_hand(message)
elif take_photo == 'no':
self.speak('OK. I won\'t take photo')
else:
self.speak('I cannot understand what you are saying')
def create_skill():
return EasyShopping()