-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtalking_camera.py
More file actions
131 lines (104 loc) · 3.86 KB
/
talking_camera.py
File metadata and controls
131 lines (104 loc) · 3.86 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
from gpiozero import Button
from signal import pause
from time import sleep
from picamera import PiCamera
import pygame
import random
from google.cloud import vision
from google.cloud import texttospeech
from google.cloud import translate_v2 as translate
SHUTTER = "GPIO8"
photo_path = '/tmp/photo.jpg'
sound_path = '/tmp/sound.wav'
prefixes = [
"what a nice ", "that's a cool ", "I like this ",
"nice ", "this is a ", "looks like a ", "wow, that's a very cool "
]
# ISO 639-1 standard language codes
languages = [
'en-gb', # English
'en-us', # American
'fr-fr', # French
'es', # Spanish
'it', # Italian
'ar-eg', # Arabic (Egypt)
'zh-cn' # Chinese (PRC)
]
def take_photo(photo_path):
camera.capture(photo_path)
def play_sound(sound_path):
recording = pygame.mixer.Sound(sound_path)
recording.play()
def say_it(text, file):
"""Generates audio file containing text-to-speach conversion"""
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-GB',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
# Set audio file format to Wav
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# The response's audio_content is binary.
with open(file, 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file ' + file)
def see_it(file) -> str:
"""Returns name of object identified in image file"""
res = None
with open(file, "rb") as imageFile:
client = vision.ImageAnnotatorClient()
response = client.annotate_image({
'image': {'content': imageFile.read()},
'features': [{'type': vision.enums.Feature.Type.LABEL_DETECTION}]
})
if len(response.label_annotations) > 0:
res = response.label_annotations[0].description
print(res)
return res
def translate_it(text, target_language):
translate_client = translate.Client()
# target_language to be selected from target_languages list above
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language)
print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage']))
return result['translatedText']
def get_phrase(prefixes, thing) -> str:
"""Returns phrase to be spoken"""
if thing:
return random.choice(prefixes) + thing
else:
return "sorry, I can't see that quite clearly"
def action():
global sound_path
take_photo(photo_path)
description = see_it(photo_path)
if description == 'Rat':
sound_path = 'sound/its-a-rat.wav'
else:
sound_path = '/tmp/sound.wav'
description = get_phrase(prefixes, description)
say_it(description, sound_path)
play_sound(sound_path)
pygame.init()
# Button with external pull-down resistor
shutter_button = Button(SHUTTER, pull_up=None, active_state=False)
camera = PiCamera()
# camera.resolution = (1024, 768)
camera.resolution = (640, 480)
camera.start_preview()
sleep(2) # Camera warm-up time
shutter_button.when_pressed = action
pause()