Spaces:
Runtime error
Runtime error
Refactoring
Browse files- app.py +8 -7
- helpers/processor.py +77 -77
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import wget
|
4 |
import subprocess
|
5 |
-
subprocess.call(['pip', 'install', 'git+https://github.com/facebookresearch/detectron2@main#subdirectory=projects/DensePose'])
|
6 |
from helpers.processor import TextureProcessor
|
7 |
|
8 |
def image_processing(person_img, model_img):
|
@@ -12,12 +12,13 @@ def load_model(current_path):
|
|
12 |
data_path = os.path.join(current_path, 'data')
|
13 |
if not os.path.isdir(data_path):
|
14 |
os.mkdir(data_path)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
current_path = os.getcwd()
|
23 |
load_model(current_path)
|
|
|
2 |
import os
|
3 |
import wget
|
4 |
import subprocess
|
5 |
+
#subprocess.call(['pip', 'install', 'git+https://github.com/facebookresearch/detectron2@main#subdirectory=projects/DensePose'])
|
6 |
from helpers.processor import TextureProcessor
|
7 |
|
8 |
def image_processing(person_img, model_img):
|
|
|
12 |
data_path = os.path.join(current_path, 'data')
|
13 |
if not os.path.isdir(data_path):
|
14 |
os.mkdir(data_path)
|
15 |
+
items_to_load = {
|
16 |
+
'config.yaml': 'https://raw.githubusercontent.com/facebookresearch/detectron2/main/projects/DensePose/configs/densepose_rcnn_R_50_FPN_WC1M_s1x.yaml',
|
17 |
+
'weights.pkl': 'https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_WC1M_s1x/217144516/model_final_48a9d9.pkl',
|
18 |
+
'Base-DensePose-RCNN-FPN.yaml': 'https://raw.githubusercontent.com/facebookresearch/detectron2/main/projects/DensePose/configs/Base-DensePose-RCNN-FPN.yaml'
|
19 |
+
}
|
20 |
+
for filename, url in items_to_load.items():
|
21 |
+
wget.download(url, os.path.join(data_path, filename))
|
22 |
|
23 |
current_path = os.getcwd()
|
24 |
load_model(current_path)
|
helpers/processor.py
CHANGED
@@ -75,107 +75,107 @@ class TextureProcessor:
|
|
75 |
return image_vis
|
76 |
|
77 |
def parse_iuv(self, result):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
|
84 |
def parse_bbox(self, result):
|
85 |
-
|
86 |
|
87 |
def interpolate_tex(self, tex):
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
|
105 |
def concat_textures(self, array):
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
|
115 |
def get_texture(self, im, iuv, bbox, tex_part_size=200):
|
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 |
def create_iuv(self, results, image):
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
|
164 |
def get_config(self, config_fpath, model_fpath):
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
|
173 |
def execute(self, image):
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
|
180 |
def execute_on_outputs(self, context: Dict[str, Any], outputs: Instances):
|
181 |
result = {}
|
|
|
75 |
return image_vis
|
76 |
|
77 |
def parse_iuv(self, result):
|
78 |
+
i = result['pred_densepose'][0].labels.cpu().numpy().astype(float)
|
79 |
+
uv = (result['pred_densepose'][0].uv.cpu().numpy() * 255.0).astype(float)
|
80 |
+
iuv = np.stack((uv[1, :, :], uv[0, :, :], i))
|
81 |
+
iuv = np.transpose(iuv, (1, 2, 0))
|
82 |
+
return iuv
|
83 |
|
84 |
def parse_bbox(self, result):
|
85 |
+
return result['pred_boxes_XYXY'][0].cpu().numpy()
|
86 |
|
87 |
def interpolate_tex(self, tex):
|
88 |
+
valid_mask = np.array((tex.sum(0) != 0) * 1, dtype='uint8')
|
89 |
+
radius_increase = 10
|
90 |
+
kernel = np.ones((radius_increase, radius_increase), np.uint8)
|
91 |
+
dilated_mask = cv2.dilate(valid_mask, kernel, iterations=1)
|
92 |
+
invalid_region = 1 - valid_mask
|
93 |
+
actual_part_max = tex.max()
|
94 |
+
actual_part_min = tex.min()
|
95 |
+
actual_part_uint = np.array(
|
96 |
+
(tex - actual_part_min) / (actual_part_max - actual_part_min) * 255, dtype='uint8')
|
97 |
+
actual_part_uint = cv2.inpaint(actual_part_uint.transpose((1, 2, 0)), invalid_region, 1,
|
98 |
+
cv2.INPAINT_TELEA).transpose((2, 0, 1))
|
99 |
+
actual_part = (actual_part_uint / 255.0) * \
|
100 |
+
(actual_part_max - actual_part_min) + actual_part_min
|
101 |
+
actual_part = actual_part * dilated_mask
|
102 |
+
|
103 |
+
return actual_part
|
104 |
|
105 |
def concat_textures(self, array):
|
106 |
+
texture = []
|
107 |
+
for i in range(4):
|
108 |
+
tmp = array[6 * i]
|
109 |
+
for j in range(6 * i + 1, 6 * i + 6):
|
110 |
+
tmp = np.concatenate((tmp, array[j]), axis=1)
|
111 |
+
texture = tmp if len(texture) == 0 else np.concatenate(
|
112 |
+
(texture, tmp), axis=0)
|
113 |
+
return texture
|
114 |
|
115 |
def get_texture(self, im, iuv, bbox, tex_part_size=200):
|
116 |
+
im = im.transpose(2, 1, 0) / 255
|
117 |
+
image_w, image_h = im.shape[1], im.shape[2]
|
118 |
+
bbox[2] = bbox[2] - bbox[0]
|
119 |
+
bbox[3] = bbox[3] - bbox[1]
|
120 |
+
x, y, w, h = [int(v) for v in bbox]
|
121 |
+
bg = np.zeros((image_h, image_w, 3))
|
122 |
+
bg[y:y + h, x:x + w, :] = iuv
|
123 |
+
iuv = bg
|
124 |
+
iuv = iuv.transpose((2, 1, 0))
|
125 |
+
i, u, v = iuv[2], iuv[1], iuv[0]
|
126 |
|
127 |
+
n_parts = 22
|
128 |
+
texture = np.zeros((n_parts, 3, tex_part_size, tex_part_size))
|
129 |
|
130 |
+
for part_id in range(1, n_parts + 1):
|
131 |
+
generated = np.zeros((3, tex_part_size, tex_part_size))
|
132 |
|
133 |
+
x, y = u[i == part_id], v[i == part_id]
|
134 |
|
135 |
+
tex_u_coo = (x * (tex_part_size - 1) / 255).astype(int)
|
136 |
+
tex_v_coo = (y * (tex_part_size - 1) / 255).astype(int)
|
137 |
|
138 |
+
tex_u_coo = np.clip(tex_u_coo, 0, tex_part_size - 1)
|
139 |
+
tex_v_coo = np.clip(tex_v_coo, 0, tex_part_size - 1)
|
140 |
|
141 |
+
for channel in range(3):
|
142 |
+
generated[channel][tex_v_coo,
|
143 |
+
tex_u_coo] = im[channel][i == part_id]
|
144 |
|
145 |
+
if np.sum(generated) > 0:
|
146 |
+
generated = self.interpolate_tex(generated)
|
147 |
|
148 |
+
texture[part_id - 1] = generated[:, ::-1, :]
|
149 |
|
150 |
+
tex_concat = np.zeros((24, tex_part_size, tex_part_size, 3))
|
151 |
+
for i in range(texture.shape[0]):
|
152 |
+
tex_concat[i] = texture[i].transpose(2, 1, 0)
|
153 |
+
tex = self.concat_textures(tex_concat)
|
154 |
|
155 |
+
return tex
|
156 |
|
157 |
def create_iuv(self, results, image):
|
158 |
+
iuv = self.parse_iuv(results)
|
159 |
+
bbox = self.parse_bbox(results)
|
160 |
+
uv_texture = self.get_texture(image, iuv, bbox)
|
161 |
+
uv_texture = uv_texture.transpose([1, 0, 2])
|
162 |
+
return uv_texture
|
163 |
|
164 |
def get_config(self, config_fpath, model_fpath):
|
165 |
+
cfg = get_cfg()
|
166 |
+
add_densepose_config(cfg)
|
167 |
+
cfg.merge_from_file(config_fpath)
|
168 |
+
cfg.MODEL.WEIGHTS = model_fpath
|
169 |
+
cfg.MODEL.DEVICE = 'cpu'
|
170 |
+
cfg.freeze()
|
171 |
+
return cfg
|
172 |
|
173 |
def execute(self, image):
|
174 |
+
context = {'results': []}
|
175 |
+
with torch.no_grad():
|
176 |
+
outputs = self.predictor(image)['instances']
|
177 |
+
self.execute_on_outputs(context, outputs)
|
178 |
+
return context['results']
|
179 |
|
180 |
def execute_on_outputs(self, context: Dict[str, Any], outputs: Instances):
|
181 |
result = {}
|