-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyolodemo.py
More file actions
267 lines (186 loc) · 6.27 KB
/
yolodemo.py
File metadata and controls
267 lines (186 loc) · 6.27 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#! /usr/bin/env python
# -*- encoding: UTF-8 -*-
"""Example: Modify Face Tracking policy on the robot."""
import configuration
import qi
from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule
from PIL import Image
import requests
import json
import argparse
import sys
import time
memory = None
YoloModule = None
class ImageGetter():
def __init__(self):
self.video_service = ALProxy("ALVideoDevice")
resolution = 5#3 # VGA
colorSpace = 11 # RGB
self.video_service.setActiveCamera(0)
for idx in self.video_service.getSubscribers():
if idx.startswith('python_client_yolo'):
self.video_service.unsubscribe(idx)
self.videoClient = self.video_service.subscribe("python_client_yolo", resolution, colorSpace, 10)
print ('videoclientID: ',self.videoClient)
def __del__(self):
print('unsubscribing ',self.videoClient)
self.video_service.unsubscribe(self.videoClient)
def getImage(self):
t0 = time.time()
# Get a camera image.
# image[6] contains the image data passed as an array of ASCII chars.
naoImage = self.video_service.getImageRemote(self.videoClient)
t1 = time.time()
# Time the image transfer.
print "acquisition delay ", t1 - t0
self.video_service.releaseImage(self.videoClient)
return naoImage
def PepperSay(message, tts):
global memory
try:
if memory is not None:
memory.unsubscribeToEvent("Dialog/LastInput","YoloModule")
if tts is not None:
tts.say(message)
if memory is not None:
memory.subscribeToEvent("Dialog/LastInput","YoloModule", "onInput")
except:
pass
# create python module
class myModule(ALModule):
"""python class myModule test auto documentation: comment needed to create a new python module"""
def __init__(self, name):
ALModule.__init__(self, name)
self.tts = ALProxy("ALTextToSpeech")
self.mycamera= ImageGetter()
self.show_flag = False
def close(self):
self.mycamera.close()
def onInput(self, key, value, message):
awp = ALProxy("ALBasicAwareness")
awp.pauseAwareness()
global memory
print memory.getData("Dialog/LastInput")
if memory.getData("Dialog/LastInput")=='':
return
#print memory.getDataListName()
print ("yeah:",key, value, message)
#self.tts.say("yeah!")
#PepperSay("mmm...",self.tts)
image = self.mycamera.getImage()
awp.resumeAwareness()
#print ("image:", image)
if image is None:
return
print "width of the image: ", image[0]
print "height of the image: ", image[1]
print "image.getNbLayers: ", image[2]
print 'colorspace of the image: ', image[3]
print 'time stamp (second): ', image[4]
print 'time stamp (microsecond):', image[5]
print 'data of the image (ignore to print). - image[6]', len(image[6])
print 'camera ID: ', image[7]
print 'camera FOV left angle: ', image[8]
print 'camera FOV top angle: ', image[9]
print 'camera FOV right angle: ', image[10]
print 'camera FOV bottom angle: ', image[11]
# use Python Image Library (PIL)
# Create a PIL Image from our pixel array.
image_colorspace = 'RGB'
image_width, image_height = image[0], image[1]
image_data=image[6]
risimage = Image.frombytes(image_colorspace, (image_width, image_height), image_data)
self.saveImage(risimage, 'PNG', 'camImage.png')
risposta = self.analyseImage(risimage,'camImage.png')
print (risposta)
PepperSay(risposta,self.tts)
def saveImage(self, image, image_format='PNG', image_filename='camImage.png'):
# use Python Image Library (PIL)
# Create a PIL Image from our pixel array.
# Save the image.
print '<VideoImage> - save image to', image_filename
image.save(image_filename, image_format)
if self.show_flag == True:
image.show()
def analyseImage(self,image,filename):
#TODO
#filename = 'D:\\foto\\IMG_9324.jpg'
r = requests.post("http://"+configuration.YOLO_IP+":" + str(configuration.YOLO_PORT)+"/detect",
files = {'image': open(filename,'rb')},
data={'model': 'yolo4'})
print(r.status_code, r.reason)
ris = r.content
print (ris)
obj = json.loads(ris)
listbb = obj.get('bounding-boxes',[])
classes=[]
for bb in listbb:
classes.append(bb.get('ObjectClassName',''))
risstr = "ecco quello che vedo: " + ', '.join(classes)
return risstr.encode('ascii', 'replace')
def createDialog(dlg, tts):
if configuration.Language == 'ENG':
dlg.setLanguage("English")
print "English loaded topics:", dlg.getLoadedTopics("English")
for topic in dlg.getLoadedTopics("English"):
if topic=='topic_yolo':
dlg.unloadTopic(topic)
f = open("topic_eng.top", "r")
topicContent = f.read()
f.close()
topicName = dlg.loadTopicContent (topicContent)
print topicName
dlg.activateTopic(topicName)
if configuration.Language == 'ITA':
dlg.setLanguage("Italian")
print "Italian loaded topics:", dlg.getLoadedTopics("Italian")
for topic in dlg.getLoadedTopics("Italian"):
if topic=='topic_yolo':
dlg.unloadTopic(topic)
f = open("topic_yolo.top", "r")
topicContent = f.read()
f.close()
topicName = dlg.loadTopicContent (topicContent)
print topicName
dlg.activateTopic(topicName)
# tts.say("Ciao! Dimmi e ti dirò cosa vedo!")
PepperSay("Ciao!",tts)
global memory
memory = ALProxy("ALMemory")
global YoloModule
YoloModule = myModule("YoloModule")
memory.subscribeToEvent("Dialog/LastInput","YoloModule", "onInput")
def main(pip, pport):
myBroker = ALBroker("myBroker",
"0.0.0.0", # listen to anyone
0, # find a free port and use it
pip, # parent broker IP
pport) # parent broker port
# setto linguaggio per sicurezza
tts = ALProxy("ALTextToSpeech")
dlg = ALProxy("ALDialog")
print(tts)
dlg.setASRConfidenceThreshold(0.35)
print (dlg.getASRConfidenceThreshold())
createDialog(dlg,tts)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "Interrupted by user, shutting down"
PepperSay("ciao, a presto!",tts)
global YoloModule
YoloModule.close()
myBroker.shutdown()
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", type=str, default=configuration.PEPPER_IP,
help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
parser.add_argument("--port", type=int, default=configuration.PEPPER_PORT,
help="Naoqi port number")
args = parser.parse_args()
main(args.ip, args.port)