Spaces:
Runtime error
Runtime error
File size: 1,592 Bytes
fc286f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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
) |