File size: 5,179 Bytes
4697797
c5cb9ba
 
af6d5f7
4697797
 
9604304
 
4697797
9604304
 
4697797
 
 
 
 
 
 
 
 
 
c5cb9ba
4697797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5cb9ba
 
af6d5f7
 
 
 
 
 
 
 
 
 
 
4697797
 
 
 
 
 
 
 
 
c5cb9ba
 
 
 
 
4697797
 
c5cb9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4697797
 
 
c5cb9ba
 
4697797
c5cb9ba
 
4697797
c5cb9ba
 
4697797
 
 
 
 
 
 
 
 
 
c5cb9ba
 
4697797
 
 
 
 
 
 
 
 
 
 
c5cb9ba
 
 
4697797
fc90453
 
 
 
 
 
4697797
 
 
 
 
 
fc90453
c5cb9ba
 
 
 
4697797
 
 
 
 
 
c5cb9ba
 
 
 
 
 
 
 
 
 
 
 
 
4697797
 
 
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
import gradio as gr
import os
import random
import datetime
from utils import *

file_url = "https://storage.googleapis.com/derendering_model/derendering_supp.zip"
filename = "derendering_supp.zip"

download_file(file_url, filename)
unzip_file(filename)
print("Downloaded and unzipped the file.")

diagram = get_svg_content("derendering_supp/derender_diagram.svg")
org = get_svg_content("org/cor.svg")

org_content = f"""
{org}
"""


def demo(Dataset, Model, Output_Format):
    if Model == "Small-i":
        inkml_path = f"./derendering_supp/small-i_{Dataset}_inkml"
    elif Model == "Small-p":
        inkml_path = f"./derendering_supp/small-p_{Dataset}_inkml"
    elif Model == "Large-i":
        inkml_path = f"./derendering_supp/large-i_{Dataset}_inkml"

    path = f"./derendering_supp/{Dataset}/images_sample"
    samples = os.listdir(path)
    # Randomly pick a sample
    picked_samples = random.sample(samples, min(1, len(samples)))

    query_modes = ["d+t", "r+d", "vanilla"]
    plot_title = {"r+d": "Recognized: ", "d+t": "OCR Input: ", "vanilla": ""}
    text_outputs = []
    img_outputs = []
    video_outputs = []
    now = datetime.datetime.now()
    now = now.strftime("%Y-%m-%d %H:%M:%S")
    print(
        now,
        "Taking sample from dataset:",
        Dataset,
        "and model:",
        Model,
        "with output format:",
        Output_Format,
    )
    for name in picked_samples:
        img_path = os.path.join(path, name)
        img = load_and_pad_img_dir(img_path)

        for mode in query_modes:
            example_id = name.strip(".png")
            inkml_file = os.path.join(inkml_path, mode, example_id + ".inkml")
            text_field = parse_inkml_annotations(inkml_file)["textField"]
            output_text = f"{plot_title[mode]}{text_field}"
            # Text output for three modes
            # d+t: OCR recognition input to the model
            # r+d: Recognition from the model
            # vanilla: None
            text_outputs.append(output_text)
            ink = inkml_to_ink(inkml_file)

            if Output_Format == "Image+Video":
                video_filename = mode + ".mp4"
                plot_ink_to_video(ink, video_filename, input_image=img)
                video_outputs.append(video_filename)
            else:
                video_outputs.append(None)

            fig, ax = plt.subplots()
            ax.axis("off")
            plot_ink(ink, ax, input_image=img)
            buf = BytesIO()
            fig.savefig(buf, format="png", bbox_inches="tight")
            plt.close(fig)
            buf.seek(0)
            res = Image.open(buf)
            img_outputs.append(res)
    return (
        img,
        text_outputs[0],
        img_outputs[0],
        video_outputs[0],
        text_outputs[1],
        img_outputs[1],
        video_outputs[1],
        text_outputs[2],
        img_outputs[2],
        video_outputs[2],
    )


with gr.Blocks() as app:
    gr.HTML(org_content)
    gr.Markdown(
        f"""
        # InkSight: Offline-to-Online Handwriting Conversion by Learning to Read and Write<br>
        <div>{diagram}</div>
        ๐Ÿ”” This demo showcases the outputs of <b>Small-i</b>, <b>Small-p</b>, and <b>Large-i</b> on three public datasets (100 samples each).<br>
        โ„น๏ธ Choose a model variant and dataset, then click 'Sample' to see an input with its corresponding outputs for all three inference types.<br>
        ๐Ÿ“ Choose the output format: Image or Image+Video. While showing only images are faster, videos can demonstrate the writing process of the inks.<br>
        """
    )
    with gr.Row():
        dataset = gr.Dropdown(
            ["IMGUR5K", "IAM", "HierText"], label="Dataset", value="HierText"
        )
        model = gr.Dropdown(
            ["Small-i", "Large-i", "Small-p"],
            label="InkSight Model Variant",
            value="Small-i",
        )
        output_format = gr.Dropdown(
            ["Image", "Image+Video"], label="Output Format", value="Image"
        )
        im = gr.Image(label="Input Image")

    with gr.Row():
        d_t_img = gr.Image(label="Derender with Text")
        r_d_img = gr.Image(label="Recognize and Derender")
        vanilla_img = gr.Image(label="Vanilla")

    with gr.Row():
        d_t_text = gr.Textbox(
            label="OCR recognition input to the model", interactive=False
        )
        r_d_text = gr.Textbox(label="Recognition from the model", interactive=False)
        vanilla_text = gr.Textbox(label="Vanilla", interactive=False)

    with gr.Row():
        d_t_vid = gr.Video(label="Derender with Text", autoplay=True)
        r_d_vid = gr.Video(label="Recognize and Derender", autoplay=True)
        vanilla_vid = gr.Video(label="Vanilla", autoplay=True)

    with gr.Row():
        btn_sub = gr.Button("Sample")

    btn_sub.click(
        fn=demo,
        inputs=[dataset, model, output_format],
        outputs=[
            im,
            d_t_text,
            d_t_img,
            d_t_vid,
            r_d_text,
            r_d_img,
            r_d_vid,
            vanilla_text,
            vanilla_img,
            vanilla_vid,
        ],
    )

app.launch()