from flask import Flask, request, jsonify from upload_file import OSSUploader import os from video_handler import process_video, merge_videos from speech_synthesis_factory import TtsClientWrapper import azure.cognitiveservices.speech as speechsdk import time app = Flask(__name__) oss_uploader = OSSUploader() @app.route('/tts', methods=['POST']) def tts_process(): print(request.get_json()) data = request.get_json() if 'code' not in data: return jsonify({"error": "Missing 'code' in request data"}), 400 if 'text' not in data: return jsonify({"error": "Missing 'text' in request data"}), 400 tts_wrapper = TtsClientWrapper() code = data['code'] text = data['text'] synthesizer = tts_wrapper.getTtsClient(code) speech_synthesis_result = synthesizer.speak_text_async(text).get() if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted: audio_data_stream = speechsdk.AudioDataStream(speech_synthesis_result) timestamp = int(time.time()) temp_file_path = f'tts_{timestamp}.wav' audio_data_stream.save_to_wav_file(temp_file_path) video_path = oss_uploader.upload_video_to_oss_local(temp_file_path) # 删除临时文件 if os.path.exists(temp_file_path): os.remove(temp_file_path) print(video_path) return video_path else: return jsonify({"error": f"Speech synthesis failed: {speech_synthesis_result.error_details}"}) @app.route('/deepface', methods=['POST']) def deepface_process(): print(request.get_json()) video_paths = [] try: data = request.get_json() if 'video_urls' not in data: return jsonify({"error": "Missing 'video_urls' in request data"}), 400 video_urls = data['video_urls'] # 分别处理每个视频URL emotion_dicts = [] for url in video_urls: emotion_dict, video_path = process_video(url.split("?")[0]) emotion_dicts.append(emotion_dict) video_paths.append(video_path) # 合并标注好的视频 merged_video_path = merge_videos(video_paths) # 合并情绪字典 merged_emotion_dict = {} for d in emotion_dicts: for emotion, count in d.items(): if emotion in merged_emotion_dict: merged_emotion_dict[emotion] += count else: merged_emotion_dict[emotion] = count # 计算总帧数 total_frames = sum(sum(d.values()) for d in emotion_dicts) # 计算每种情绪的百分比并保留整数部分 emotion_percentage_dict = {} for emotion, count in merged_emotion_dict.items(): percentage = int((count / total_frames) * 100) emotion_percentage_dict[emotion] = percentage merged_video_path = oss_uploader.upload_video_to_oss_local(merged_video_path) response = { "emotion_percentage_dict": emotion_percentage_dict, "merged_video_path": merged_video_path } return jsonify(response) finally: # 删除中间产生的标注视频 for video_path in video_paths: if os.path.exists(video_path): os.remove(video_path) if __name__ == '__main__': app.run(host='0.0.0.0', port=2002, debug=True)