Spaces:
Runtime error
Runtime error
Commit
·
9cf6de3
1
Parent(s):
7f6b110
gradio 5.27.0
Browse files- app.py +53 -22
- prepare_examples.py +12 -12
app.py
CHANGED
|
@@ -444,23 +444,28 @@ def get_target_anno(img, keypts):
|
|
| 444 |
|
| 445 |
return img, target_pose, target_cond, keypts, gr.update(), True
|
| 446 |
|
| 447 |
-
def visualize_ref(ref):
|
| 448 |
if ref is None:
|
| 449 |
return None
|
| 450 |
|
| 451 |
# from user or from example
|
| 452 |
-
h, w = ref["background"].shape[:2]
|
| 453 |
-
if ref["layers"][0].sum() == 0:
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
else:
|
|
|
|
|
|
|
| 459 |
from_example = False
|
|
|
|
|
|
|
| 460 |
|
| 461 |
# inpaint mask
|
| 462 |
if from_example:
|
| 463 |
-
inpaint_mask = ref["background"][:, :, -1]
|
|
|
|
| 464 |
inpainted = inpaint_mask.copy()
|
| 465 |
inpaint_mask = cv2.resize(
|
| 466 |
inpaint_mask, opts.image_size, interpolation=cv2.INTER_AREA
|
|
@@ -483,6 +488,20 @@ def visualize_ref(ref):
|
|
| 483 |
gr.Warning("Run botton not enabled? Please try again.", duration=10)
|
| 484 |
return img, inpaint_mask
|
| 485 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
def get_kps(img, keypoints, side: Literal["right", "left"], evt: gr.SelectData):
|
| 487 |
if keypoints is None:
|
| 488 |
keypoints = [[], []]
|
|
@@ -639,8 +658,11 @@ def sample_diff(ref_cond, target_cond, target_keypts, num_gen, seed, cfg):
|
|
| 639 |
return results, results_pose
|
| 640 |
|
| 641 |
@spaces_120_fn
|
| 642 |
-
def ready_sample(img_cropped, inpaint_mask, keypts, keypts_np):
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
| 644 |
sam_predictor.set_image(img)
|
| 645 |
if keypts is None and keypts_np is not None:
|
| 646 |
keypts = keypts_np
|
|
@@ -956,6 +978,7 @@ def fix_clear_all():
|
|
| 956 |
42,
|
| 957 |
3.0,
|
| 958 |
10,
|
|
|
|
| 959 |
)
|
| 960 |
|
| 961 |
def enable_component(image1, image2):
|
|
@@ -1138,18 +1161,18 @@ fix_example_kpts = [
|
|
| 1138 |
["bad_hands/7_kpts.png", 3.0, 42],
|
| 1139 |
]
|
| 1140 |
fix_example_all = [
|
| 1141 |
-
["bad_hands/1.jpg", "bad_hands/1_composite.png", "bad_hands/1_kpts.png", 3.0, 1224],
|
| 1142 |
-
["bad_hands/3.jpg", "bad_hands/3_composite.png", "bad_hands/3_kpts.png", 1.0, 42],
|
| 1143 |
-
["bad_hands/4.jpg", "bad_hands/4_composite.png", "bad_hands/4_kpts.png", 2.0, 42],
|
| 1144 |
-
["bad_hands/5.jpg", "bad_hands/5_composite.png", "bad_hands/5_kpts.png", 3.0, 42],
|
| 1145 |
-
["bad_hands/6.jpg", "bad_hands/6_composite.png", "bad_hands/6_kpts.png", 3.0, 1348],
|
| 1146 |
-
["bad_hands/7.jpg", "bad_hands/7_composite.png", "bad_hands/7_kpts.png", 3.0, 42],
|
| 1147 |
]
|
| 1148 |
for i in range(len(fix_example_kpts)):
|
| 1149 |
npy_path = fix_example_kpts[i][0].replace("_kpts.png", ".npy")
|
| 1150 |
fix_example_kpts[i].append(npy_path)
|
| 1151 |
for i in range(len(fix_example_all)):
|
| 1152 |
-
npy_path = fix_example_all[i][
|
| 1153 |
fix_example_all[i].append(npy_path)
|
| 1154 |
|
| 1155 |
custom_css = """
|
|
@@ -1199,6 +1222,10 @@ custom_css = """
|
|
| 1199 |
#fix_examples_all table tr td:nth-child(6) {
|
| 1200 |
display: none !important;
|
| 1201 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1202 |
#fix_examples_all table tr:first-child {
|
| 1203 |
display: none !important;
|
| 1204 |
}
|
|
@@ -1906,6 +1933,7 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
|
|
| 1906 |
fix_target_cond = gr.State(value=None)
|
| 1907 |
fix_latent = gr.State(value=None)
|
| 1908 |
fix_inpaint_latent = gr.State(value=None)
|
|
|
|
| 1909 |
|
| 1910 |
# more options
|
| 1911 |
with gr.Accordion(label="More options", open=False):
|
|
@@ -2162,9 +2190,9 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
|
|
| 2162 |
with gr.Row():
|
| 2163 |
gr.Examples(
|
| 2164 |
fix_example_all,
|
| 2165 |
-
inputs=[fix_crop, fix_ref, fix_kp_all, fix_cfg, fix_seed, fix_kpts_path],
|
| 2166 |
examples_per_page=20,
|
| 2167 |
-
postprocess=
|
| 2168 |
elem_id="fix_examples_all",
|
| 2169 |
)
|
| 2170 |
with gr.Row():
|
|
@@ -2196,10 +2224,12 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
|
|
| 2196 |
gr.HTML(tut1_example)
|
| 2197 |
|
| 2198 |
# listeners
|
|
|
|
|
|
|
| 2199 |
fix_crop.change(stash_original, fix_crop, fix_original) # fix_original: (real_H, real_W, 3)
|
| 2200 |
fix_crop.change(stay_crop, [fix_crop, fix_crop_coord], [fix_crop_coord, fix_ref])
|
| 2201 |
fix_crop.select(process_crop, [fix_crop, fix_crop_coord], [fix_crop_coord, fix_ref, fix_crop])
|
| 2202 |
-
fix_ref.change(visualize_ref, [fix_ref], [fix_img, fix_inpaint_mask])
|
| 2203 |
fix_img.change(lambda x: x, [fix_img], [fix_kp_right])
|
| 2204 |
fix_img.change(lambda x: x, [fix_img], [fix_kp_left])
|
| 2205 |
fix_ref.change(
|
|
@@ -2266,7 +2296,7 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
|
|
| 2266 |
fix_kpts_np.change(enable_component, [fix_inpaint_mask, fix_kpts_np], fix_run)
|
| 2267 |
fix_run.click(
|
| 2268 |
ready_sample,
|
| 2269 |
-
[fix_ref, fix_inpaint_mask, fix_kpts, fix_kpts_np],
|
| 2270 |
[
|
| 2271 |
fix_ref_cond,
|
| 2272 |
fix_target_cond,
|
|
@@ -2324,6 +2354,7 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
|
|
| 2324 |
fix_seed,
|
| 2325 |
fix_cfg,
|
| 2326 |
fix_quality,
|
|
|
|
| 2327 |
],
|
| 2328 |
)
|
| 2329 |
fix_clear.click(
|
|
|
|
| 444 |
|
| 445 |
return img, target_pose, target_cond, keypts, gr.update(), True
|
| 446 |
|
| 447 |
+
def visualize_ref(ref, ex_mask):
|
| 448 |
if ref is None:
|
| 449 |
return None
|
| 450 |
|
| 451 |
# from user or from example
|
| 452 |
+
# h, w = ref["background"].shape[:2]
|
| 453 |
+
# if ref["layers"][0].sum() == 0:
|
| 454 |
+
# if ref["background"][:, :, -1].sum() == h * w * 255:
|
| 455 |
+
# from_example = False
|
| 456 |
+
# else:
|
| 457 |
+
# from_example = True
|
| 458 |
+
# else:
|
| 459 |
+
# from_example = False
|
| 460 |
+
if ex_mask is None:
|
| 461 |
from_example = False
|
| 462 |
+
else:
|
| 463 |
+
from_example = True
|
| 464 |
|
| 465 |
# inpaint mask
|
| 466 |
if from_example:
|
| 467 |
+
# inpaint_mask = ref["background"][:, :, -1]
|
| 468 |
+
inpaint_mask = (np.all(ex_mask > 245, axis=-1)).astype(np.uint8)*128 + 64
|
| 469 |
inpainted = inpaint_mask.copy()
|
| 470 |
inpaint_mask = cv2.resize(
|
| 471 |
inpaint_mask, opts.image_size, interpolation=cv2.INTER_AREA
|
|
|
|
| 488 |
gr.Warning("Run botton not enabled? Please try again.", duration=10)
|
| 489 |
return img, inpaint_mask
|
| 490 |
|
| 491 |
+
def make_composite(img, mask):
|
| 492 |
+
if mask is None:
|
| 493 |
+
return gr.update()
|
| 494 |
+
mask = (np.all(mask > 245, axis=-1)).astype(np.uint8)*128 + 64
|
| 495 |
+
# composite = np.concatenate((img[..., :3], mask[..., None]), axis=-1)
|
| 496 |
+
composite = {
|
| 497 |
+
"background": img[..., :3],
|
| 498 |
+
"layers": [
|
| 499 |
+
mask,
|
| 500 |
+
],
|
| 501 |
+
"composite": np.concatenate((img[..., :3], mask[..., None]), axis=-1),
|
| 502 |
+
}
|
| 503 |
+
return composite
|
| 504 |
+
|
| 505 |
def get_kps(img, keypoints, side: Literal["right", "left"], evt: gr.SelectData):
|
| 506 |
if keypoints is None:
|
| 507 |
keypoints = [[], []]
|
|
|
|
| 658 |
return results, results_pose
|
| 659 |
|
| 660 |
@spaces_120_fn
|
| 661 |
+
def ready_sample(img_cropped, img_original, ex_mask, inpaint_mask, keypts, keypts_np):
|
| 662 |
+
if ex_mask is None:
|
| 663 |
+
img = cv2.resize(img_cropped["background"][..., :3], opts.image_size, interpolation=cv2.INTER_AREA)
|
| 664 |
+
else:
|
| 665 |
+
img = cv2.resize(img_original[..., :3], opts.image_size, interpolation=cv2.INTER_AREA)
|
| 666 |
sam_predictor.set_image(img)
|
| 667 |
if keypts is None and keypts_np is not None:
|
| 668 |
keypts = keypts_np
|
|
|
|
| 978 |
42,
|
| 979 |
3.0,
|
| 980 |
10,
|
| 981 |
+
None
|
| 982 |
)
|
| 983 |
|
| 984 |
def enable_component(image1, image2):
|
|
|
|
| 1161 |
["bad_hands/7_kpts.png", 3.0, 42],
|
| 1162 |
]
|
| 1163 |
fix_example_all = [
|
| 1164 |
+
["bad_hands/1.jpg", "bad_hands/1_composite.png", "bad_hands/1_mask.jpg", "bad_hands/1_kpts.png", 3.0, 1224],
|
| 1165 |
+
["bad_hands/3.jpg", "bad_hands/3_composite.png", "bad_hands/3_mask.jpg", "bad_hands/3_kpts.png", 1.0, 42],
|
| 1166 |
+
["bad_hands/4.jpg", "bad_hands/4_composite.png", "bad_hands/4_mask.jpg", "bad_hands/4_kpts.png", 2.0, 42],
|
| 1167 |
+
["bad_hands/5.jpg", "bad_hands/5_composite.png", "bad_hands/5_mask.jpg", "bad_hands/5_kpts.png", 3.0, 42],
|
| 1168 |
+
["bad_hands/6.jpg", "bad_hands/6_composite.png", "bad_hands/6_mask.jpg", "bad_hands/6_kpts.png", 3.0, 1348],
|
| 1169 |
+
["bad_hands/7.jpg", "bad_hands/7_composite.png", "bad_hands/7_mask.jpg", "bad_hands/7_kpts.png", 3.0, 42],
|
| 1170 |
]
|
| 1171 |
for i in range(len(fix_example_kpts)):
|
| 1172 |
npy_path = fix_example_kpts[i][0].replace("_kpts.png", ".npy")
|
| 1173 |
fix_example_kpts[i].append(npy_path)
|
| 1174 |
for i in range(len(fix_example_all)):
|
| 1175 |
+
npy_path = fix_example_all[i][3].replace("_kpts.png", ".npy")
|
| 1176 |
fix_example_all[i].append(npy_path)
|
| 1177 |
|
| 1178 |
custom_css = """
|
|
|
|
| 1222 |
#fix_examples_all table tr td:nth-child(6) {
|
| 1223 |
display: none !important;
|
| 1224 |
}
|
| 1225 |
+
#fix_examples_all table tr th:nth-child(7),
|
| 1226 |
+
#fix_examples_all table tr td:nth-child(7) {
|
| 1227 |
+
display: none !important;
|
| 1228 |
+
}
|
| 1229 |
#fix_examples_all table tr:first-child {
|
| 1230 |
display: none !important;
|
| 1231 |
}
|
|
|
|
| 1933 |
fix_target_cond = gr.State(value=None)
|
| 1934 |
fix_latent = gr.State(value=None)
|
| 1935 |
fix_inpaint_latent = gr.State(value=None)
|
| 1936 |
+
fix_ex_mask = gr.Image(value=None, visible=False)
|
| 1937 |
|
| 1938 |
# more options
|
| 1939 |
with gr.Accordion(label="More options", open=False):
|
|
|
|
| 2190 |
with gr.Row():
|
| 2191 |
gr.Examples(
|
| 2192 |
fix_example_all,
|
| 2193 |
+
inputs=[fix_crop, fix_ref, fix_ex_mask, fix_kp_all, fix_cfg, fix_seed, fix_kpts_path],
|
| 2194 |
examples_per_page=20,
|
| 2195 |
+
postprocess=True,
|
| 2196 |
elem_id="fix_examples_all",
|
| 2197 |
)
|
| 2198 |
with gr.Row():
|
|
|
|
| 2224 |
gr.HTML(tut1_example)
|
| 2225 |
|
| 2226 |
# listeners
|
| 2227 |
+
# fix_ex_mask.change(make_composite, [fix_crop, fix_ex_mask], fix_ref)
|
| 2228 |
+
fix_crop.input(lambda x: gr.update(None), fix_crop, fix_ex_mask)
|
| 2229 |
fix_crop.change(stash_original, fix_crop, fix_original) # fix_original: (real_H, real_W, 3)
|
| 2230 |
fix_crop.change(stay_crop, [fix_crop, fix_crop_coord], [fix_crop_coord, fix_ref])
|
| 2231 |
fix_crop.select(process_crop, [fix_crop, fix_crop_coord], [fix_crop_coord, fix_ref, fix_crop])
|
| 2232 |
+
fix_ref.change(visualize_ref, [fix_ref, fix_ex_mask], [fix_img, fix_inpaint_mask])
|
| 2233 |
fix_img.change(lambda x: x, [fix_img], [fix_kp_right])
|
| 2234 |
fix_img.change(lambda x: x, [fix_img], [fix_kp_left])
|
| 2235 |
fix_ref.change(
|
|
|
|
| 2296 |
fix_kpts_np.change(enable_component, [fix_inpaint_mask, fix_kpts_np], fix_run)
|
| 2297 |
fix_run.click(
|
| 2298 |
ready_sample,
|
| 2299 |
+
[fix_ref, fix_original, fix_ex_mask, fix_inpaint_mask, fix_kpts, fix_kpts_np],
|
| 2300 |
[
|
| 2301 |
fix_ref_cond,
|
| 2302 |
fix_target_cond,
|
|
|
|
| 2354 |
fix_seed,
|
| 2355 |
fix_cfg,
|
| 2356 |
fix_quality,
|
| 2357 |
+
fix_ex_mask,
|
| 2358 |
],
|
| 2359 |
)
|
| 2360 |
fix_clear.click(
|
prepare_examples.py
CHANGED
|
@@ -85,15 +85,15 @@ def visualize_hand(all_joints, img, side=["right", "left"], n_avail_joints=21):
|
|
| 85 |
# print(f"Saved composite image {masked_pth.replace('_mask.jpg', '_composite.png')}")
|
| 86 |
|
| 87 |
'''visualize keypoint example'''
|
| 88 |
-
data_dir = "bad_hands"
|
| 89 |
-
kpts_paths = sorted(glob.glob(os.path.join(data_dir, "*.npy")))
|
| 90 |
-
for kpts_pth in kpts_paths:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 85 |
# print(f"Saved composite image {masked_pth.replace('_mask.jpg', '_composite.png')}")
|
| 86 |
|
| 87 |
'''visualize keypoint example'''
|
| 88 |
+
# data_dir = "bad_hands"
|
| 89 |
+
# kpts_paths = sorted(glob.glob(os.path.join(data_dir, "*.npy")))
|
| 90 |
+
# for kpts_pth in kpts_paths:
|
| 91 |
+
# img_pth = kpts_pth.replace(".npy", ".jpg")
|
| 92 |
+
# kpts = np.load(kpts_pth)
|
| 93 |
+
# img = np.array(Image.open(img_pth))
|
| 94 |
+
# h, w = img.shape[:2]
|
| 95 |
+
# kpts = kpts / np.array([256,256]) * np.array([w, h])
|
| 96 |
+
# kpts_vis = visualize_hand(kpts, img)
|
| 97 |
+
# save_path = kpts_pth.replace(".npy", "_kpts.png")
|
| 98 |
+
# kpts_vis = Image.fromarray(kpts_vis).save(save_path)
|
| 99 |
+
# print(f"Saved {save_path}")
|