-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_script.py
More file actions
354 lines (296 loc) · 10.9 KB
/
device_script.py
File metadata and controls
354 lines (296 loc) · 10.9 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import os
import sys
import numpy as np
import cv2
import json
from matplotlib import pyplot as plt
import time
import speech_recognition as sr
import pyaudio
import paho.mqtt.client as mqtt_client
import picamera
# 0 = Pas de mouvement à faire
# 0.25 = Mouvement lente vitesse
# 0.5 = Mouvement moyenne vitesse
# 1 = Mouvement grande vitesse
movX = 0
movY = 0
lastCoosJSONFile = "/home/pi/Desktop/LeexEye/lastCoos.json"
lastPhotoIndexFilePath = "/home/pi/Desktop/LeexEye/lastPhotoIndex.txt"
photosFolderPath = "/home/pi/Desktop/LeexEye/photos/"
cascadefileFace = "/home/pi/Desktop/LeexEye/Haar-face-recognition/haarcascade_frontalface_alt.xml"
classCascadeFace = cv2.CascadeClassifier(cascadefileFace)
cascadefileEye = "/home/pi/Desktop/LeexEye/Haar-face-recognition/haarcascade_eye.xml"
classCascadeEye = cv2.CascadeClassifier(cascadefileEye)
def detectSpeecByVoice():
p = sr.Microphone.get_pyaudio().PyAudio()
microIndex = 1 #Index du micro PRi en cours
#microIndex = 2 #Index du micro PRi maison
#print(sr.Microphone.list_microphone_names())
"""
print(sr.Microphone.list_microphone_names()[microIndex][0:3])
while sr.Microphone.list_microphone_names()[microIndex][0:3] != 'USB':
time.sleep(0.5)
sr.Microphone.get_pyaudio().PyAudio().terminate
sr.Microphone.get_pyaudio().PyAudio()
print(sr.Microphone.list_microphone_names(),'boucle')
"""
r = sr.Recognizer()
micro = sr.Microphone(microIndex)
print("[Listen]")
result = ""
try:
with micro as source:
r.adjust_for_ambient_noise(source)
audio_data = r.listen(source, timeout=2, phrase_time_limit=5)
#FR
result = r.recognize_google(audio_data, language="fr-FR")
except sr.WaitTimeoutError:
print("Pas de texte ou de commande vocale.")
except sr.UnknownValueError:
try:
#EN
result = r.recognize_google(audio_data)
except sr.UnknownValueError:
print("Pas de texte ou de commande vocale.")
except OSError:
print("[Ended] \nPas de micro ! Branchez en un USB. \nRedémarrage!")
p.terminate()
os.system("sleep 1 && sh /home/pi/Desktop/LeexEye/launcher.sh &")
os.system("sudo pkill -15 -f main.py")
sys.exit()
p.terminate()
print("[Ended]\n\"" + result + "\"")
return result
def checkDiskPhotoSize():
if os.path.exists(photosFolderPath):
fileCount = 0
paths = os.listdir(photosFolderPath)
for path in paths:
if os.path.isfile(photosFolderPath + path):
fileCount += 1
#print(photosFolderPath + path + " id: " + str(path.split("img")[1].split(".jpg")[0]))
#print(fileCount)
if fileCount >= 50:
ids = []
for path in paths:
if os.path.isfile(photosFolderPath + path):
ids.append(int(path.split("img")[1].split(".jpg")[0]))
ids.sort()
os.remove(photosFolderPath + "img" + str(ids[0]) + ".jpg")
#print(ids)
def takePicture(lastPhotoIndex):
camera = picamera.PiCamera()
#camera.resolution = (1280, 720)
camera.framerate = 5
#camera.start_preview()
#time.sleep(2)
camera.capture("/home/pi/Desktop/LeexEye/photos/img" + str(lastPhotoIndex) + ".jpg")
#camera.stop_preview()
camera.close()
nextImageIndex(lastPhotoIndex)
def getLastEyeCoos(eye):
x = 0
y = 0
if not os.path.exists(lastCoosJSONFile):
f = open(lastCoosJSONFile, 'x')
f = open(lastCoosJSONFile, 'r+')
if len(str(f.read())) == 0:
f = open(lastCoosJSONFile, 'w+')
fileBaseCoos = {
'left': [x, y],
'right': [x, y]
}
json.dump(fileBaseCoos, f)
f.close
if eye == 0:
return fileBaseCoos['left']
elif eye == 1:
return fileBaseCoos['right']
else :
f = open(lastCoosJSONFile, 'r+')
fileCoos = json.loads(str(f.read()))
if eye == 0:
x = fileCoos['left'][0]
y = fileCoos['left'][1]
f.close
return x, y
elif eye == 1:
x = fileCoos['right'][0]
y = fileCoos['right'][1]
f.close
return x, y
if not f.closed:
f.close
return x, y
def setLastEyeCoos(x, y, eye):
if not os.path.exists(lastCoosJSONFile):
getLastEyeCoos(eye)
leftEye = getLastEyeCoos(0)
rightEye = getLastEyeCoos(1)
f = open(lastCoosJSONFile, 'w+')
if eye == 0:
otherEye = rightEye
fileBaseCoos = {
'left': [x, y],
'right': [otherEye[0], otherEye[1]]
}
elif eye == 1:
otherEye = leftEye
fileBaseCoos = {
'left': [otherEye[0], otherEye[1]],
'right': [x, y]
}
json.dump(fileBaseCoos, f)
f.close
def getImageIndex():
lastPhotoIndex_ = 0
if not os.path.exists(lastPhotoIndexFilePath):
f = open(lastPhotoIndexFilePath, 'x')
f = open(lastPhotoIndexFilePath, 'r+')
if len(f.read()) == 0:
f = open(lastPhotoIndexFilePath, 'w+')
f.write("0")
lastPhotoIndex_ = 0
f.close
else :
f = open(lastPhotoIndexFilePath, 'r+')
lastPhotoIndex_ = int(f.read())
if not f.closed:
f.close
return lastPhotoIndex_
def nextImageIndex(lastPhotoIndex):
f = open(lastPhotoIndexFilePath, 'w+')
nextPhotoIndex = lastPhotoIndex + 1
f.write(str(nextPhotoIndex))
f.close
def setImageIndex(newIndex):
f = open(lastPhotoIndexFilePath, 'w')
f.write(str(newIndex))
f.close
def eyeDetect(filePath, movX, movY, texToSend):
roi_gray = 0
#roi_color = 0
img = cv2.imread(filePath)
imgY = img.shape[0]
imgX = img.shape[1]
rotateM = cv2.getRotationMatrix2D(center=((imgX / 2), (imgY /2)), angle=180, scale=1)
rotatedImg = cv2.warpAffine(src=img, M=rotateM, dsize=(imgX, imgY))
img = rotatedImg
img = img[int(imgY / 12):int(((imgY / 12) + (imgY / 12) * 10)), int(imgX / 12): int(((imgX / 12) + (imgX / 12) * 10))]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.array(gray, dtype='uint8')
debut = time.time()
faces = classCascadeFace.detectMultiScale(gray, 1.112)
if len(faces) != 1:
#Erreur: Plusieurs visages
print("Il faut un seul visage, plusieurs ou aucun sont détectés.")
for (x, y, w, h) in faces:
#cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 255), 1)
roi_gray = gray[y: y+h, x: x+w]
#roi_color = img[y: y+h, x: x+w]
eyes = classCascadeEye.detectMultiScale(roi_gray, 3)
print(len(eyes))
if len(eyes) != 2:
#Erreur: Plus ou moins de 2 yeux
print("Il faut 2 aucun ou plus de 2 yeux sont détectés.")
currentEye = 0
for (ex, ey, ew, eh) in eyes:
#cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (255, 255, 0), 2)
if ex < w/2:
currentEye = 0
print("Oeil Gauche")
elif ex > w/2:
currentEye = 1
print("Oeil Droite")
eye_centerX = (ex+(ex+ew))/2
eye_centerY = (ey+(ey+eh))/2
#cv2.line(img, (int(eye_centerX) + x, int(eye_centerY) + y), (int(eye_centerX) + x, int(eye_centerY) + y), (0, 0, 255), 10)
lastCoos = getLastEyeCoos(currentEye)
lastEye_centerX = lastCoos[0]
lastEye_centerY = lastCoos[1]
#print("Dernières coos: " + str(lastCoos))
#print("Coos actuelles: x=" + str(int(eye_centerX)) + ", y=" + str(int(eye_centerY)))
if lastEye_centerX == 0 or lastEye_centerY == 0:
setLastEyeCoos(eye_centerX, eye_centerY, currentEye)
continue
else:
gapX = lastEye_centerX - eye_centerX
gapY = lastEye_centerY - eye_centerY
if gapX > 0:
#Oeil décalé à gauche donc regarder à droite
#print("Deplacer à droite")
if movX < 0:
movX = 0
elif movX > 0:
movX = 1
else:
movX = 0.5
elif gapX < 0:
#Oeil décalé à droite donc regarder à gauche
#print("Deplacer à gauche")
if movX > 0:
movX = 0
elif movX < 0:
movX = -1
else:
movX = -0.5
if gapY > 0:
#Oeil décalé à haut donc regarder à haut
#print("Deplacer en haut")
if movY < 0:
movY = 0
elif movY > 0:
movY = 1
else:
movY = 0.5
elif gapY < 0:
#Oeil décalé à bas donc regarder à bas
#print("Deplacer en bas")
if movY > 0:
movY = 0
elif movY < 0:
movY = -1
else:
movY = -0.5
setLastEyeCoos(eye_centerX, eye_centerY, currentEye)
executeMovements(movX, movY, texToSend)
duree = time.time() - debut
print("Durée du programme: " + str(duree) + " seconde(s)")
#plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#plt.imsave("rendu_detection.jpg", img)
#plt.show()
return movX, movY
def executeMovements(movX, movY, textToSend):
print("X: " + str(movX))
print("X: " + str(movX))
movs = (movX, movY)
MqttServerIP = "localhost" #IP du broker (rapsberry)
client = mqtt_client.Client()
client.connect(MqttServerIP, 1883, 60)
client.loop_start()
client.publish('leexeye', json.dumps(str(movs)) + " / " + textToSend, 1)
client.loop_stop()
client.disconnect()
if __name__ == '__main__':
print("[Main.py Running ...]")
programEnd = False
try:
while not programEnd:
time.sleep(0.5)
checkDiskPhotoSize()
lastPhotoIndex = getImageIndex()
takePicture(lastPhotoIndex)
textToSend = detectSpeecByVoice()
lastPhotoFilePath = "/home/pi/Desktop/LeexEye/photos/img" + str(lastPhotoIndex) + ".jpg"
eyeDetect(lastPhotoFilePath, movX, movX, textToSend)
time.sleep(0.5)
except KeyboardInterrupt:
programEnd = True
pass
"""
lastPhotoFilePath = "photos/tests/test0.jpg"
movs = eyeDetect(lastPhotoFilePath, movX, movX)
lastPhotoFilePath = "photos/tests/test1.jpg"
movs = eyeDetect(lastPhotoFilePath, movX, movY)
"""