Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| import tempfile | |
| import os | |
| def analyze_body_movement(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." | |
| frame_count = movement_score = 0 | |
| prev_gray = None | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| if prev_gray is not None: | |
| movement_score += np.sum(cv2.absdiff(prev_gray, gray)) | |
| prev_gray = gray | |
| frame_count += 1 | |
| cap.release() | |
| if not isinstance(video, str): | |
| os.unlink(video_path) | |
| avg_movement = movement_score / (frame_count - 1) if frame_count > 1 else 0 | |
| movement_level = "Low" if avg_movement < 1000 else "Medium" if avg_movement < 5000 else "High" | |
| return f"Movement level: {movement_level}\nAverage movement score: {avg_movement:.2f}" | |
| def create_body_movement_tab(): | |
| with gr.Column(): | |
| with gr.Row(): | |
| with gr.Column(): | |
| video_input = gr.Video() | |
| analyze_button = gr.Button("Analyze") | |
| output = gr.Textbox(label="Analysis Results") | |
| # Add the example here | |
| gr.Examples(["./assets/videos/fitness.mp4"], [video_input]) | |
| analyze_button.click(analyze_body_movement, inputs=video_input, outputs=output) |