dbOperate_upload_Uvoice.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import pyaudio
  2. import wave
  3. import pymysql
  4. import time
  5. from datetime import datetime
  6. from pydub import AudioSegment
  7. import uuid
  8. import os
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aiDogProject.settings")
  10. import django
  11. django.setup()
  12. from aiDogApp.models import *
  13. from .generalRequest import req_url # Importing your generalRequest.py method
  14. # Audio recording settings
  15. SAMPLE_RATE = 16000
  16. BIT_RATE = 16
  17. CHANNELS = 1
  18. RECORD_SECONDS = 2
  19. CHUNK = 1024
  20. FORMAT = pyaudio.paInt16
  21. # 连接mysql数据库
  22. # def get_db_connection():
  23. # return pymysql.connect(
  24. # host='127.0.0.1',
  25. # port=3306,
  26. # user='root',
  27. # password='',
  28. # database='aidog'
  29. # )
  30. # 录制音频 并存储为WAV文件
  31. def record_audio(file_path,mp3_path, duration=4):
  32. # Audio settings
  33. sample_rate = 16000
  34. channels = 1
  35. bit_rate = pyaudio.paInt16
  36. chunk_size = 1024
  37. audio = pyaudio.PyAudio()
  38. # Start recording
  39. stream = audio.open(format=bit_rate, channels=channels,
  40. rate=sample_rate, input=True,
  41. frames_per_buffer=chunk_size)
  42. print("Recording...")
  43. frames = []
  44. for _ in range(0, int(sample_rate / chunk_size * duration)):
  45. data = stream.read(chunk_size)
  46. frames.append(data)
  47. print("Recording complete.")
  48. # Stop and close the stream
  49. stream.stop_stream()
  50. stream.close()
  51. audio.terminate()
  52. # Save audio to file
  53. with wave.open(file_path, 'wb') as wf:
  54. wf.setnchannels(channels)
  55. wf.setsampwidth(audio.get_sample_size(bit_rate))
  56. wf.setframerate(sample_rate)
  57. wf.writeframes(b''.join(frames))
  58. # Convert to MP3
  59. audio_segment = AudioSegment.from_wav(file_path)
  60. audio_segment.export(mp3_path, format="mp3")
  61. # Remove the intermediate WAV file
  62. os.remove(file_path)
  63. # Function to insert audio information into MySQL database
  64. def insert_voice_data(uid, feature_id, wav_file_path,command_type,record_time):
  65. try:
  66. # conn = get_db_connection()
  67. # cursor = conn.cursor()
  68. # 读取 WAV 文件 为二进制数据
  69. with open(wav_file_path, 'rb') as wav_file:
  70. wav_data = wav_file.read()
  71. # 插入到声纹信息表中
  72. # 口令类型(0:叫名字 1:坐下,2:站起,3:摇尾巴,4:走过来,5 抬左手,6:抬右手)
  73. if command_type == 0:
  74. # 口令类型(0:叫名字
  75. # insert_query = """
  76. # INSERT INTO voice_feature_info (uid, voice_feature_id, voice_name_command,command_type)
  77. # VALUES (%s, %s, %s, %s)
  78. # """
  79. # # cursor.execute(insert_query, (uid, feature_id, wav_data, command_type))
  80. VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
  81. voice_name_command=wav_data,
  82. record_time=record_time)
  83. elif command_type == 1:
  84. # 口令类型(1:坐下
  85. VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
  86. voice_sit_command=wav_data,
  87. record_time=record_time)
  88. elif command_type == 2:
  89. # 口令类型(2:站起
  90. VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
  91. voice_stand_command=wav_data,
  92. record_time=record_time
  93. )
  94. elif command_type == 3:
  95. # 口令类型(3:摇尾巴
  96. VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
  97. voice_wagtail_command=wav_data,
  98. record_time=record_time)
  99. else:
  100. # 口令类型(4:走过来
  101. VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
  102. voice_come_command=wav_data,
  103. record_time=record_time)
  104. # conn.commit()
  105. print(f"Data inserted into database for UID: {uid}")
  106. return True
  107. # 抛出异常
  108. except Exception as err:
  109. print(f"Error: {err}")
  110. return False
  111. # finally:
  112. # cursor.close()
  113. # conn.close()
  114. APPId = "4522d502"
  115. APISecret = "Zjc5MjJhMzBmMDYxNTg4MTNlMTg1MmQw"
  116. APIKey = "f7a9f0ceae3ff7ebfc0c89efeb18810d"
  117. file_path = './media/讯飞开放平台.mp3'#读取数据库路径下音源
  118. # Main method to record audio, save locally, insert to DB, and call req_url()
  119. def process_voice_recording(uid,voice_feature_id, command_type):
  120. # 定义本地缓存路径
  121. cache_directory = "audio_cache"
  122. if not os.path.exists(cache_directory):
  123. os.makedirs(cache_directory)
  124. # 创建一个唯一的文件名
  125. timestamp = int(time.time())
  126. timestamp11 = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  127. file_path = os.path.join(cache_directory, f"voice_{uid}_{timestamp}.wav")
  128. mp3_path=os.path.join(cache_directory, f"voice_{uid}_{timestamp}.mp3")
  129. # 录制wav音频 并存储到cache
  130. record_audio(file_path,mp3_path)
  131. # 插入到数据库 声纹信息表
  132. if insert_voice_data(uid, voice_feature_id, mp3_path,command_type,timestamp11):
  133. # 如果插入成功,调用generalRequest.py里的第三方声纹识别API
  134. res='xxx'
  135. res=req_url(api_name='createFeature', APPId=APPId,
  136. APIKey=APIKey, APISecret=APISecret, file_path=mp3_path, featureId=voice_feature_id,groupId=uid) # 传参数 缓存音频路径,声纹id
  137. print(f"音频文件缓存在 {mp3_path} 并且传参到 req_url().")
  138. yy = datetime.today()
  139. end_timestamp = datetime.strftime(yy, '%Y-%m-%d %H:%M:%S')
  140. # return res
  141. return (f"音频文件缓存在 {mp3_path} 并且传参到 req_url().返回内容为{res}.请求时间为:---{timestamp11}.返回时间为:---{end_timestamp}")
  142. else:
  143. print("Failed to insert voice data into database.")
  144. return ("Failed to insert voice data into database.")
  145. # 移除缓存中的wav文件
  146. # os.remove(file_path)
  147. # Example Usage:
  148. # if __name__ == "__main__":
  149. # user_uid = 'JTYNZP9O' # 用户ID
  150. # feature_id = 'sfggcvvh1212' # 用户声纹 ID
  151. # command_type=1
  152. #
  153. # process_voice_recording(user_uid, feature_id,command_type)