File size: 3,660 Bytes
26a9456
d7f3620
b9371bb
61bd4aa
f251776
a6e209b
 
638a4e8
214cbbd
 
959a59f
a3e4fb1
 
d7f3620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c05e7c
 
 
 
 
 
 
 
45bc16e
d7f3620
 
 
 
 
fb797db
300b53a
214cbbd
 
26a9456
214cbbd
d7f3620
 
 
d7b3b72
 
 
 
 
45bc16e
d7b3b72
26a9456
2bd9163
b439c96
d7f3620
 
 
 
 
 
 
 
 
 
 
 
45bc16e
26a9456
d7f3620
eec8b99
 
d7f3620
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
#This code is largely based on: https://huggingface.co/spaces/diffusers/controlnet-3d-pose
import gradio as gr
from PIL import Image, ImageFilter, ImageOps
from io import BytesIO
import numpy as np 
import base64
import cv2
import os
import uuid
uid=uuid.uuid4()
canvas_html = '<div style="height:50%"><pose-maker/></div>'
low_threshold = 100
high_threshold = 200
load_js = """
async () => {
  const url = "https://huggingface.co/datasets/mishig/gradio-components/raw/main/mannequinAll.js"
  fetch(url)
    .then(res => res.text())
    .then(text => {
      const script = document.createElement('script');
      script.type = "module"
      script.src = URL.createObjectURL(new Blob([text], { type: 'application/javascript' }));
      document.head.appendChild(script);
    });
}
"""
get_js_image = """
async (canvas, prompt) => {
  const poseMakerEl = document.querySelector("pose-maker");
  const imgBase64 = poseMakerEl.captureScreenshotDepthMap();
  return [imgBase64, prompt]
}
"""
js_change_rotation_axis = """
async (axis) => {
  const poseMakerEl = document.querySelector("pose-maker");
  poseMakerEl.changeRotationAxis(axis);
}
"""
js_pose_template = """
async (pose) => {
  const poseMakerEl = document.querySelector("pose-maker");
  poseMakerEl.setPose(pose);
}
"""
def get_canny_filter(image):
    if not isinstance(image, np.ndarray):
        image = np.array(image) 
    image = cv2.Canny(image, low_threshold, high_threshold)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    canny_image = Image.fromarray(image)
    return canny_image
def generate_images(canvas,output_type):        
    base64_img = canvas
    image_data = base64.b64decode(base64_img.split(',')[1])
    input_img = Image.open(BytesIO(image_data)).convert(
        'RGB').resize((512, 512))
    input_img = input_img.filter(ImageFilter.GaussianBlur(radius=2))
    input_img = get_canny_filter(input_img) if output_type == "Canny" else input_img
    #input_img = ImageOps.invert(input_img)
    input_img.save(f"{uid}-pose.png")
    out = os.path.abspath(f"{uid}-pose.png")
    out_url = f'https://omnibus-model-mover.hf.space/file={out}'       
    return f"{uid}-pose.png",f"{uid}-pose.png",out_url
def placeholder_fn(axis):
    pass
with gr.Blocks() as b:
    with gr.Row():
        canvas = gr.HTML(canvas_html, elem_id="canvas_html", visible=True)
        with gr.Column():
            rotation_axis = gr.Radio(choices=["x", "y", "z"], value="x", label="Joint rotation axis")
            pose_template = gr.Radio(choices=["regular", "ballet", "handstand", "split", "kick", "chilling"], value="regular", label="Pose template")
            output_type = gr.Radio(choices=["Depth", "Canny"], value="Depth", label="Image Output")
            run_button = gr.Button("Generate")
            out_text = gr.Textbox(label="Image URL")
            out_file=gr.File()
            out_im = gr.Image(type='filepath',show_share_button=True)
    
    rotation_axis.change(fn=placeholder_fn,
                            inputs=[rotation_axis],
                            outputs=[],
                            queue=False,
                            _js=js_change_rotation_axis)
    pose_template.change(fn=placeholder_fn,
                            inputs=[pose_template],
                            outputs=[],
                            queue=False,
                            _js=js_pose_template)
    run_button.click(fn=generate_images,
                     inputs=[canvas,output_type],
                     outputs=[out_im,out_file,out_text],
                     _js=get_js_image)
    b.load(None,None,None,_js=load_js)

b.launch()