File size: 5,214 Bytes
a3d6c18 9d63ece a3d6c18 cd51d32 a3d6c18 cd51d32 a3d6c18 cd51d32 a3d6c18 9d63ece a3d6c18 9d63ece a3d6c18 cd51d32 a3d6c18 cd51d32 9d63ece cd51d32 a3d6c18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
from pathlib import Path
from typing import Optional, Union
from PIL import Image, ImageDraw
from torch import ge
from internals.util.commons import download_file, download_image, safe_index
from internals.util.config import get_root_dir
from models.pose.body import Body
class PoseDetector:
# __det_model = "https://comic-assets.s3.ap-south-1.amazonaws.com/models/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
__pose_model = (
"https://comic-assets.s3.ap-south-1.amazonaws.com/models/body_pose_model.pth"
)
__loaded = False
def load(self):
if self.__loaded:
return
pose_path = Path.home() / ".cache" / self.__pose_model.split("/")[-1]
download_file(self.__pose_model, pose_path)
self.body_estimation = Body(str(pose_path))
self.__loaded = True
def transform(
self,
image: Union[str, Image.Image],
width: int,
height: int,
client_coordinates: Optional[dict],
) -> Image.Image:
"Infer pose coordinates from image, map head and body coordinates to infered ones, create pose"
if type(image) is str:
image = download_image(image)
infer_coordinates = self.infer(image, width, height)
if client_coordinates and client_coordinates["candidate"]:
client_coordinates = self.resize_coordinates(
client_coordinates, 384, 384, width, height
)
infer_coordinates = self.map_head_to_body(
client_coordinates, infer_coordinates
)
print(infer_coordinates)
return self.create_pose(infer_coordinates, width, height)
def resize_coordinates(
self, data: dict, ori_width, ori_height, new_width, new_height
):
points = data["candidate"]
new_points = []
if new_width > new_height:
ori_min = min(ori_width, ori_height)
new_min = min(new_width, new_height)
else:
ori_min = max(ori_width, ori_height)
new_min = max(new_width, new_height)
for _, pair in enumerate(points):
x = pair[0] * new_min / ori_min
y = pair[1] * new_min / ori_min
new_points.append([x, y])
return {"candidate": new_points, "subset": data["subset"]}
def create_pose(self, data: dict, width: int, height: int) -> Image.Image:
image = Image.new("RGB", (width, height), "black")
draw = ImageDraw.Draw(image)
points: list = data["candidate"]
for pair in self.__pose_logical_map:
xy = safe_index(points, pair[0] - 1)
x1y1 = safe_index(points, pair[1] - 1)
if xy and x1y1:
draw.line(
(xy[0], xy[1], x1y1[0], x1y1[1]),
fill=pair[2],
width=4,
)
for i, point in enumerate(points):
x = point[0]
y = point[1]
draw.ellipse((x - 3, y - 3, x + 3, y + 3), fill=self.__points_color[i])
return image
def infer(self, image: Union[str, Image.Image], width, height) -> dict:
candidate = []
subset = []
if type(image) == str:
image = download_image(image)
image = image.resize((width, height))
candidate, subset = self.body_estimation.__call__(image)
candidate = candidate.tolist()
subset = subset.tolist()
candidate = [item[:2] for item in candidate]
return {"candidate": candidate[:18], "subset": subset[:18]}
def map_head_to_body(
self, client_coordinates: dict, infer_coordinates: dict
) -> dict:
client_points = client_coordinates["candidate"]
infer_points = infer_coordinates["candidate"]
c_neck = client_points[1]
i_neck = infer_points[1]
dx = i_neck[0] - c_neck[0]
dy = i_neck[1] - c_neck[1]
for i in range(2, 15):
point = client_points[i - 1]
infer_points[i - 1] = [point[0] + dx, point[1] + dy]
return {"candidate": infer_points, "subset": infer_coordinates["subset"]}
def __convert_keypoints(self, keypoints):
return [keypoints[i] for i in self.__kim]
__kim = [0, 17, 6, 8, 10, 5, 7, 9, 12, 14, 16, 11, 13, 15, 2, 1, 4, 3]
__pose_logical_map = [
[1, 2, "#000099"],
[1, 16, "#330099"],
[1, 15, "#660099"],
[16, 18, "#990099"],
[15, 17, "#990066"],
[2, 3, "#990001"],
[2, 6, "#993301"],
[3, 4, "#996502"],
[4, 5, "#999900"],
[6, 7, "#669900"],
[7, 8, "#349900"],
[2, 9, "#009900"],
[2, 12, "#009999"],
[9, 10, "#009966"],
[10, 11, "#009966"],
[12, 13, "#006699"],
[13, 14, "#013399"],
]
__points_color = [
"#ff0000",
"#ff5600",
"#ffaa01",
"#ffff00",
"#aaff03",
"#53ff00",
"#03ff00",
"#03ff55",
"#03ffaa",
"#03ffff",
"#05aaff",
"#0055ff",
"#0000ff",
"#5500ff",
"#aa00ff",
"#ff00aa",
"#ff00ff",
"#ff0055",
]
|