from kivy.app import App from kivy.uix.button import Button from kivy.uix.image import Image from kivy.uix.floatlayout import FloatLayout from kivy.graphics.texture import Texture from kivy.clock import Clock import numpy as np import cv2 from playsound import playsound from keras.models import load_model import speech_recognition as sr ### este main.py necesita "test.py", "keras_model.h5" y "label.text" model = load_model('keras_model.h5') labels = open('labels.txt', 'r').readlines() cont = 0 aux = ['nada', 'mil','dos mil', 'cinco mil'] class MainApp(App): def build(self): global text layout = FloatLayout(size=(300, 300)) self.image = Image() layout.add_widget(self.image) layout.add_widget(Button( opacity=0, on_release=self.on_press_button) ) self.capture = cv2.VideoCapture(0) Clock.schedule_interval(self.load_video, 1.0/60.0) return layout def load_video(self, *args): global probabilities ret, frame = self.capture.read() image = frame.copy() image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3) image = (image / 127.5) - 1 probabilities = model.predict(image) cv2.putText(frame, f'{aux[np.argmax(probabilities)]}', (5, 30), cv2.FONT_ITALIC, 1, (255, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, f'{cont}', (5, 450), cv2.FONT_ITALIC, 1, (255, 0, 255), 2, cv2.LINE_AA) self.image_frame = frame buffer = cv2.flip(frame, 0).tostring() texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr') texture.blit_buffer(buffer, colorfmt='bgr', bufferfmt='ubyte') self.image.texture = texture def on_press_button(self, *args): global cont r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source) try: text = r.recognize_google(audio, language='es-ES') if text == 'contar': if np.argmax(probabilities) == 1: playsound('mil pesos.mp3') cont += 1 elif np.argmax(probabilities) == 2: playsound('2.mp3') playsound('mil pesos.mp3') cont += 2 elif np.argmax(probabilities) == 3: playsound('5.mp3') playsound('mil pesos.mp3') cont += 5 else: playsound('enfocar.mp3') return 0 elif text == 'total': if cont == 1: playsound('mil pesos.mp3') elif cont < 16 and cont > 0: playsound('{}.mp3'.format(cont)) playsound('mil pesos.mp3') elif cont != 0 and (cont % 10) == 0 and cont < 60: playsound('{}.mp3'.format(cont)) playsound('mil pesos.mp3') elif cont != 0: aux = cont % 10 digito = cont - aux playsound('{}.mp3'.format(digito)) playsound('y.mp3') playsound('{}.mp3'.format(aux)) playsound('mil pesos.mp3') else: playsound('enfocar.mp3') return 0 elif text == 'borrar': cont = 0 else: return 0 except: return 0 if __name__ == '__main__': MainApp().run()