Spaces:
Runtime error
Runtime error
import gradio as gr | |
import cv2 | |
import numpy as np | |
import tempfile | |
import os | |
def analyze_posture(video): | |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file: | |
video_path = video if isinstance(video, str) else temp_file.name | |
if not isinstance(video, str): | |
temp_file.write(video) | |
cap = cv2.VideoCapture(video_path) | |
if not cap.isOpened(): | |
return "Error: Unable to open video file." | |
posture_score = frame_count = 0 | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
left_half = frame[:, :frame.shape[1]//2] | |
right_half = cv2.flip(frame[:, frame.shape[1]//2:], 1) | |
posture_score += np.sum(cv2.absdiff(left_half, right_half)) | |
frame_count += 1 | |
cap.release() | |
if not isinstance(video, str): | |
os.unlink(video_path) | |
avg_posture_score = posture_score / frame_count if frame_count > 0 else 0 | |
posture_quality = "Good" if avg_posture_score < 1000000 else "Fair" if avg_posture_score < 2000000 else "Poor" | |
return f"Posture quality: {posture_quality}\nAverage posture score: {avg_posture_score:.2f}" | |
def create_posture_analysis_tab(): | |
with gr.Column(): | |
video_input = gr.Video() | |
analyze_button = gr.Button("Analyze") | |
output = gr.Textbox(label="Analysis Results") | |
analyze_button.click(analyze_posture, inputs=video_input, outputs=output) | |
# Add examples | |
gr.Examples( | |
examples=["./assets/videos/fitness.mp4"], | |
inputs=video_input | |
) |