main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from flask import Flask, request, jsonify
  2. from upload_file import OSSUploader
  3. import os
  4. from video_handler import process_video, merge_videos
  5. from speech_synthesis_factory import TtsClientWrapper
  6. import azure.cognitiveservices.speech as speechsdk
  7. import time
  8. app = Flask(__name__)
  9. oss_uploader = OSSUploader()
  10. @app.route('/tts', methods=['POST'])
  11. def tts_process():
  12. print(request.get_json())
  13. data = request.get_json()
  14. if 'code' not in data:
  15. return jsonify({"error": "Missing 'code' in request data"}), 400
  16. if 'text' not in data:
  17. return jsonify({"error": "Missing 'text' in request data"}), 400
  18. tts_wrapper = TtsClientWrapper()
  19. code = data['code']
  20. text = data['text']
  21. synthesizer = tts_wrapper.getTtsClient(code)
  22. speech_synthesis_result = synthesizer.speak_text_async(text).get()
  23. if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
  24. audio_data_stream = speechsdk.AudioDataStream(speech_synthesis_result)
  25. timestamp = int(time.time())
  26. temp_file_path = f'tts_{timestamp}.wav'
  27. audio_data_stream.save_to_wav_file(temp_file_path)
  28. video_path = oss_uploader.upload_video_to_oss_local(temp_file_path)
  29. # 删除临时文件
  30. if os.path.exists(temp_file_path):
  31. os.remove(temp_file_path)
  32. print(video_path)
  33. return video_path
  34. else:
  35. return jsonify({"error": f"Speech synthesis failed: {speech_synthesis_result.error_details}"})
  36. @app.route('/deepface', methods=['POST'])
  37. def deepface_process():
  38. print(request.get_json())
  39. video_paths = []
  40. try:
  41. data = request.get_json()
  42. if 'video_urls' not in data:
  43. return jsonify({"error": "Missing 'video_urls' in request data"}), 400
  44. video_urls = data['video_urls']
  45. # 分别处理每个视频URL
  46. emotion_dicts = []
  47. for url in video_urls:
  48. emotion_dict, video_path = process_video(url.split("?")[0])
  49. emotion_dicts.append(emotion_dict)
  50. video_paths.append(video_path)
  51. # 合并标注好的视频
  52. merged_video_path = merge_videos(video_paths)
  53. # 合并情绪字典
  54. merged_emotion_dict = {}
  55. for d in emotion_dicts:
  56. for emotion, count in d.items():
  57. if emotion in merged_emotion_dict:
  58. merged_emotion_dict[emotion] += count
  59. else:
  60. merged_emotion_dict[emotion] = count
  61. # 计算总帧数
  62. total_frames = sum(sum(d.values()) for d in emotion_dicts)
  63. # 计算每种情绪的百分比并保留整数部分
  64. emotion_percentage_dict = {}
  65. for emotion, count in merged_emotion_dict.items():
  66. percentage = int((count / total_frames) * 100)
  67. emotion_percentage_dict[emotion] = percentage
  68. merged_video_path = oss_uploader.upload_video_to_oss_local(merged_video_path)
  69. response = {
  70. "emotion_percentage_dict": emotion_percentage_dict,
  71. "merged_video_path": merged_video_path
  72. }
  73. return jsonify(response)
  74. finally:
  75. # 删除中间产生的标注视频
  76. for video_path in video_paths:
  77. if os.path.exists(video_path):
  78. os.remove(video_path)
  79. if __name__ == '__main__':
  80. app.run(host='0.0.0.0', port=2002, debug=True)