app.py 7.2 KB
Newer Older
W
wuyefeilin 已提交
1 2
import gradio as gr
import numpy as np
S
Stinky-Tofu 已提交
3
import cv2
W
wuyefeilin 已提交
4

W
wuyefeilin 已提交
5 6
import utils
from predict import build_predictor
W
wuyefeilin 已提交
7

S
Stinky-Tofu 已提交
8 9 10 11 12 13 14
ID_PHOTO_IMAGE_DEMO = "./images/idphoto.jpg"

MC_PHOTO_BG = "Red"
MC_PHOTO_SIZE = (413, 626)
MC_PHOTO_WIFE_IMAGE_DEMO = "./images/wife.jpg"
MC_PHOTO_HUSBAND_IMAGE_DEMO = "./images/husband.jpg"

W
wuyefeilin 已提交
15 16
predictor = build_predictor()
sizes_play = utils.size_play()
W
wuyefeilin 已提交
17 18


S
Stinky-Tofu 已提交
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
def crop(img, alpha, thr=0.001):
    """
    Crop the image and alpha according to alpha mask.

    Args:
        img(numpy:ndarray): The image array.
        alpha(numpy:ndarray): The alpha corresponding to the image.
        thr(float): The threshold used to generate the alpha mask.

    Returns:
        img_cropped(numpy:ndarray): Image after crop.
        alpha_cropped(numpy:ndarray): alpha after crop.
    """
    _, mask = cv2.threshold(alpha, 0, 255, cv2.THRESH_BINARY, thr)
    cnt, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    x, y, w, h = cv2.boundingRect(cnt[0])
    return img[y: y + h, x: x + w], alpha[y: y + h, x: x + w]


def copy_img_to_bg(img, alpha, target_h, target_x, bg_color, bg_size):
    """
    Copy img and alpha to the corresponding background respectively.

    Args:
        img(numpy:ndarray): The image array.
        alpha(numpy:ndarray): The alpha corresponding to the image.
        target_h(int): The target height of img in background.
        target_x(int): The target position of the img's x-center in the background.
        bg_color(str): The color of background, the options are "White"、"Blue"、"Red".
        bg_size(tuple): The size of background.

    Returns:
        res_img(numpy:ndarray): Result after copy img to background.
        res_alpha(numpy:ndarray): Result after copy alpha to background.
    """
    bg_h, bg_w = bg_size
    img_h, img_w, _ = img.shape
    r = 1.0 * img_h / target_h
    target_w = int(img_w / r)

    res_img = np.ones((bg_h, bg_w, 3)) * utils.COLOR_MAP[bg_color]
    res_alpha = np.zeros((bg_h, bg_w))

    img = cv2.resize(img, (target_w, target_h), interpolation=cv2.INTER_AREA)
    alpha = cv2.resize(alpha, (target_w, target_h), interpolation=cv2.INTER_AREA)

    x1 = int(max(0, target_x - target_w / 2))
    y1 = int(max(0, bg_h - target_h))
    x2 = int(min(bg_w, x1+target_w))
    y2 = int(min(bg_h, y1+target_h))
    act_w = x2 - x1
    act_h = y2 - y1
    res_img[y1: y2, x1: x2] = img[:act_h, :act_w]
    res_alpha[y1: y2, x1: x2] = alpha[:act_h, :act_w]

    return res_img, res_alpha


def get_mc_photo_app_output(wife, husband):
    """
    Composite marriage certificate photo

    Args:
        wife(numpy:ndarray): The wife image array.
        husband(numpy:ndarray): The husband image array.

    Returns:
        res(numpy:ndarray): The composited marriage certificate photo.
        res_download(str): The path of res.
    """
    alpha_wife = predictor.run(wife)
    alpha_husband = predictor.run(husband)

    _, wife_fg = utils.bg_replace(wife, alpha_wife, bg_name=MC_PHOTO_BG)
    husband, _ = utils.bg_replace(husband, alpha_husband, bg_name=MC_PHOTO_BG)

    wife_fg, alpha_wife = crop(wife_fg, alpha_wife)
    husband, alpha_husband = crop(husband, alpha_husband)

    wife_fg, alpha_wife = copy_img_to_bg(wife_fg, alpha_wife, 338, 225, MC_PHOTO_BG, MC_PHOTO_SIZE)
    husband, alpha_husband = copy_img_to_bg(husband, alpha_husband, 375, 401, MC_PHOTO_BG, MC_PHOTO_SIZE)

    alpha_wife = alpha_wife[:, :, None] / 255.0
    res = (wife_fg * alpha_wife + (1 - alpha_wife) * husband).astype(np.uint8)

    res_download = utils.download(res, "Large")
    return res, res_download


