Spaces:
Running
Running
File size: 1,638 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 50 51 |
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) |