Spaces:
Sleeping
Sleeping
Create PoseAnnotator.py
Browse files- PoseAnnotator.py +63 -0
PoseAnnotator.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import supervision as sv
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import urllib
|
| 8 |
+
|
| 9 |
+
# load pre-trained vision model
|
| 10 |
+
model2 = YOLO("yolo11s-pose.pt")
|
| 11 |
+
|
| 12 |
+
# build pose annotator
|
| 13 |
+
def pose_annotate(image:str, pose_annotator:str) -> Image.Image:
|
| 14 |
+
"""
|
| 15 |
+
Args:
|
| 16 |
+
image: the path to the image file
|
| 17 |
+
pose_annotator: the type of annotator to use
|
| 18 |
+
Returns:
|
| 19 |
+
annotated image
|
| 20 |
+
"""
|
| 21 |
+
# load the input image
|
| 22 |
+
image = cv2.imread(image)
|
| 23 |
+
|
| 24 |
+
# run object detection on the image
|
| 25 |
+
result = model2(image)[0]
|
| 26 |
+
|
| 27 |
+
# detect keypoints in image
|
| 28 |
+
key_points = sv.KeyPoints.from_ultralytics(result)
|
| 29 |
+
|
| 30 |
+
if pose_annotator == "Vertex":
|
| 31 |
+
vertex_annotator = sv.VertexAnnotator(
|
| 32 |
+
color=sv.Color.GREEN,
|
| 33 |
+
radius=10
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
annotated_frame = vertex_annotator.annotate(
|
| 37 |
+
scene=image.copy(),
|
| 38 |
+
key_points=key_points
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
elif pose_annotator == "Edge":
|
| 42 |
+
edge_annotator = sv.EdgeAnnotator(
|
| 43 |
+
color=sv.Color.GREEN,
|
| 44 |
+
thickness=5
|
| 45 |
+
)
|
| 46 |
+
annotated_frame = edge_annotator.annotate(
|
| 47 |
+
scene=image.copy(),
|
| 48 |
+
key_points=key_points
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
elif pose_annotator == "Vertexlabel":
|
| 52 |
+
vertex_label_annotator = sv.VertexLabelAnnotator(
|
| 53 |
+
color=sv.Color.GREEN,
|
| 54 |
+
text_color=sv.Color.BLACK,
|
| 55 |
+
border_radius=5
|
| 56 |
+
)
|
| 57 |
+
annotated_frame = vertex_label_annotator.annotate(
|
| 58 |
+
scene=image.copy(),
|
| 59 |
+
key_points=key_points
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return annotated_frame
|
| 63 |
+
|