def get_id_photo_output(img, size, bg, download_size):
W
wuyefeilin 已提交
109 110
    """
    Get the special size and background photo.
W
wuyefeilin 已提交
111

W
wuyefeilin 已提交
112 113 114 115 116
    Args:
        img(numpy:ndarray): The image array.
        size(str): The size user specified.
        bg(str): The background color user specified.
        download_size(str): The size for image saving.
W
wuyefeilin 已提交
117

W
wuyefeilin 已提交
118 119
    """
    alpha = predictor.run(img)
S
Stinky-Tofu 已提交
120
    res, _ = utils.bg_replace(img, alpha, bg_name=bg)
W
wuyefeilin 已提交
121

W
wuyefeilin 已提交
122 123 124 125
    size_index = sizes_play.index(size)
    res = utils.adjust_size(res, size_index)
    res_download = utils.download(res, download_size)
    return res, res_download
W
wuyefeilin 已提交
126 127


S
Stinky-Tofu 已提交
128 129 130
def clear_id_photo_all():
    utils.delete_result()
    return None, None, sizes_play[0], 'White', 'Large', None
W
wuyefeilin 已提交
131 132


S
Stinky-Tofu 已提交
133 134 135 136
def clear_mc_photo_all():
    utils.delete_result()
    return None, None, None, None

W
wuyefeilin 已提交
137

S
Stinky-Tofu 已提交
138 139
with gr.Blocks() as demo:
    gr.Markdown("""# ID and MC(Marriage Certificate) Photo DIY""")
W
wuyefeilin 已提交
140
    gr.Markdown(
S
Stinky-Tofu 已提交
141 142
        """<font color=Gray>Tips: Please upload photos with good posture, center portrait, 
        crown free, no jewelry, ears and eyebrows exposed.</font>"""
W
wuyefeilin 已提交
143
    )
S
Stinky-Tofu 已提交
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
    with gr.Tab("IDPhoto"):
        id_photo_img_in = gr.Image(value=ID_PHOTO_IMAGE_DEMO, label="Input image")
        with gr.Row():
            id_photo_size = gr.Dropdown(sizes_play, label="Sizes", value=sizes_play[0])
            id_photo_bg = gr.Radio(
                ["White", "Red", "Blue"], label="Background color", value='White')
            id_photo_download_size = gr.Radio(
                ["Small", "Middle", "Large"],
                label="File size (affects image quality)",
                value='Large',
                interactive=True)

        with gr.Row():
            id_photo_clear_btn = gr.Button("Clear")
            id_photo_submit_btn = gr.Button("Submit")

        id_photo_img_out = gr.Image(label="Output image", interactive=False).style(height=300)
        id_photo_downloaded_img = gr.File(label='Image download').style(height=50)

    id_photo_clear_btn.click(
        fn=clear_id_photo_all,
        inputs=None,
        outputs=[id_photo_img_in, id_photo_img_out, id_photo_size, id_photo_bg,
                 id_photo_download_size, id_photo_downloaded_img])
    id_photo_submit_btn.click(
        fn=get_id_photo_output,
        inputs=[id_photo_img_in, id_photo_size, id_photo_bg, id_photo_download_size],
        outputs=[id_photo_img_out, id_photo_downloaded_img])

    with gr.Tab("MCPhoto"):
        with gr.Row():
            mc_photo_img_wife = gr.Image(value=MC_PHOTO_WIFE_IMAGE_DEMO, label="Wife", interactive=True)
            mc_photo_img_husband = gr.Image(value=MC_PHOTO_HUSBAND_IMAGE_DEMO, label="Husband", interactive=True)

        with gr.Row():
            mc_photo_clear_button = gr.Button("Clear")
            mc_photo_submit_button = gr.Button("Submit")

        mc_photo_img_out = gr.Image(label="Output image", interactive=False).style(height=300)
        mc_photo_download_img = gr.File(label='Image download').style(height=50)

    mc_photo_clear_button.click(
        fn=clear_mc_photo_all,
W
wuyefeilin 已提交
187
        inputs=None,
S
Stinky-Tofu 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200
        outputs=[mc_photo_img_wife, mc_photo_img_husband, mc_photo_img_out, mc_photo_download_img])
    mc_photo_submit_button.click(
        fn=get_mc_photo_app_output,
        inputs=[mc_photo_img_wife, mc_photo_img_husband],
        outputs=[mc_photo_img_out, mc_photo_download_img])

    gr.Markdown(
        """<font color=Gray>This application is supported by 
        [PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg). 
        If you have any question or feature request, 
        welcome to raise issues on [GitHub](https://github.com/PaddlePaddle/PaddleSeg/issues). 
        BTW, a star is a great encouragement for us, thanks!  ^_^</font>"""
    )
W
wuyefeilin 已提交
201

W
wuyefeilin 已提交
202 203
    gr.Button.style(1)

W
wuyefeilin 已提交
204
demo.launch(share=True)