Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

113 lignes
3.1 KiB

  1. from flask import Flask, render_template, jsonify
  2. from flask_socketio import SocketIO
  3. import os
  4. import speech_recognition as sr
  5. import question
  6. import imageio
  7. import threading
  8. import time
  9. app = Flask(__name__)
  10. socketio = SocketIO(app)
  11. current_directory = os.path.dirname(os.path.realpath(__file__))
  12. intents_and_question = question.intents_and_question
  13. video_urls = question.video_urls
  14. micPermission = True
  15. class SharedData:
  16. def __init__(self):
  17. self.micPermission = micPermission
  18. #self.pause_event = threading.Event()
  19. #self.listening_state = True
  20. def get_intent(text):
  21. lowercase_text = text.lower()
  22. matching_intent = [intent for intent, questions in intents_and_question.items() if any(q in lowercase_text for q in questions)]
  23. return matching_intent[0] if matching_intent else 'no_answer'
  24. def get_video_response(intent):
  25. return video_urls.get(intent)
  26. def calc_video_length(url):
  27. try:
  28. reader = imageio.get_reader(url)
  29. duration = reader.get_meta_data()['duration']
  30. reader.close()
  31. return duration
  32. except Exception as e:
  33. return "error"
  34. def recognize_and_emit(shared_data):
  35. recognizer = sr.Recognizer()
  36. if shared_data.micPermission is True:
  37. with sr.Microphone() as source:
  38. while True:
  39. try:
  40. #shared_data.pause_event.wait()
  41. print("Listening ....")
  42. audio_data = recognizer.record(source, duration = 3.5)
  43. #audio_data = recognizer.listen(source, timeout=3)
  44. print("Recognizing your text .........")
  45. text = recognizer.recognize_google(audio_data)
  46. intent = get_intent(text.strip())
  47. video_url = get_video_response(intent)
  48. length = calc_video_length(video_url)
  49. #shared_data.length = length
  50. socketio.emit('text_received', {'text': text, 'intent': intent, 'video_url': video_url})
  51. print("Recognized Text:", text)
  52. shared_data.micPermission = False
  53. print("Going to sleep...")
  54. source = None
  55. time.sleep(length)
  56. shared_data.micPermission = True
  57. recognize_and_emit(shared_data)
  58. except sr.UnknownValueError:
  59. print("Speech Recognition could not understand audio")
  60. except sr.RequestError as e:
  61. print(f"Could not request {e}")
  62. @app.route('/')
  63. def index():
  64. return render_template('index.html')
  65. @socketio.on('start_listening')
  66. def start_listening(data):
  67. shared_data = SharedData()
  68. recognition_thread = threading.Thread(target=recognize_and_emit, args=(shared_data,))
  69. recognition_thread.start()
  70. #recognition_thread.join() # Wait for the recognition thread to finish
  71. if __name__ == '__main__':
  72. socketio.run(app, debug=True)