File size: 9,912 Bytes
81e69dc ec53eb5 81e69dc 38fc6e1 81e69dc |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import spaces
import gradio as gr
import subprocess
from PIL import Image
import json
import os
import time
import mp_box
import draw_landmarks68
import landmarks68_utils
import io
import numpy as np
from glibvision.cv2_utils import pil_to_bgr_image,bgr_to_rgb
from gradio_utils import save_image,save_buffer,clear_old_files ,read_file
from close_eyes import process_close_eyes_image # little bit better
from open_mouth import process_open_mouth # I'm truly sorry, but I must admit the code is very confusing.comment still written in Japanese
'''
Face landmark detection based Face Detection.
https://ai.google.dev/edge/mediapipe/solutions/vision/face_landmarker
from model card
https://storage.googleapis.com/mediapipe-assets/MediaPipe%20BlazeFace%20Model%20Card%20(Short%20Range).pdf
Licensed Apache License, Version 2.0
Train with google's dataset(more detail see model card)
'''
#@spaces.GPU(duration=120)
def process_images(image,eyelid_thick=1,eyelid_blur=9,inpaint_radius=10,inpaint_blur=30,mask_dilate=10,dilate_blur=10,
open_size_y=8,inside_layer_low_depth=False,hole_image_name="dark01",
make_animation=True,eyes_duration=200,mouth_duration=40,
progress=gr.Progress(track_tqdm=True)):
clear_old_files()
if image == None:
raise gr.Error("Need Image")
progress(0, desc="Start Making Animation")
boxes,mp_image,face_landmarker_result = mp_box.mediapipe_to_box(image)
annotated_image,bbox,landmark_points = draw_landmarks68.draw_landmarks_on_image(image,face_landmarker_result)
landmark_list = draw_landmarks68.convert_to_landmark_group_json(landmark_points)
galleries = []
progressed = 0
progress_step = 0.8/open_size_y
animations = []
np_image = pil_to_bgr_image(image)
if make_animation:
start_index = 0
else:
start_index = open_size_y-1
for i in range(start_index,open_size_y):
mouth_opened = process_open_mouth(np_image,landmark_list,0,i,True,inside_layer_low_depth,0,hole_image_name+".jpg")
animations.append(mouth_opened)
mouth_opened_path = save_image(mouth_opened)
galleries.append((mouth_opened_path,f"mouth-opened {i}"))
progressed+=progress_step
progress(progressed)
if make_animation:
np_image = pil_to_bgr_image(animations[0])# TODO option
eyes_closed_np,mask_np = process_close_eyes_image(np_image,landmark_list,eyelid_thick,eyelid_blur,inpaint_radius,inpaint_blur,mask_dilate,dilate_blur)
eyes_closed = Image.fromarray(bgr_to_rgb(eyes_closed_np))
eyes_closed_path = save_image(eyes_closed)
galleries.append((eyes_closed_path,"eyes-closed"))
eyes_closed_mask_path = save_image(Image.fromarray(mask_np))
galleries.append((eyes_closed_mask_path,"eyes-closed-mask"))
duractions = [mouth_duration]*len(animations)*2+[eyes_duration]
if make_animation:
animations = animations + animations[::-1]+[eyes_closed]
output_buffer = io.BytesIO()
animations[0].save(output_buffer,
save_all=True,
append_images=animations[1:],
duration=duractions,
loop=0,
format='WebP')
webp_path = save_buffer(output_buffer)
#galleries.append((webp_path,"animation"))
return webp_path,galleries
css="""
#col-left {
margin: 0 auto;
max-width: 640px;
}
#col-right {
margin: 0 auto;
max-width: 640px;
}
.grid-container {
display: flex;
align-items: center;
justify-content: center;
gap:10px
}
.image {
width: 128px;
height: 128px;
object-fit: cover;
}
.text {
font-size: 16px;
}
"""
#css=css,
with gr.Blocks(css=css, elem_id="demo-container") as demo:
with gr.Column():
gr.HTML(read_file("demo_header.html"))
gr.HTML(read_file("demo_tools.html"))
with gr.Row():
with gr.Column():
image = gr.Image(height=800,sources=['upload','clipboard'],image_mode='RGB',elem_id="image_upload", type="pil", label="Upload")
with gr.Row(elem_id="prompt-container", equal_height=False):
with gr.Row():
btn = gr.Button("Create Closed-eye and Mouth-opened", elem_id="run_button",variant="primary")
with gr.Accordion(label="Eyes-Closed Advanced Settings", open=False):
with gr.Row( equal_height=True):
eyelid_thick = gr.Slider(
label="Eyelid thick",
minimum=0,
maximum=20,
step=1,
value=1)
eyelid_blur = gr.Slider(
label="Eyelid blur",
minimum=0,
maximum=30,
step=1,
value=7)
with gr.Row( equal_height=True):
inpaint_radius = gr.Slider(
label="Inpaint Radius",
minimum=1,
maximum=20,
step=1,
value=10,info="incresing make smooth but slow")
inpaint_blur = gr.Slider(
label="Inpaint blur",
minimum=0,
maximum=30,
step=1,
value=20)
with gr.Row( equal_height=True):
mask_dilate = gr.Slider(
label="Mask dilate",
minimum=0,
maximum=20,
step=1,
value=10)
dilate_blur = gr.Slider(
label="dilate blur",
minimum=0,
maximum=20,
step=1,
value=10)
with gr.Row( equal_height=True):
eyes_duration = gr.Slider(
label="Eyeclosed animation duration",
minimum=1,
maximum=500,
step=1,
value=200)
with gr.Accordion(label="Mouth-Opened Advanced Settings", open=False):
with gr.Row( equal_height=True):
make_animation = gr.Checkbox(label="animation",value=True,info="take long time if open-size is large")
open_size_y = gr.Slider(
label="Open Size",
minimum=1,
maximum=40,
step=1,
value=8,info="Large size is for img2img/inpaint")
inside_layer_low_depth=gr.Checkbox(label="Inner Layer Low",value=False,info="if value >20 check on better result")
hole_image_name=gr.Dropdown(label="inner image name",choices=["dark01","black","mid01","mid02"],value="dark01",info="if you use img2img black is better")
with gr.Row( equal_height=True):
mouth_duration = gr.Slider(
label="mouhtopen animation duration",info="per frame",
minimum=1,
maximum=500,
step=1,
value=40)
with gr.Column():
animation_out = gr.Image(height=760,label="Animation", elem_id="output-animation")
image_out = gr.Gallery(label="Output", elem_id="output-img",preview=True)
btn.click(fn=process_images, inputs=[image,eyelid_thick,eyelid_blur,inpaint_radius,inpaint_blur,mask_dilate,dilate_blur,
open_size_y,inside_layer_low_depth,hole_image_name,make_animation,
eyes_duration,mouth_duration],outputs=[animation_out,image_out] ,api_name='infer')
gr.Examples(
examples =[
["examples/00003245_00.jpg","examples/00003245_00.webp"],
["examples/00002062.jpg","examples/00002062.webp"],
["examples/00100265.jpg","examples/00100265.webp"],
["examples/00824006.jpg","examples/00824006.webp"],
["examples/00824008.jpg","examples/00824008.webp"],
["examples/02316230.jpg","examples/02316230.webp"],
["examples/00825000.jpg","examples/00825000.webp"],
["examples/00826007.jpg","examples/00826007.webp"],
["examples/00827009.jpg","examples/00827009.webp"],
["examples/00828003.jpg","examples/00828003.webp"],
],
#examples =["examples/00003245_00.jpg","examples/00002062.jpg","examples/00100265.jpg","examples/00824006.jpg","examples/00824008.jpg",
# "examples/00825000.jpg","examples/00826007.jpg","examples/00827009.jpg","examples/00828003.jpg",],
inputs=[image,animation_out],examples_per_page=5
)
gr.HTML(read_file("demo_footer.html"))
if __name__ == "__main__":
demo.launch()
|