Spaces:
Build error
Build error
Commit 路
2d0567e
1
Parent(s): ff8dc11
Create loading.py
Browse files- loading.py +67 -0
loading.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
from mediapipe import solutions
|
| 4 |
+
from mediapipe.framework.formats import landmark_pb2
|
| 5 |
+
from mediapipe.tasks import python
|
| 6 |
+
from mediapipe.tasks.python import vision
|
| 7 |
+
|
| 8 |
+
# Crear un objeto PoseLandmarker
|
| 9 |
+
model_asset_path = 'pose_landmarker_heavy.task'
|
| 10 |
+
base_options = python.BaseOptions(model_asset_path, delegate=python.BaseOptions.Delegate.CPU)
|
| 11 |
+
|
| 12 |
+
def draw_landmarks_on_image(rgb_image, detection_result):
|
| 13 |
+
"""
|
| 14 |
+
Dibuja los puntos de referencia de la pose en la imagen.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
rgb_image (np.ndarray): Imagen RGB de entrada.
|
| 18 |
+
detection_result: Resultado de la detecci贸n de pose.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
np.ndarray: Imagen anotada con los puntos de referencia de la pose.
|
| 22 |
+
"""
|
| 23 |
+
pose_landmarks_list = detection_result.pose_landmarks
|
| 24 |
+
annotated_image = np.copy(rgb_image)
|
| 25 |
+
# Recorrer las poses detectadas para visualizarlas
|
| 26 |
+
for pose_landmarks in pose_landmarks_list:
|
| 27 |
+
# Dibujar los puntos de referencia de la pose
|
| 28 |
+
pose_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
|
| 29 |
+
pose_landmarks_proto.landmark.extend([
|
| 30 |
+
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in pose_landmarks])
|
| 31 |
+
solutions.drawing_utils.draw_landmarks(
|
| 32 |
+
annotated_image,
|
| 33 |
+
pose_landmarks_proto,
|
| 34 |
+
solutions.pose.POSE_CONNECTIONS,
|
| 35 |
+
solutions.drawing_styles.get_default_pose_landmarks_style())
|
| 36 |
+
return annotated_image
|
| 37 |
+
|
| 38 |
+
def load_model(input_img, pos, confidence):
|
| 39 |
+
"""
|
| 40 |
+
Carga el modelo de detecci贸n de pose y lo aplica a la imagen de entrada.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
input_img (np.ndarray): La imagen de entrada.
|
| 44 |
+
pos (float): Confianza m铆nima para la detecci贸n de poses.
|
| 45 |
+
confidence (int): N煤mero m谩ximo de poses a detectar.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
np.ndarray: Imagen anotada con los resultados de la detecci贸n de poses.
|
| 49 |
+
"""
|
| 50 |
+
# Configuraci贸n del objeto PoseLandmarker con par谩metros pr贸pios
|
| 51 |
+
options = vision.PoseLandmarkerOptions(
|
| 52 |
+
base_options=base_options,
|
| 53 |
+
num_poses=confidence,
|
| 54 |
+
min_pose_detection_confidence=pos,
|
| 55 |
+
min_pose_presence_confidence=pos,
|
| 56 |
+
min_tracking_confidence=pos)
|
| 57 |
+
detector = vision.PoseLandmarker.create_from_options(options)
|
| 58 |
+
|
| 59 |
+
rgb_frame = mp.Image(image_format=mp.ImageFormat.SRGB, data=input_img)
|
| 60 |
+
|
| 61 |
+
# Detectar los puntos de referencia de la pose en la imagen de entrada
|
| 62 |
+
detection_result = detector.detect(rgb_frame)
|
| 63 |
+
|
| 64 |
+
# Procesar el resultado de la detecci贸n y visualizarlo
|
| 65 |
+
annotated_image = draw_landmarks_on_image(rgb_frame.numpy_view(), detection_result)
|
| 66 |
+
|
| 67 |
+
return annotated_image
|