File size: 3,019 Bytes
22d81d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
import numpy as np
from Project.aligned_image.aligned_images import align
from Project.scripts.inference import inference
from Project.notebook.out import out

def final(image, style):
    myimg = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    cv2.imwrite('static/img_in/in.jpg', myimg)
    align()
    inference()
    out(style)
    result = cv2.imread('static/img_out/inference_results/00000.jpg')
    aligned = cv2.imread('static/img_aligned/in_01.png')
    return cv2.cvtColor(result, cv2.COLOR_RGB2BGR), cv2.cvtColor(aligned, cv2.COLOR_RGB2BGR)

# 创建 Gradio 接口
style_options = {
    "Emotion": {'Angry': 'angry', 'Surprised': 'surprised'},
    "Celebrity": {'Beyonce': 'Beyonce', 'Hilary Clinton': 'Hilary_clinton', 'Johnny Depp': 'Jhonny Depp', 'Taylor Swift': 'Taylor Swift', 'Trump': 'trump'},
    "Hair Style": {'Afro': 'afro', 'Bowlcut': 'bowlcut', 'Curly Hair': 'curly hair', 'Purple Hair': 'purple hair'}
}

# 定义更新风格选项的函数
def update_styles(style_type):
    if not style_type:
        return gr.Dropdown(choices=[])
    return gr.Dropdown(choices=list(style_options[style_type].keys()))

# 创建 Gradio 界面
with gr.Blocks() as demo:
    gr.Markdown("# Image Style Transfer")
    gr.Markdown("### This app is based on styleclip. Choose a style type and a style from the dropdowns below.")
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="numpy", label="Upload Image")
            style_type_dropdown = gr.Dropdown(choices=list(style_options.keys()), label="Style Type")
            style_dropdown = gr.Dropdown(choices=["Angry", "Curly Hair", "Taylor Swift"], label="Style")
            style_type_dropdown.change(fn=update_styles, inputs=style_type_dropdown, outputs=style_dropdown)
            with gr.Row():
                clear_button = gr.Button("Clear")
                submit_button = gr.Button("Submit")
        with gr.Column():
            output_image = gr.Image(type="numpy", label="Result Image")
            aligned_image = gr.Image(type="numpy", label="Aligned Image")
    
    def on_submit(image, style_type, style):
        style_value = style_options[style_type][style]
        return final(image, style_value)
    
    def on_clear():
        return None, None, None, None, None
    
    clear_button.click(fn=on_clear, inputs=[], outputs=[image_input, style_type_dropdown, style_dropdown, output_image, aligned_image])
    submit_button.click(fn=on_submit, inputs=[image_input, style_type_dropdown, style_dropdown], outputs=[output_image, aligned_image])

    examples = gr.Examples(
        examples=[
            ["static/img/example1.jpg", "Emotion", "Angry"],
            ["static/img/example2.jpg", "Celebrity", "Taylor Swift"],
            ["static/img/example3.jpg", "Hair Style", "Curly Hair"],
        ],
        inputs=[image_input, style_type_dropdown, style_dropdown],
    )
# 启动 Gradio 应用
demo.launch()