-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
93 lines (91 loc) · 3.62 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
import rekognition
import storage
import database
import gpiozero
import camera
import draw
import time
import audio
import polly
import os
ct = 50
image_file = '/tmp/image.jpg'
temp_file = '/tmp/test.jpg'
button = gpiozero.Button(17)
ready_message = "[PiCeDoFi-IMAPI-RU] system ready"
try:
audio.play_mp3("startup_comment.mp3")
os.system('clear')
print(ready_message)
while True:
if button.is_pressed:
print("starting preview...")
camera.preview(button)
camera.capture(image_file)
print('working...')
audio.play_mp3("intro_comment.mp3")
# upload picture to S3
s3 = storage.upload(image_file)
image = draw.load_image(image_file)
# call rekognition apis
# Step 1. Are there faces in the image?
face_result = rekognition.detect_faces_api(s3)
face_labels = rekognition.get_face_labels(face_result, ct)
if face_labels:
os.system('clear')
draw.display_text(face_labels)
database.inc(face_labels)
audio.play_mp3("faces_comment.mp3")
polly.speak(face_labels)
draw.annotate_faces(image, face_result)
# Step 2. Is the picture of a celebrity?
celeb_result = rekognition.detect_celebrities_api(s3)
celeb_labels = rekognition.get_celebrity_labels(celeb_result, ct)
if celeb_labels:
draw.display_text(celeb_labels)
database.inc(celeb_labels)
draw.annotate_celebs(image, celeb_result)
for celeb in celeb_labels:
audio.play_mp3("celeb_comment.mp3")
polly.render_speech(celeb)
celeb_image = rekognition.get_celebrity_image(celeb)
if celeb_image:
draw.preview_image(celeb_image, button)
os.system('clear')
desc = rekognition.get_celebrity_desc(celeb)
print(desc)
polly.render_speech(desc)
else:
audio.play_mp3("no_celeb_comment.mp3")
# Step 3. Check for words
text_result = rekognition.detect_text_api(s3)
text_labels = rekognition.get_text_labels(text_result, ct)
draw.annotate_text(image, text_result)
if text_labels and not face_labels:
draw.display_text(text_labels)
audio.play_mp3("text_comment.mp3")
polly.speak(text_labels)
# Step 4. What else is in the picture?
label_result = rekognition.detect_labels_api(s3)
labels = rekognition.get_labels(label_result, ct)
draw.annotate_labels(image, label_result)
if labels and not face_labels:
os.system('clear')
draw.display_text(labels)
database.inc(labels)
audio.play_mp3("labels_comment.mp3")
polly.speak(labels)
draw.save_image(image, temp_file)
# Upload annotated image to S3
storage.upload(temp_file)
audio.play_mp3("closure_comment.mp3")
storage.delete(s3['S3Object']['Name'])
draw.preview_image(temp_file, button)
os.system('clear')
print(ready_message)
time.sleep(0.1)
except KeyboardInterrupt:
print("...request to stop program...")
finally:
audio.play_mp3("exception_comment.mp3")
print("[PiCeDoFi-IMAPI-RU] process ended")