import gradio as gr from evaluate import eval, install from datetime import datetime import os, shutil, spaces import warnings, tempfile from audio import gen_audio warnings.filterwarnings("ignore") warnings.simplefilter("ignore") import time, cv2, os videos = { # "教學視頻1": "teach/00.mp4", # "教學視頻2": "teach/01.mp4", # "教學視頻3": "teach/02.mp4", # "教學視頻4": "teach/03.mp4", "(病症)高峰期": "teach/(病症)高峰期.mp4", "公園": "teach/公園.mp4", "健康": "teach/健康.mp4", "刷牙": "teach/刷牙.mp4", "圖書館": "teach/圖書館.mp4", "下午": "teach/下午.mp4", "中風": "teach/中風.mp4", "中暑": "teach/中暑.mp4", "主題": "teach/主題.mp4", "住院證明": "teach/住院證明.mp4" } jsons = { # "教學視頻1": "teach/00.json", # "教學視頻2": "teach/01.json", # "教學視頻3": "teach/02.json", # "教學視頻4": "teach/03.json", "(病症)高峰期": "teach/(病症)高峰期.json", "公園": "teach/公園.json", "健康": "teach/健康.json", "刷牙": "teach/刷牙.json", "圖書館": "teach/圖書館.json", "下午": "teach/下午.json", "中風": "teach/中風.json", "中暑": "teach/中暑.json", "主題": "teach/主題.json", "住院證明": "teach/住院證明.json" } def get_video_url(option): # print(videos[option]) return videos[option] def flip_video_horizontally(video_path): if not os.path.exists(video_path): print(f"Error: {video_path} does not exist.") return cap = cv2.VideoCapture(video_path) if not cap.isOpened(): print(f"Error: cannot open {video_path}.") return frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) fourcc = cv2.VideoWriter_fourcc(*'XVID') #int(cap.get(cv2.CAP_PROP_FOURCC)) temp_output_path = video_path + "_temp.mp4" out = cv2.VideoWriter(temp_output_path, fourcc, fps, (frame_width, frame_height)) while True: ret, frame = cap.read() if not ret: break flipped_frame = cv2.flip(frame, 1) out.write(flipped_frame) cap.release() out.release() print(video_path, os.path.exists(video_path)) print(temp_output_path, os.path.exists(temp_output_path)) os.replace(temp_output_path, video_path) print(f"Finished flipping video:{video_path}") @spaces.GPU() def evaluate_sign_language(user_video, standard_video_option): if user_video.split('/')[-1].startswith("sample."): flip_video_horizontally(user_video) tmp = tempfile.TemporaryDirectory() tmpdir = tmp.name new_path = tmpdir + "/user.mp4" shutil.copy(user_video, new_path) shutil.copy(videos[standard_video_option], tmpdir + "/standard.mp4") shutil.copy(jsons[standard_video_option], tmpdir + "/standard.json") print("Copy User Video to:", new_path) print("Copy Standard Video to:", tmpdir + "/standard.mp4") print("Copy Standard JSON to:", tmpdir + "/standard.json") test, standard = user_video, videos[standard_video_option] score, final_merged_intervals, comments = eval(test, standard, tmpdir) if score < 95: score = 0 elif score < 98: score = 50 * (score - 95) / 3 elif 98 <= score <= 100: score = 50 * (score - 98) / 2 + 50 current_date = datetime.now().strftime("%Y年%m月%d日") # 初始化反馈报告 qualification = "不合格" if score < 60 else "合格" advice = f"""