app.py 2.4 KB
Newer Older
W
wuyefeilin 已提交
1 2 3
import gradio as gr
import numpy as np

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

W
wuyefeilin 已提交
7 8 9
IMAGE_DEMO = "./images/idphoto.jpg"
predictor = build_predictor()
sizes_play = utils.size_play()
W
wuyefeilin 已提交
10 11


W
wuyefeilin 已提交
12 13 14
def get_output(img, size, bg, download_size):
    """
    Get the special size and background photo.
W
wuyefeilin 已提交
15

W
wuyefeilin 已提交
16 17 18 19 20
    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 已提交
21

W
wuyefeilin 已提交
22 23 24
    """
    alpha = predictor.run(img)
    res = utils.bg_replace(img, alpha, bg_name=bg)
W
wuyefeilin 已提交
25

W
wuyefeilin 已提交
26 27 28 29
    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 已提交
30 31


W
wuyefeilin 已提交
32 33 34
def download(img, size):
    utils.download(img, size)
    return None
W
wuyefeilin 已提交
35 36


W
wuyefeilin 已提交
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
with gr.Blocks() as demo:
    gr.Markdown("""# ID Photo DIY""")

    img_in = gr.Image(value=IMAGE_DEMO, label="Input image")
    gr.Markdown(
        """<font color=Gray>Tips: Please upload photos with good posture, center portrait, crown free, no jewelry, ears and eyebrows exposed.</font>"""
    )
    with gr.Row():
        size = gr.Dropdown(sizes_play, label="Sizes", value=sizes_play[0])
        bg = gr.Radio(
            ["White", "Red", "Blue"], label="Background color", value='White')
        download_size = gr.Radio(
            ["Small", "Middle", "Large"],
            label="File size (affects image quality)",
            value='Large',
            interactive=True)

    with gr.Row():
        btn1 = gr.Button("Clear")
        btn2 = gr.Button("Submit")

    img_out = gr.Image(
        label="Output image", interactive=False).style(height=300)
    f1 = gr.File(label='Image download').style(height=50)
    with gr.Row():
        gr.Markdown(
            """<font color=Gray>This application is supported by [PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg). 
            If you have any questions or feature requists, welcome to raise issues on [GitHub](https://github.com/PaddlePaddle/PaddleSeg/issues). BTW, a star is a great encouragement for us, thanks!  ^_^</font>"""
        )

    btn2.click(
        fn=get_output,
        inputs=[img_in, size, bg, download_size],
        outputs=[img_out, f1])
    btn1.click(
        fn=utils.clear_all,
        inputs=None,
        outputs=[img_in, img_out, size, bg, download_size, f1])

W
wuyefeilin 已提交
76 77
    gr.Button.style(1)

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