Spaces:
Sleeping
Sleeping
File size: 3,376 Bytes
4045a73 865ddb0 8eb20e0 7e5988c 8eb20e0 4045a73 d707478 8eb20e0 22e427c 8eb20e0 4045a73 1688517 4045a73 aad4b49 ef9c3f5 5a5aa5d ef9c3f5 8cbbe53 4045a73 8eb20e0 46ec255 8eb20e0 46ec255 8eb20e0 46ec255 ebac8ff 46ec255 ebac8ff 46ec255 8eb20e0 31fd0a0 8eb20e0 12f50e0 8eb20e0 4045a73 71ec9ee |
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 |
import gradio as gr
import subprocess
import os
from PIL import Image
def resize_image(image_path, target_height, output_path):
# Open the image file
with Image.open(image_path) as img:
# Calculate the ratio to resize the image to the target height
ratio = target_height / float(img.size[1])
# Calculate the new width based on the aspect ratio
new_width = int(float(img.size[0]) * ratio)
# Resize the image
resized_img = img.resize((new_width, target_height), Image.LANCZOS)
# Save the resized image
resized_img.save(output_path)
return output_path
def generate(image, prompt, seed):
print(image, prompt, seed)
image_path = os.path.splitext(image)[0]
image_name = os.path.basename(image_path)
resized=resize_image(image, 512, f"output/{image_name}.jpg")
print(f"IMAGE NAME: {image_name}")
command = f"python handrefiner.py --input_img {resized} --out_dir output --strength 0.55 --weights models/inpaint_depth_control.ckpt --prompt '{prompt}' --seed {seed}"
try:
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output_path = 'output'
print("Output:", result.stdout)
print(output_path)
# List all files and directories in the given directory
contents = os.listdir("output")
# Print the contents
for item in contents:
print(item)
return f"output/{image_name}_0.jpg"
except subprocess.CalledProcessError as e:
print("Error:", e.stderr)
return None
css="""
#col-container{
max-width: 860px;
margin: 0 auto;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML("""
<h2 style="text-align: center;">
HandRefiner
</h2>
<p style="text-align: center;">
Refining Malformed Hands in Generated Images by Diffusion-based Conditional Inpainting <br />
For demo purpose, every input images are resized to 512 height ratio
</p>
<p style="margin:12px auto;display: flex;justify-content: center;">
<a href="https://huggingface.co/spaces/fffiloni/HandRefiner?duplicate=true"><img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg.svg" alt="Duplicate this Space"></a>
</p>
""")
with gr.Row():
with gr.Column():
image = gr.Image(type='filepath')
gr.Examples(
examples = [
"examples/IMG_1050.jpeg",
"examples/IMG_1051.jpeg",
"examples/IMG_1052.jpeg",
"examples/IMG_1053.jpeg"
],
inputs = [image]
)
textbox = gr.Textbox(show_label=False, value="a person facing the camera, making a hand gesture, indoor")
seed = gr.Slider(label="Seed", minimum=0, maximum=1000000, value=643534)
button = gr.Button()
output_image = gr.Image(show_label=False, type="filepath", interactive=False)
button.click(fn=generate, inputs=[image, textbox, seed], outputs=[output_image])
demo.queue(max_size=10).launch(debug=True) |