Spaces:
Runtime error
Runtime error
File size: 4,873 Bytes
33000da |
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 |
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
try:
os.system("pip install --upgrade torch==1.11.0+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html")
except Exception as e:
print(e)
from pydoc import describe
from huggingface_hub import hf_hub_download
import gradio as gr
import os
from datetime import datetime
from PIL import Image
import torch
import torchvision
import skimage
import paddlehub
import numpy as np
from lib.options import BaseOptions
from apps.crop_img import process_img
from apps.eval import Evaluator
from types import SimpleNamespace
import trimesh
import glob
print(
"torch: ", torch.__version__,
"\ntorchvision: ", torchvision.__version__,
"\nskimage:", skimage.__version__
)
print("EnV", os.environ)
net_C = hf_hub_download("radames/PIFu-upright-standing", filename="net_C")
net_G = hf_hub_download("radames/PIFu-upright-standing", filename="net_G")
opt = BaseOptions()
opts = opt.parse_to_dict()
opts['batch_size'] = 1
opts['mlp_dim'] = [257, 1024, 512, 256, 128, 1]
opts['mlp_dim_color'] = [513, 1024, 512, 256, 128, 3]
opts['num_stack'] = 4
opts['num_hourglass'] = 2
opts['resolution'] = 128
opts['hg_down'] = 'ave_pool'
opts['norm'] = 'group'
opts['norm_color'] = 'group'
opts['load_netG_checkpoint_path'] = net_G
opts['load_netC_checkpoint_path'] = net_C
opts['results_path'] = "./results"
opts['name'] = "spaces_demo"
opts = SimpleNamespace(**opts)
print("Params", opts)
evaluator = Evaluator(opts)
bg_remover_model = paddlehub.Module(name="U2Net")
def process(img_path):
base = os.path.basename(img_path)
img_name = os.path.splitext(base)[0]
print("\n\n\nStarting Process", datetime.now())
print("image name", img_name)
img_raw = Image.open(img_path).convert('RGB')
img = img_raw.resize(
(512, int(512 * img_raw.size[1] / img_raw.size[0])),
Image.Resampling.LANCZOS)
try:
# remove background
print("Removing Background")
masks = bg_remover_model.Segmentation(
images=[np.array(img)],
paths=None,
batch_size=1,
input_size=320,
output_dir='./PIFu/inputs',
visualization=False)
mask = masks[0]["mask"]
front = masks[0]["front"]
except Exception as e:
print(e)
print("Aliging mask with input training image")
print("Not aligned", front.shape, mask.shape)
img_new, msk_new = process_img(front, mask)
print("Aligned", img_new.shape, msk_new.shape)
try:
time = datetime.now()
data = evaluator.load_image_from_memory(img_new, msk_new, img_name)
print("Evaluating via PIFu", time)
evaluator.eval(data, True)
print("Success Evaluating via PIFu", datetime.now() - time)
result_path = f'./{opts.results_path}/{opts.name}/result_{img_name}'
except Exception as e:
print("Error evaluating via PIFu", e)
try:
mesh = trimesh.load(result_path + '.obj')
# flip mesh
mesh.apply_transform([[-1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, 1]])
mesh.export(file_obj=result_path + '.glb')
result_gltf = result_path + '.glb'
return [result_gltf, result_gltf]
except Exception as e:
print("error generating MESH", e)
examples = sorted(glob.glob('examples/*.png'))
description = '''
# PIFu Clothed Human Digitization
### PIFu: Pixel-Aligned Implicit Function for High-Resolution Clothed Human Digitization
<base target="_blank">
This is a demo for <a href="https://github.com/shunsukesaito/PIFu" target="_blank"> PIFu model </a>.
The pre-trained model has the following warning:
> Warning: The released model is trained with mostly upright standing scans with weak perspectie projection and the pitch angle of 0 degree. Reconstruction quality may degrade for images highly deviated from trainining data.
**The inference takes about 180seconds for a new image.**
<details>
<summary>More</summary>
#### Image Credits
* Julien and Clem
* [StyleGAN Humans](https://huggingface.co/spaces/hysts/StyleGAN-Human)
* [Renderpeople: Dennis](https://renderpeople.com)
#### More
* https://phorhum.github.io/
* https://github.com/yuliangxiu/icon
* https://shunsukesaito.github.io/PIFuHD/
</details>
'''
iface = gr.Interface(
fn=process,
description=description,
inputs=gr.Image(type="filepath", label="Input Image"),
outputs=[
gr.Model3D(
clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
gr.File(label="Download 3D Model")
],
examples=examples,
allow_flagging="never",
cache_examples=True
)
if __name__ == "__main__":
iface.launch(debug=True, enable_queue=False)
|