-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_program_v1.py
70 lines (59 loc) · 2.05 KB
/
main_program_v1.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
#!/usr/bin/python
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
# Helper libraries
import numpy as np
import PIL.Image as Image
from picamera import PiCamera
#import RPi.GPIO as GPIO
from gpiozero import Button
from gtts import gTTS
import os
camera = PiCamera()
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(15,GPIO.IN)
button = Button(15,pull_up=False)
classifier_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"
shape = (224, 224)
speech_labels = '/home/pi/tf/labels.txt'
speech_labels = np.array(open(speech_labels).read().splitlines())
# Make the Sequential model
model = tf.keras.Sequential([
hub.KerasLayer(classifier_url, input_shape=shape+(3,))
])
language = 'en'
text = 'Ready'
sp = gTTS(text=text, lang=language, slow=False)
sp.save('/home/pi/tf/ready.mp3')
os.system('omxplayer /home/pi/tf/ready.mp3')
print('Ready')
try:
while True:
#if GPIO.input(15):
button.wait_for_press()
camera.capture('/home/pi/cap.jpg')
img = '/home/pi/cap.jpg'
# Resize the image
img = Image.open(img).resize(shape)
# Convert the image to a NumPy array.
img = np.array(img)/255.0
prediction_array = model.predict(img[np.newaxis, ...])
prediction = np.argmax(prediction_array[0], axis=-1) # Find the top predicted class.
#plt.imshow(img)
#plt.axis('off')
#prediction = labels[prediction] # Show the prediction and image using matplotlib.
speech = speech_labels[prediction]
#plt.title("Prediction: " + speech.title())
#plt.show()
text = str(speech.title())
print(text)
sp = gTTS(text=text, lang=language, slow=False)
sp.save('/home/pi/tf/prediction.mp3')
os.system('omxplayer /home/pi/tf/prediction.mp3')
os.system('sudo rm /home/pi/cap.jpg')
text = 'Ready'
os.system('omxplayer /home/pi/tf/ready.mp3')
print('Ready')
except:
print('Something went wrong')