Spaces:
Runtime error
Runtime error
init space
Browse files- .gitignore +3 -0
- app.py +59 -0
- examples/01.jpg +0 -0
- examples/02.jpg +0 -0
- examples/03.jpg +0 -0
- pose_estimator.py +55 -0
- requerimets.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
*.pyc
|
2 |
+
__pycache__
|
3 |
+
.idea/
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from human_pose_estimator import PoseEstimator
|
5 |
+
|
6 |
+
from pose_estimator import rect
|
7 |
+
|
8 |
+
pose_estimator = PoseEstimator("cpu")
|
9 |
+
|
10 |
+
|
11 |
+
def get_box(image):
|
12 |
+
image_box, _ = rect(pose_estimator, image)
|
13 |
+
return image_box
|
14 |
+
|
15 |
+
|
16 |
+
def predict(img: np.ndarray):
|
17 |
+
|
18 |
+
poses, _, _ = pose_estimator.get_poses(img, 512)
|
19 |
+
|
20 |
+
for pose in poses:
|
21 |
+
pose.draw(img)
|
22 |
+
cv2.rectangle(img, (pose.bbox[0], pose.bbox[1]),
|
23 |
+
(pose.bbox[0] + pose.bbox[2], pose.bbox[1] + pose.bbox[3]), (0, 255, 0))
|
24 |
+
|
25 |
+
return img
|
26 |
+
|
27 |
+
|
28 |
+
footer = r"""
|
29 |
+
<center>
|
30 |
+
<b>
|
31 |
+
Demo for <a href='https://github.com/Daniil-Osokin/lightweight-human-pose-estimation.pytorch'>Lightweight OpenPose</a>
|
32 |
+
</b>
|
33 |
+
</center>
|
34 |
+
"""
|
35 |
+
|
36 |
+
with gr.Blocks(title="OpenPose") as app:
|
37 |
+
gr.HTML("<center><h1>Human Pose Estimation Pytorch</h1></center>")
|
38 |
+
gr.HTML("<center><h3>Real-time 2D Multi-Person Pose Estimation on CPU: Lightweight OpenPose</h3></center>")
|
39 |
+
with gr.Row(equal_height=False):
|
40 |
+
with gr.Column():
|
41 |
+
input_img = gr.Image(type="numpy", label="Input image")
|
42 |
+
# input_img = gr.Video(source="webcam")
|
43 |
+
run_btn = gr.Button(variant="primary")
|
44 |
+
with gr.Column():
|
45 |
+
output_img = gr.Image(type="pil", label="Output image")
|
46 |
+
gr.ClearButton(components=[input_img, output_img], variant="stop")
|
47 |
+
|
48 |
+
run_btn.click(predict, [input_img], [output_img])
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
blobs = [[f"examples/{x:02d}.jpg"] for x in range(1, 4)]
|
52 |
+
examples = gr.Dataset(components=[input_img], samples=blobs)
|
53 |
+
examples.click(lambda x: x[0], [examples], [input_img])
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
gr.HTML(footer)
|
57 |
+
|
58 |
+
app.launch(share=False, debug=True, show_error=True)
|
59 |
+
app.queue()
|
examples/01.jpg
ADDED
![]() |
examples/02.jpg
ADDED
![]() |
examples/03.jpg
ADDED
![]() |
pose_estimator.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from human_pose_estimator.modules.pose import Pose
|
4 |
+
|
5 |
+
|
6 |
+
def rect(estimator, image, height_size=512):
|
7 |
+
|
8 |
+
num_keypoints = Pose.num_kpts
|
9 |
+
|
10 |
+
img = cv2.imread(image, cv2.IMREAD_COLOR)
|
11 |
+
|
12 |
+
_, pose_entries, all_keypoints = estimator.get_poses(img, height_size)
|
13 |
+
|
14 |
+
rects = []
|
15 |
+
for n in range(len(pose_entries)):
|
16 |
+
if len(pose_entries[n]) == 0:
|
17 |
+
continue
|
18 |
+
pose_keypoints = np.ones((num_keypoints, 2), dtype=np.int32) * -1
|
19 |
+
|
20 |
+
valid_keypoints = []
|
21 |
+
for kpt_id in range(num_keypoints):
|
22 |
+
if pose_entries[n][kpt_id] != -1.0: # keypoint was found
|
23 |
+
pose_keypoints[kpt_id, 0] = int(all_keypoints[int(pose_entries[n][kpt_id]), 0])
|
24 |
+
pose_keypoints[kpt_id, 1] = int(all_keypoints[int(pose_entries[n][kpt_id]), 1])
|
25 |
+
valid_keypoints.append([pose_keypoints[kpt_id, 0], pose_keypoints[kpt_id, 1]])
|
26 |
+
valid_keypoints = np.array(valid_keypoints)
|
27 |
+
|
28 |
+
if pose_entries[n][10] != -1.0 or pose_entries[n][13] != -1.0:
|
29 |
+
pmin = valid_keypoints.min(0)
|
30 |
+
pmax = valid_keypoints.max(0)
|
31 |
+
|
32 |
+
center = (0.5 * (pmax[:2] + pmin[:2])).astype(np.int32)
|
33 |
+
radius = int(0.65 * max(pmax[0] - pmin[0], pmax[1] - pmin[1]))
|
34 |
+
elif pose_entries[n][10] == -1.0 and pose_entries[n][13] == -1.0 and pose_entries[n][8] != -1.0 and \
|
35 |
+
pose_entries[n][11] != -1.0:
|
36 |
+
# if leg is missing, use pelvis to get cropping
|
37 |
+
center = (0.5 * (pose_keypoints[8] + pose_keypoints[11])).astype(np.int32)
|
38 |
+
radius = int(1.45 * np.sqrt(((center[None, :] - valid_keypoints) ** 2).sum(1)).max(0))
|
39 |
+
center[1] += int(0.05 * radius)
|
40 |
+
else:
|
41 |
+
center = np.array([img.shape[1] // 2, img.shape[0] // 2])
|
42 |
+
radius = max(img.shape[1] // 2, img.shape[0] // 2)
|
43 |
+
|
44 |
+
x1 = center[0] - radius
|
45 |
+
y1 = center[1] - radius
|
46 |
+
|
47 |
+
rects.append([x1, y1, 2 * radius, 2 * radius])
|
48 |
+
|
49 |
+
for (x, y, w, h) in rects:
|
50 |
+
cv2.rectangle(img, (x, y),
|
51 |
+
(x + w, y + h),
|
52 |
+
(0, 0, 255), 2)
|
53 |
+
|
54 |
+
return img, rects
|
55 |
+
|
requerimets.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=1.4.0
|
2 |
+
numpy
|
3 |
+
opencv-python
|
4 |
+
human-pose-estimator
|