Spaces:
Sleeping
Sleeping
Commit
·
31cf66c
1
Parent(s):
a843446
added folder for hand pose estimation
Browse files- README.md +5 -1
- src/mediapipe_pose/utils.py +115 -0
README.md
CHANGED
|
@@ -13,4 +13,8 @@ useful: https://github.com/ruslanmv/How-to-Sync-Hugging-Face-Spaces-with-a-GitHu
|
|
| 13 |
|
| 14 |
https://stackoverflow.com/questions/5620525/git-pushing-to-two-repos-in-one-command
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
https://stackoverflow.com/questions/5620525/git-pushing-to-two-repos-in-one-command
|
| 15 |
|
| 16 |
+
https://dev.to/0xkoji/sync-github-repo-and-hugging-face-space-repo-with-github-actions-3ca1
|
| 17 |
+
|
| 18 |
+
https://discuss.huggingface.co/t/how-to-sync-hugging-face-model-commits-with-github/149599
|
| 19 |
+
|
| 20 |
+
https://github.com/huggingface/huggingface_hub/issues/534
|
src/mediapipe_pose/utils.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @title Default title text
|
| 2 |
+
from mediapipe.framework.formats import detection_pb2
|
| 3 |
+
from mediapipe.framework.formats import location_data_pb2
|
| 4 |
+
from mediapipe.framework.formats import landmark_pb2
|
| 5 |
+
|
| 6 |
+
from mediapipe.python.solutions.drawing_utils import DrawingSpec, _normalized_to_pixel_coordinates
|
| 7 |
+
|
| 8 |
+
import math
|
| 9 |
+
from typing import List, Mapping, Optional, Tuple, Union
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
PRESENCE_THRESHOLD = 0.5
|
| 13 |
+
RGB_CHANNELS = 3
|
| 14 |
+
BLACK_COLOR = (0, 0, 0)
|
| 15 |
+
RED_COLOR = (0, 0, 255)
|
| 16 |
+
GREEN_COLOR = (0, 128, 0)
|
| 17 |
+
BLUE_COLOR = (255, 0, 0)
|
| 18 |
+
VISIBILITY_THRESHOLD = 0.5
|
| 19 |
+
|
| 20 |
+
def draw_landmarks(
|
| 21 |
+
image: np.ndarray,
|
| 22 |
+
landmark_list: landmark_pb2.NormalizedLandmarkList,
|
| 23 |
+
connections: Optional[List[Tuple[int, int]]] = None,
|
| 24 |
+
landmark_drawing_spec: Union[DrawingSpec,
|
| 25 |
+
Mapping[int, DrawingSpec]] = DrawingSpec(
|
| 26 |
+
color=RED_COLOR),
|
| 27 |
+
connection_drawing_spec: Union[DrawingSpec,
|
| 28 |
+
Mapping[Tuple[int, int],
|
| 29 |
+
DrawingSpec]] = DrawingSpec()):
|
| 30 |
+
"""Draws the landmarks and the connections on the image.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
image: A three channel RGB image represented as numpy ndarray.
|
| 34 |
+
landmark_list: A normalized landmark list proto message to be annotated on
|
| 35 |
+
the image.
|
| 36 |
+
connections: A list of landmark index tuples that specifies how landmarks to
|
| 37 |
+
be connected in the drawing.
|
| 38 |
+
landmark_drawing_spec: A DrawingSpec object that specifies the landmarks'
|
| 39 |
+
drawing settings such as color, line thickness, and circle radius.
|
| 40 |
+
connection_drawing_spec: A DrawingSpec object that specifies the
|
| 41 |
+
connections' drawing settings such as color and line thickness.
|
| 42 |
+
|
| 43 |
+
Raises:
|
| 44 |
+
ValueError: If one of the followings:
|
| 45 |
+
a) If the input image is not three channel RGB.
|
| 46 |
+
b) If any connetions contain invalid landmark index.
|
| 47 |
+
"""
|
| 48 |
+
if not landmark_list:
|
| 49 |
+
return
|
| 50 |
+
if image.shape[2] != RGB_CHANNELS:
|
| 51 |
+
raise ValueError('Input image must contain three channel rgb data.')
|
| 52 |
+
image_rows, image_cols, _ = image.shape
|
| 53 |
+
idx_to_coordinates = {}
|
| 54 |
+
for idx, landmark in enumerate(landmark_list.landmark):
|
| 55 |
+
if ((landmark.HasField('visibility') and
|
| 56 |
+
landmark.visibility < VISIBILITY_THRESHOLD) or
|
| 57 |
+
(landmark.HasField('presence') and
|
| 58 |
+
landmark.presence < PRESENCE_THRESHOLD)):
|
| 59 |
+
continue
|
| 60 |
+
landmark_px = _normalized_to_pixel_coordinates(landmark.x, landmark.y,
|
| 61 |
+
image_cols, image_rows)
|
| 62 |
+
if landmark_px:
|
| 63 |
+
idx_to_coordinates[idx] = landmark_px
|
| 64 |
+
if connections:
|
| 65 |
+
num_landmarks = len(landmark_list.landmark)
|
| 66 |
+
# Draws the connections if the start and end landmarks are both visible.
|
| 67 |
+
for connection in connections:
|
| 68 |
+
start_idx = connection[0]
|
| 69 |
+
end_idx = connection[1]
|
| 70 |
+
if not (0 <= start_idx < num_landmarks and 0 <= end_idx < num_landmarks):
|
| 71 |
+
raise ValueError(f'Landmark index is out of range. Invalid connection '
|
| 72 |
+
f'from landmark #{start_idx} to landmark #{end_idx}.')
|
| 73 |
+
if start_idx in idx_to_coordinates and end_idx in idx_to_coordinates:
|
| 74 |
+
if isinstance(connection_drawing_spec, Mapping):
|
| 75 |
+
cv2.line(image, idx_to_coordinates[start_idx],
|
| 76 |
+
idx_to_coordinates[end_idx],
|
| 77 |
+
connection_drawing_spec[connection].color,
|
| 78 |
+
connection_drawing_spec[connection].thickness)
|
| 79 |
+
else:
|
| 80 |
+
cv2.line(image, idx_to_coordinates[start_idx],
|
| 81 |
+
idx_to_coordinates[end_idx], connection_drawing_spec.color,
|
| 82 |
+
connection_drawing_spec.thickness)
|
| 83 |
+
|
| 84 |
+
# Draws landmark points after finishing the connection lines, which is
|
| 85 |
+
# aesthetically better.
|
| 86 |
+
for idx, landmark_px in idx_to_coordinates.items():
|
| 87 |
+
if isinstance(landmark_drawing_spec, Mapping):
|
| 88 |
+
cv2.circle(img=image, center=(int(landmark_px[0]) - landmark_drawing_spec[idx].circle_radius//2, int(landmark_px[1])), radius=landmark_drawing_spec[idx].circle_radius//2, color=landmark_drawing_spec[idx].color, thickness=landmark_drawing_spec[idx].thickness)
|
| 89 |
+
cv2.circle(img=image, center=(int(landmark_px[0]) + landmark_drawing_spec[idx].circle_radius//2, int(landmark_px[1])), radius=landmark_drawing_spec[idx].circle_radius//2, color=landmark_drawing_spec[idx].color, thickness=landmark_drawing_spec[idx].thickness)
|
| 90 |
+
|
| 91 |
+
# Triangle (bottom of heart)
|
| 92 |
+
pts = np.array([
|
| 93 |
+
[int(landmark_px[0]) - landmark_drawing_spec[idx].circle_radius, int(landmark_px[1])],
|
| 94 |
+
[int(landmark_px[0]) + landmark_drawing_spec[idx].circle_radius, int(landmark_px[1])],
|
| 95 |
+
[int(landmark_px[0]), landmark_px[1] + landmark_drawing_spec[idx].circle_radius*2]
|
| 96 |
+
], np.int32).reshape((-1, 1, 2))
|
| 97 |
+
|
| 98 |
+
cv2.fillPoly(image, [pts], landmark_drawing_spec[idx].color)
|
| 99 |
+
|
| 100 |
+
else:
|
| 101 |
+
# Two circles (top lobes of heart)
|
| 102 |
+
cv2.circle(img=image, center=(int(landmark_px[0]) - landmark_drawing_spec.circle_radius//2, int(landmark_px[1])), radius=landmark_drawing_spec.circle_radius//2, color=landmark_drawing_spec.color, thickness=landmark_drawing_spec.thickness)
|
| 103 |
+
cv2.circle(img=image, center=(int(landmark_px[0]) + landmark_drawing_spec.circle_radius//2, int(landmark_px[1])), radius=landmark_drawing_spec.circle_radius//2, color=landmark_drawing_spec.color, thickness=landmark_drawing_spec.thickness)
|
| 104 |
+
|
| 105 |
+
# Triangle (bottom of heart)
|
| 106 |
+
pts = np.array([
|
| 107 |
+
[int(landmark_px[0]) - landmark_drawing_spec.circle_radius, int(landmark_px[1])],
|
| 108 |
+
[int(landmark_px[0]) + landmark_drawing_spec.circle_radius, int(landmark_px[1])],
|
| 109 |
+
[int(landmark_px[0]), landmark_px[1] + landmark_drawing_spec.circle_radius*2]
|
| 110 |
+
], np.int32).reshape((-1, 1, 2))
|
| 111 |
+
|
| 112 |
+
cv2.fillPoly(image, [pts], landmark_drawing_spec.color)
|
| 113 |
+
|
| 114 |
+
#cv2.circle(image, landmark_px, landmark_drawing_spec.circle_radius,
|
| 115 |
+
#landmark_drawing_spec.color, landmark_drawing_spec.thickness)
|