|
|
|
|
|
from __future__ import annotations |
|
|
|
import os |
|
import pathlib |
|
import urllib.request |
|
|
|
import gradio as gr |
|
import PIL.Image |
|
from manga_ocr import MangaOcr |
|
|
|
DESCRIPTION = "# [Manga OCR](https://github.com/kha-white/manga-ocr)" |
|
|
|
|
|
def download_sample_images() -> list[pathlib.Path]: |
|
image_dir = pathlib.Path("images") |
|
if not image_dir.exists(): |
|
image_dir.mkdir() |
|
for index in range(12): |
|
url = f"https://raw.githubusercontent.com/kha-white/manga-ocr/master/assets/examples/{index:02d}.jpg" |
|
urllib.request.urlretrieve(url, image_dir / f"{index:02d}.jpg") |
|
return sorted(image_dir.rglob("*.jpg")) |
|
|
|
|
|
mocr = MangaOcr() |
|
|
|
|
|
def run(image: PIL.Image.Image) -> str: |
|
return mocr(image) |
|
|
|
|
|
examples = download_sample_images() |
|
|
|
with gr.Blocks(css="style.css") as demo: |
|
gr.Markdown(DESCRIPTION) |
|
with gr.Row(): |
|
with gr.Column(): |
|
image = gr.Image(label="Input", type="pil") |
|
run_button = gr.Button() |
|
with gr.Column(): |
|
result = gr.Text(label="Output") |
|
gr.Examples( |
|
examples=examples, |
|
inputs=image, |
|
outputs=result, |
|
fn=run, |
|
) |
|
run_button.click( |
|
fn=run, |
|
inputs=image, |
|
outputs=result, |
|
api_name="run", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.queue().launch() |
|
|