import gradio as gr import os import random import datetime from utils import * from pathlib import Path file_url = "https://storage.googleapis.com/derendering_model/derendering_supp.zip" filename = "derendering_supp.zip" # Cache videos to speed up demo video_cache_dir = Path("./cached_videos") video_cache_dir.mkdir(exist_ok=True) 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}" gif_filenames = [ "christians.gif", "good.gif", "october.gif", "welcome.gif", "you.gif", "letter.gif", ] captions = [ "CHRISTIANS", "Good", "October", "WELOME", "you", "letter", ] gif_base64_strings = { caption: get_base64_encoded_gif(f"gifs/{name}") for caption, name in zip(captions, gif_filenames) } sketches = [ "bird.gif", "cat.gif", "coffee.gif", "penguin.gif", ] sketches_base64_strings = { name: get_base64_encoded_gif(f"sketches/{name}") for name in sketches } datasets = ["IAM", "IMGUR5K", "HierText"] models = ["Small-i", "Large-i", "Small-p"] query_modes = ["d+t", "r+d", "vanilla"] def pregenerate_videos(): for Dataset in datasets: for Model in models: inkml_path_base = f"./derendering_supp/{Model.lower()}_{Dataset}_inkml" for mode in query_modes: path = f"./derendering_supp/{Dataset}/images_sample" if not os.path.exists(path): continue samples = os.listdir(path) selected_samples = random.sample(samples, len(samples) // 3) for name in tqdm( selected_samples, desc=f"Generating {Model}-{Dataset}-{mode} videos" ): example_id = name.strip(".png") inkml_file = os.path.join( inkml_path_base, mode, f"{example_id}.inkml" ) if not os.path.exists(inkml_file): continue video_filename = f"{Model}_{Dataset}_{mode}_{example_id}.mp4" video_filepath = video_cache_dir / video_filename if not video_filepath.exists(): img_path = os.path.join(path, name) img = load_and_pad_img_dir(img_path) ink = inkml_to_ink(inkml_file) plot_ink_to_video(ink, str(video_filepath), input_image=img) pregenerate_videos() 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" now = datetime.datetime.now() random.seed(now.timestamp()) now = now.strftime("%Y-%m-%d %H:%M:%S") print( now, "Taking sample from dataset:", Dataset, "and model:", Model, "with output format:", Output_Format, ) 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 = [] 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_outputs.append(output_text) ink = inkml_to_ink(inkml_file) video_filename = f"{Model}_{Dataset}_{mode}_{example_id}.mp4" video_filepath = video_cache_dir / video_filename if Output_Format == "Image+Video": if not video_filepath.exists(): plot_ink_to_video(ink, str(video_filepath), input_image=img) print("Cached video at:", video_filepath) video_outputs.append("./" + str(video_filepath)) 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( "# InkSight: Offline-to-Online Handwriting Conversion by Learning to Read and Write" ) gr.HTML( """
""" ) gr.HTML(f"{title}
{}
{}
{}