import gradio as gr import cv2 import numpy as np from annotator.util import resize_image, HWC3 DESCRIPTION = '# ControlNet v1.1 Annotators (that runs on cpu only)' DESCRIPTION += '\n
This app generates Control Image for Mochi Diffusion's ControlNet.
' DESCRIPTION += '\nHEIC image is not converted. Please use PNG or JPG image.
' model_canny = None def canny(img, res, l, h): img = resize_image(HWC3(img), res) global model_canny if model_canny is None: from annotator.canny import CannyDetector model_canny = CannyDetector() result = model_canny(img, l, h) return [result] model_hed = None def hed(img, res): img = resize_image(HWC3(img), res) global model_hed if model_hed is None: from annotator.hed import HEDdetector model_hed = HEDdetector() result = model_hed(img) return [result] model_pidi = None def pidi(img, res): img = resize_image(HWC3(img), res) global model_pidi if model_pidi is None: from annotator.pidinet import PidiNetDetector model_pidi = PidiNetDetector() result = model_pidi(img) return [result] model_mlsd = None def mlsd(img, res, thr_v, thr_d): img = resize_image(HWC3(img), res) global model_mlsd if model_mlsd is None: from annotator.mlsd import MLSDdetector model_mlsd = MLSDdetector() result = model_mlsd(img, thr_v, thr_d) return [result] model_midas = None def midas(img, res): img = resize_image(HWC3(img), res) global model_midas if model_midas is None: from annotator.midas import MidasDetector model_midas = MidasDetector() result = model_midas(img) return [result] model_zoe = None def zoe(img, res): img = resize_image(HWC3(img), res) global model_zoe if model_zoe is None: from annotator.zoe import ZoeDetector model_zoe = ZoeDetector() result = model_zoe(img) return [result] model_normalbae = None def normalbae(img, res): img = resize_image(HWC3(img), res) global model_normalbae if model_normalbae is None: from annotator.normalbae import NormalBaeDetector model_normalbae = NormalBaeDetector() result = model_normalbae(img) return [result] model_openpose = None def openpose(img, res, hand_and_face): img = resize_image(HWC3(img), res) global model_openpose if model_openpose is None: from annotator.openpose import OpenposeDetector model_openpose = OpenposeDetector() result = model_openpose(img, hand_and_face) return [result] model_dwpose = None def dwpose(img, res): img = resize_image(HWC3(img), res) global model_dwpose if model_dwpose is None: from annotator.dwpose import DWposeDetector model_dwpose = DWposeDetector() result = model_dwpose(img) return [result] #model_uniformer = None #def uniformer(img, res): # img = resize_image(HWC3(img), res) # global model_uniformer # if model_uniformer is None: # from annotator.uniformer import UniformerDetector # model_uniformer = UniformerDetector() # result = model_uniformer(img) # return [result] model_lineart_anime = None def lineart_anime(img, res, invert=True): img = resize_image(HWC3(img), res) global model_lineart_anime if model_lineart_anime is None: from annotator.lineart_anime import LineartAnimeDetector model_lineart_anime = LineartAnimeDetector() # result = model_lineart_anime(img) if (invert): result = cv2.bitwise_not(model_lineart_anime(img)) else: result = model_lineart_anime(img) return [result] model_lineart = None def lineart(img, res, coarse=False, invert=True): img = resize_image(HWC3(img), res) global model_lineart if model_lineart is None: from annotator.lineart import LineartDetector model_lineart = LineartDetector() # result = model_lineart(img, coarse) if (invert): result = cv2.bitwise_not(model_lineart(img, coarse)) else: result = model_lineart(img, coarse) return [result] model_oneformer_coco = None def oneformer_coco(img, res): img = resize_image(HWC3(img), res) global model_oneformer_coco if model_oneformer_coco is None: from annotator.oneformer import OneformerCOCODetector model_oneformer_coco = OneformerCOCODetector() result = model_oneformer_coco(img) return [result] model_oneformer_ade20k = None def oneformer_ade20k(img, res): img = resize_image(HWC3(img), res) global model_oneformer_ade20k if model_oneformer_ade20k is None: from annotator.oneformer import OneformerADE20kDetector model_oneformer_ade20k = OneformerADE20kDetector() result = model_oneformer_ade20k(img) return [result] model_content_shuffler = None def content_shuffler(img, res): img = resize_image(HWC3(img), res) global model_content_shuffler if model_content_shuffler is None: from annotator.shuffle import ContentShuffleDetector model_content_shuffler = ContentShuffleDetector() result = model_content_shuffler(img) return [result] model_color_shuffler = None def color_shuffler(img, res): img = resize_image(HWC3(img), res) global model_color_shuffler if model_color_shuffler is None: from annotator.shuffle import ColorShuffleDetector model_color_shuffler = ColorShuffleDetector() result = model_color_shuffler(img) return [result] model_inpaint = None def inpaint(image, invert): # image = resize_image(img, res) # color = HWC3(image["image"]) # if(invert): # alpha = image["mask"][:, :, 0:1] # else: # alpha = 255 - image["mask"][:, :, 0:1] # result = np.concatenate([color, alpha], axis=2) result = image["mask"] return [result] block = gr.Blocks().queue() with block: gr.Markdown(DESCRIPTION) with gr.Row(): gr.Markdown("## Canny Edge") with gr.Row(): with gr.Column(): input_image = gr.Image(label="Input Image", type="numpy", height=480) # input_image = gr.Image(source='upload', type="numpy") low_threshold = gr.Slider(label="low_threshold", minimum=1, maximum=255, value=100, step=1) high_threshold = gr.Slider(label="high_threshold", minimum=1, maximum=255, value=200, step=1) resolution = gr.Slider(label="resolution", minimum=256, maximum=1024, value=512, step=64) run_button = gr.Button("Run") # run_button = gr.Button(label="Run") with gr.Column(): gallery = gr.Gallery(label="Generated images", show_label=False, height="auto") # gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto") run_button.click(fn=canny, inputs=[input_image, resolution, low_threshold, high_threshold], outputs=[gallery]) gr.Markdown("Mochi Diffusion v4.1で使えるようになりました") with gr.Row(): with gr.Column(): input_image = gr.ImageMask(sources="upload", type="numpy", height="auto") input_preview=gr.Image() # input_image = gr.ImageEditor(sources="upload", type="numpy", height="auto", layers="False", brush=gr.Brush(colors=["#000000"])) # input_image = gr.Image(source='upload', type="numpy", tool="sketch", height=512) # resolution = gr.Slider(label="resolution", minimum=256, maximum=1024, value=512, step=64) invert = gr.Checkbox(label='Invert Mask', value=False) run_button = gr.Button("Run") # run_button = gr.Button(label="Run") with gr.Column(): gallery = gr.Gallery(label="Generated images", show_label=False, height="auto") # gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto")# run_button.click(fn=inpaint, inputs=[input_image, resolution], outputs=[gallery]) run_button.click(fn=inpaint, inputs=[input_image, invert], outputs=[gallery]) gr.Markdown("
Check Invert to use with Mochi Diffusion.") with gr.Row(): with gr.Column(): input_image = gr.Image(label="Input Image", type="numpy", height=480) # input_image = gr.Image(source='upload', type="numpy") invert = gr.Checkbox(label='Invert', value=True) resolution = gr.Slider(label="resolution", minimum=256, maximum=1024, value=512, step=64) run_button = gr.Button("Run") # run_button = gr.Button(label="Run") with gr.Column(): gallery = gr.Gallery(label="Generated images", show_label=False, height="auto") # gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto") run_button.click(fn=lineart_anime, inputs=[input_image, resolution, invert], outputs=[gallery]) gr.Markdown("
Check Invert to use with Mochi Diffusion. Inverted image can also be created here for use with ControlNet Scribble.") with gr.Row(): with gr.Column(): input_image = gr.Image(label="Input Image", type="numpy", height=480) # input_image = gr.Image(source='upload', type="numpy") coarse = gr.Checkbox(label='Using coarse model', value=False) invert = gr.Checkbox(label='Invert', value=True) resolution = gr.Slider(label="resolution", minimum=256, maximum=1024, value=512, step=64) run_button = gr.Button("Run") # run_button = gr.Button(label="Run") with gr.Column(): gallery = gr.Gallery(label="Generated images", show_label=False, height="auto") # gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto") run_button.click(fn=lineart, inputs=[input_image, resolution, coarse, invert], outputs=[gallery]) # with gr.Row(): # gr.Markdown("## Uniformer Segmentation") # with gr.Row(): # with gr.Column(): # input_image = gr.Image(source='upload', type="numpy") # resolution = gr.Slider(label="resolution", minimum=256, maximum=1024, value=512, step=64) # run_button = gr.Button(label="Run") # with gr.Column(): # gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto") # run_button.click(fn=uniformer, inputs=[input_image, resolution], outputs=[gallery]) gr.Markdown("