Spaces:
Paused
Paused
Commit
·
fccef52
1
Parent(s):
1f26869
salto alto exercise
Browse files
app.py
CHANGED
|
@@ -1,12 +1,21 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from vitpose import VitPose
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from tasks import process_video,process_salto_alto
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
from config import AI_API_TOKEN
|
| 8 |
-
from typing import Any
|
| 9 |
import logging
|
|
|
|
|
|
|
|
|
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
app = FastAPI()
|
|
@@ -34,7 +43,8 @@ async def upload(background_tasks: BackgroundTasks,
|
|
| 34 |
player_id: str = Body(...)):
|
| 35 |
|
| 36 |
if token != AI_API_TOKEN:
|
| 37 |
-
return JSONResponse(content={"message": "Unauthorized",
|
|
|
|
| 38 |
|
| 39 |
logger.info("reading contents")
|
| 40 |
contents = await file.read()
|
|
@@ -47,10 +57,15 @@ async def upload(background_tasks: BackgroundTasks,
|
|
| 47 |
logger.info(f"file saved {file.filename}")
|
| 48 |
|
| 49 |
# Create a clone of the file with content already read
|
| 50 |
-
background_tasks.add_task(process_video,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Return the file as a response
|
| 53 |
-
return JSONResponse(content={"message": "Video uploaded successfully",
|
|
|
|
| 54 |
|
| 55 |
@app.post("/exercise/salto_alto")
|
| 56 |
async def upload(background_tasks: BackgroundTasks,
|
|
@@ -83,9 +98,10 @@ async def upload(background_tasks: BackgroundTasks,
|
|
| 83 |
file.filename,
|
| 84 |
vitpose,
|
| 85 |
player_data,
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
# Return the file as a response
|
| 90 |
print(f"returning response")
|
| 91 |
-
return JSONResponse(content={"message": "Video uploaded successfully",
|
|
|
|
|
|
| 1 |
+
from fastapi import (FastAPI,
|
| 2 |
+
UploadFile,
|
| 3 |
+
File,
|
| 4 |
+
Header,
|
| 5 |
+
BackgroundTasks,
|
| 6 |
+
Body,
|
| 7 |
+
HTTPException)
|
| 8 |
+
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
from vitpose import VitPose
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from tasks import process_video,process_salto_alto
|
| 13 |
from fastapi.responses import JSONResponse
|
| 14 |
from config import AI_API_TOKEN
|
|
|
|
| 15 |
import logging
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
app = FastAPI()
|
|
|
|
| 43 |
player_id: str = Body(...)):
|
| 44 |
|
| 45 |
if token != AI_API_TOKEN:
|
| 46 |
+
return JSONResponse(content={"message": "Unauthorized",
|
| 47 |
+
"status": 401})
|
| 48 |
|
| 49 |
logger.info("reading contents")
|
| 50 |
contents = await file.read()
|
|
|
|
| 57 |
logger.info(f"file saved {file.filename}")
|
| 58 |
|
| 59 |
# Create a clone of the file with content already read
|
| 60 |
+
background_tasks.add_task(process_video,
|
| 61 |
+
file.filename,
|
| 62 |
+
vitpose,
|
| 63 |
+
user_id,
|
| 64 |
+
player_id)
|
| 65 |
|
| 66 |
# Return the file as a response
|
| 67 |
+
return JSONResponse(content={"message": "Video uploaded successfully",
|
| 68 |
+
"status": 200})
|
| 69 |
|
| 70 |
@app.post("/exercise/salto_alto")
|
| 71 |
async def upload(background_tasks: BackgroundTasks,
|
|
|
|
| 98 |
file.filename,
|
| 99 |
vitpose,
|
| 100 |
player_data,
|
| 101 |
+
exercise_id,
|
| 102 |
+
repetitions)
|
| 103 |
|
| 104 |
# Return the file as a response
|
| 105 |
print(f"returning response")
|
| 106 |
+
return JSONResponse(content={"message": "Video uploaded successfully",
|
| 107 |
+
"status": 200})
|
tasks.py
CHANGED
|
@@ -9,6 +9,7 @@ import numpy as np
|
|
| 9 |
|
| 10 |
import time
|
| 11 |
import json
|
|
|
|
| 12 |
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
|
@@ -55,8 +56,8 @@ def process_video(file_name: str,vitpose: VitPose,user_id: str,player_id: str):
|
|
| 55 |
def process_salto_alto(file_name: str,
|
| 56 |
vitpose: VitPose,
|
| 57 |
player_data: dict,
|
| 58 |
-
|
| 59 |
-
|
| 60 |
"""
|
| 61 |
Process a high jump exercise video using VitPose for pose estimation.
|
| 62 |
|
|
@@ -80,14 +81,14 @@ def process_salto_alto(file_name: str,
|
|
| 80 |
output_video = file_name.replace('.mp4', '_analyzed.mp4')
|
| 81 |
# Process the video and get the jump metrics
|
| 82 |
# print(f"reference_height: {reference_height}")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
results_dict = {'video_analysis': {'output_video': 'user_id_2_player_id_2_exercise_salto_alto_VIDEO-2025-05-19-18-55-47_analyzed.mp4'}, 'repetition_data': [{'repetition': 1, 'distancia_elevada': 0.47999998927116394, 'salto_alto': 2.180000066757202, 'potencia_sayer': 3768.719970703125}, {'repetition': 2, 'distancia_elevada': 0.49000000953674316, 'salto_alto': 2.190000057220459, 'potencia_sayer': 3827.929931640625}, {'repetition': 3, 'distancia_elevada': 0.5099999904632568, 'salto_alto': 2.2100000381469727, 'potencia_sayer': 3915.5}]}
|
| 93 |
|
|
@@ -106,7 +107,7 @@ def process_salto_alto(file_name: str,
|
|
| 106 |
def send_results_api(results_dict: dict,
|
| 107 |
player_id: str,
|
| 108 |
exercise_id: str,
|
| 109 |
-
video_path: str):
|
| 110 |
"""
|
| 111 |
Updated function to send results to the new webhook endpoint
|
| 112 |
"""
|
|
@@ -164,6 +165,12 @@ def analyze_jump_video(model: VitPose,
|
|
| 164 |
Returns:
|
| 165 |
Dictionary containing jump metrics and video analysis data
|
| 166 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# Configuration parameters
|
| 168 |
JUMP_THRESHOLD_PERCENT = 0.05 # Porcentaje de cambio en la altura del tobillo para detectar el inicio del salto
|
| 169 |
SMOOTHING_WINDOW = 5 # Ventana para suavizar la altura de los tobillos
|
|
@@ -539,6 +546,11 @@ def draw_metrics_overlay(frame, max_jump_height, salto_alto, velocity_vertical,
|
|
| 539 |
Returns:
|
| 540 |
Frame with metrics overlay
|
| 541 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
overlay = frame.copy()
|
| 543 |
alpha = 0.7
|
| 544 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
|
|
|
| 9 |
|
| 10 |
import time
|
| 11 |
import json
|
| 12 |
+
from fastapi.responses import JSONResponse
|
| 13 |
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
|
|
|
| 56 |
def process_salto_alto(file_name: str,
|
| 57 |
vitpose: VitPose,
|
| 58 |
player_data: dict,
|
| 59 |
+
exercise_id: str,
|
| 60 |
+
repetitions) -> dict:
|
| 61 |
"""
|
| 62 |
Process a high jump exercise video using VitPose for pose estimation.
|
| 63 |
|
|
|
|
| 81 |
output_video = file_name.replace('.mp4', '_analyzed.mp4')
|
| 82 |
# Process the video and get the jump metrics
|
| 83 |
# print(f"reference_height: {reference_height}")
|
| 84 |
+
results_dict = analyze_jump_video(
|
| 85 |
+
model=model,
|
| 86 |
+
input_video=file_name,
|
| 87 |
+
output_video=output_video,
|
| 88 |
+
player_height= float(reference_height) / 100, #cm to m
|
| 89 |
+
body_mass_kg= float(body_mass_kg),
|
| 90 |
+
repetitions=repetitions
|
| 91 |
+
)
|
| 92 |
|
| 93 |
results_dict = {'video_analysis': {'output_video': 'user_id_2_player_id_2_exercise_salto_alto_VIDEO-2025-05-19-18-55-47_analyzed.mp4'}, 'repetition_data': [{'repetition': 1, 'distancia_elevada': 0.47999998927116394, 'salto_alto': 2.180000066757202, 'potencia_sayer': 3768.719970703125}, {'repetition': 2, 'distancia_elevada': 0.49000000953674316, 'salto_alto': 2.190000057220459, 'potencia_sayer': 3827.929931640625}, {'repetition': 3, 'distancia_elevada': 0.5099999904632568, 'salto_alto': 2.2100000381469727, 'potencia_sayer': 3915.5}]}
|
| 94 |
|
|
|
|
| 107 |
def send_results_api(results_dict: dict,
|
| 108 |
player_id: str,
|
| 109 |
exercise_id: str,
|
| 110 |
+
video_path: str) -> JSONResponse:
|
| 111 |
"""
|
| 112 |
Updated function to send results to the new webhook endpoint
|
| 113 |
"""
|
|
|
|
| 165 |
Returns:
|
| 166 |
Dictionary containing jump metrics and video analysis data
|
| 167 |
"""
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
#TODO: REFACTOR THIS FUNCTION
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
# Configuration parameters
|
| 175 |
JUMP_THRESHOLD_PERCENT = 0.05 # Porcentaje de cambio en la altura del tobillo para detectar el inicio del salto
|
| 176 |
SMOOTHING_WINDOW = 5 # Ventana para suavizar la altura de los tobillos
|
|
|
|
| 546 |
Returns:
|
| 547 |
Frame with metrics overlay
|
| 548 |
"""
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
#TODO: REFACTOR THIS FUNCTION
|
| 552 |
+
|
| 553 |
+
|
| 554 |
overlay = frame.copy()
|
| 555 |
alpha = 0.7
|
| 556 |
font = cv2.FONT_HERSHEY_SIMPLEX
|