soeexample.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import os
  4. import threading
  5. import time
  6. import requests
  7. from spoken_language.common import credential
  8. from spoken_language.read_config import read_config
  9. from spoken_language.soe import speaking_assessment
  10. config_data = read_config()
  11. app_id, secret_id, secret_key = config_data['appId'], config_data['SecretId'], config_data['SecretKey']
  12. APPID = app_id
  13. SECRET_ID = secret_id
  14. SECRET_KEY = secret_key
  15. TOKEN = ""
  16. ENGINE_MODEL_TYPE = "16k_en"
  17. SLICE_SIZE = 32000
  18. spoken_result = {}
  19. class MySpeechRecognitionListener(speaking_assessment.SpeakingAssessmentListener):
  20. def __init__(self, id):
  21. self.id = id
  22. def on_recognition_start(self, response):
  23. pass
  24. def on_intermediate_result(self, response):
  25. rsp_str = json.dumps(response, ensure_ascii=False)
  26. def on_recognition_complete(self, response):
  27. global spoken_result
  28. spoken_result[self.id] = response
  29. def on_fail(self, response):
  30. rsp_str = json.dumps(response, ensure_ascii=False)
  31. def process(id):
  32. audio = r"C:\Users\86131\Desktop\音频\output_16k_mono.mp3"
  33. listener = MySpeechRecognitionListener(id)
  34. credential_var = credential.Credential(SECRET_ID, SECRET_KEY)
  35. recognizer = speaking_assessment.SpeakingAssessment(
  36. APPID, credential_var, ENGINE_MODEL_TYPE, listener)
  37. recognizer.set_text_mode(0)
  38. recognizer.set_ref_text("anyway")
  39. recognizer.set_eval_mode(0)
  40. recognizer.set_keyword("")
  41. recognizer.set_sentence_info_enabled(0)
  42. recognizer.set_voice_format(1)
  43. try:
  44. recognizer.start()
  45. with open(audio, 'rb') as f:
  46. content = f.read(SLICE_SIZE)
  47. while content:
  48. recognizer.write(content)
  49. content = f.read(SLICE_SIZE)
  50. # sleep模拟实际实时语音发送间隔
  51. time.sleep(0.2)
  52. except Exception as e:
  53. print(e)
  54. finally:
  55. recognizer.stop()
  56. def process_rec(task_id, audio_path, audio_text, audio_binary=None):
  57. audio = audio_path
  58. listener = MySpeechRecognitionListener(task_id)
  59. credential_var = credential.Credential(SECRET_ID, SECRET_KEY)
  60. recognizer = speaking_assessment.SpeakingAssessment(
  61. APPID, credential_var, ENGINE_MODEL_TYPE, listener)
  62. recognizer.set_text_mode(0)
  63. recognizer.set_ref_text(audio_text)
  64. recognizer.set_eval_mode(1)
  65. recognizer.set_keyword("")
  66. recognizer.set_sentence_info_enabled(0)
  67. recognizer.set_voice_format(2)
  68. recognizer.set_rec_mode(1)
  69. try:
  70. recognizer.start()
  71. if audio_binary:
  72. recognizer.write(audio_binary)
  73. else:
  74. with open(f"{task_id}.mp3", 'rb') as f:
  75. content = f.read()
  76. recognizer.write(content)
  77. except Exception as e:
  78. print(e)
  79. finally:
  80. recognizer.stop()
  81. def process_multithread(number):
  82. thread_list = []
  83. for i in range(0, number):
  84. thread = threading.Thread(target=process, args=(i,))
  85. thread_list.append(thread)
  86. thread.start()
  87. for thread in thread_list:
  88. thread.join()
  89. def make_spoken(task_id, audio_url, audio_content, audio_text):
  90. if audio_url:
  91. print("有url,应该去下载mp3文件")
  92. r = requests.get(audio_url)
  93. audio_content = r.content
  94. else:
  95. with open(f"{task_id}.mp3", 'wb') as f:
  96. f.write(audio_content)
  97. process_rec(task_id, audio_path=f"", audio_text=audio_text, audio_binary=audio_content)
  98. global spoken_result
  99. for _ in range(60):
  100. if task_id in spoken_result:
  101. r = spoken_result[task_id]
  102. del spoken_result[task_id]
  103. if os.path.exists(f"{task_id}.mp3"):
  104. os.remove(f"{task_id}.mp3")
  105. return r
  106. time.sleep(0.5)
  107. return None
  108. if __name__ == "__main__":
  109. process_rec(0, r"C:\Users\86131\Desktop\音频\output_16k_mono.mp3", "You must study to be frank with the world apple")