Spaces:
Sleeping
Sleeping
vk
commited on
Commit
·
d426818
1
Parent(s):
efcde92
first commit
Browse files- .gitattributes +5 -0
- app.py +37 -0
- requirements.txt +4 -0
- test-images/test1.jpg +3 -0
- test-images/test2.jpg +3 -0
- test-images/test3.jpg +3 -0
- utils.py +98 -0
- yolo11n-pose.pt +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
yolo11n-pose.pt filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
test-images/ filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
test-images/test2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
test-images/test3.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
test-images/test1.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
from utils import PlotPose
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def show_example(path):
|
| 9 |
+
return cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_response(input_img):
|
| 13 |
+
result = model(input_img)
|
| 14 |
+
pred_kpts = result[0].keypoints.data.numpy()
|
| 15 |
+
out_img=result[0].orig_img.copy()
|
| 16 |
+
plotpose.lw=int(input_img.shape[0]*input_img.shape[1]*0.00001)
|
| 17 |
+
if len(pred_kpts)>0:
|
| 18 |
+
for i in range(len(pred_kpts)):
|
| 19 |
+
out_img = plotpose.plot(out_img, pred_kpts[i])
|
| 20 |
+
|
| 21 |
+
return out_img.astype(np.uint8)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
model = YOLO("yolo11n-pose.pt")
|
| 26 |
+
plotpose=PlotPose()
|
| 27 |
+
model.to("cpu")
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
cache_examples=False,
|
| 30 |
+
fn=get_response,
|
| 31 |
+
inputs=[gr.Image(type="numpy")], # Accepts image input
|
| 32 |
+
examples=[[show_example('test-images/test1.jpg')],[show_example('test-images/test2.jpg')],[show_example('test-images/test3.jpg')]],
|
| 33 |
+
outputs=[gr.Image(type="numpy")],
|
| 34 |
+
title="Posture-Estimation-YOLO11",
|
| 35 |
+
description="Upload images to predict human keypoint")
|
| 36 |
+
|
| 37 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv_python
|
| 2 |
+
ultralytics
|
| 3 |
+
gradio
|
| 4 |
+
|
test-images/test1.jpg
ADDED
|
Git LFS Details
|
test-images/test2.jpg
ADDED
|
Git LFS Details
|
test-images/test3.jpg
ADDED
|
Git LFS Details
|
utils.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics.utils.plotting import colors
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class PlotPose:
|
| 7 |
+
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.skeleton = [
|
| 10 |
+
[16, 14],
|
| 11 |
+
[14, 12],
|
| 12 |
+
[17, 15],
|
| 13 |
+
[15, 13],
|
| 14 |
+
[12, 13],
|
| 15 |
+
[6, 12],
|
| 16 |
+
[7, 13],
|
| 17 |
+
[6, 7],
|
| 18 |
+
[6, 8],
|
| 19 |
+
[7, 9],
|
| 20 |
+
[8, 10],
|
| 21 |
+
[9, 11],
|
| 22 |
+
[2, 3],
|
| 23 |
+
[1, 2],
|
| 24 |
+
[1, 3],
|
| 25 |
+
[2, 4],
|
| 26 |
+
[3, 5],
|
| 27 |
+
[4, 6],
|
| 28 |
+
[5, 7],
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
self.limb_color = colors.pose_palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]]
|
| 32 |
+
self.kpt_color = colors.pose_palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def plot(self,im,
|
| 36 |
+
kpts,
|
| 37 |
+
shape=(640, 640),
|
| 38 |
+
radius=None,
|
| 39 |
+
kpt_line=True,
|
| 40 |
+
conf_thres= 0.25,):
|
| 41 |
+
|
| 42 |
+
"""
|
| 43 |
+
Plot keypoints on the image.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
im (numpy array): img to plot keypoints
|
| 47 |
+
kpts (numpy arrayr): Keypoints, shape [17, 3] (x, y, confidence).
|
| 48 |
+
shape (tuple, optional): Image shape (h, w).
|
| 49 |
+
radius (int, optional): Keypoint radius.
|
| 50 |
+
kpt_line (bool, optional): Draw lines between keypoints.
|
| 51 |
+
conf_thres (float, optional): Confidence threshold.
|
| 52 |
+
kpt_color (tuple, optional): Keypoint color (B, G, R).
|
| 53 |
+
|
| 54 |
+
Note:
|
| 55 |
+
- `kpt_line=True` currently only supports human pose plotting.
|
| 56 |
+
- Modifies self.im in-place.
|
| 57 |
+
- If self.pil is True, converts image to numpy array and back to PIL.
|
| 58 |
+
"""
|
| 59 |
+
self.lw = 4
|
| 60 |
+
radius = radius if radius is not None else self.lw
|
| 61 |
+
nkpt, ndim = kpts.shape
|
| 62 |
+
is_pose = nkpt == 17 and ndim in {2, 3}
|
| 63 |
+
kpt_line &= is_pose # `kpt_line=True` for now only supports human pose plotting
|
| 64 |
+
for i, k in enumerate(kpts):
|
| 65 |
+
color_k = self.kpt_color[i].tolist()
|
| 66 |
+
# color_k = kpt_color or (kpt_color[i].tolist() if is_pose else colors(i))
|
| 67 |
+
x_coord, y_coord = k[0], k[1]
|
| 68 |
+
if x_coord % shape[1] != 0 and y_coord % shape[0] != 0:
|
| 69 |
+
if len(k) == 3:
|
| 70 |
+
conf = k[2]
|
| 71 |
+
if conf < conf_thres:
|
| 72 |
+
continue
|
| 73 |
+
cv2.circle(im, (int(x_coord), int(y_coord)), radius, 255, -1, lineType=cv2.LINE_AA)
|
| 74 |
+
|
| 75 |
+
if kpt_line:
|
| 76 |
+
ndim = kpts.shape[-1]
|
| 77 |
+
for i, sk in enumerate(self.skeleton):
|
| 78 |
+
pos1 = (int(kpts[(sk[0] - 1), 0]), int(kpts[(sk[0] - 1), 1]))
|
| 79 |
+
pos2 = (int(kpts[(sk[1] - 1), 0]), int(kpts[(sk[1] - 1), 1]))
|
| 80 |
+
if ndim == 3:
|
| 81 |
+
conf1 = kpts[(sk[0] - 1), 2]
|
| 82 |
+
conf2 = kpts[(sk[1] - 1), 2]
|
| 83 |
+
if conf1 < conf_thres or conf2 < conf_thres:
|
| 84 |
+
continue
|
| 85 |
+
if pos1[0] % shape[1] == 0 or pos1[1] % shape[0] == 0 or pos1[0] < 0 or pos1[1] < 0:
|
| 86 |
+
continue
|
| 87 |
+
if pos2[0] % shape[1] == 0 or pos2[1] % shape[0] == 0 or pos2[0] < 0 or pos2[1] < 0:
|
| 88 |
+
continue
|
| 89 |
+
cv2.line(
|
| 90 |
+
im,
|
| 91 |
+
pos1,
|
| 92 |
+
pos2,
|
| 93 |
+
self.limb_color[i].tolist(),
|
| 94 |
+
thickness=int(self.lw ),
|
| 95 |
+
lineType=cv2.LINE_AA,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return im
|
yolo11n-pose.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:869e83fcdffdc7371fa4e34cd8e51c838cc729571d1635e5141e3075e9319dc0
|
| 3 |
+
size 6255593
|