hysts HF staff commited on
Commit
6ca4ba4
·
1 Parent(s): 65a32a1
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +80 -0
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ images
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import functools
7
+ import pathlib
8
+ import urllib.request
9
+
10
+ import gradio as gr
11
+ import PIL.Image
12
+ from manga_ocr import MangaOcr
13
+
14
+ TITLE = 'kha-white/manga-ocr'
15
+ DESCRIPTION = 'This is a demo for https://github.com/kha-white/manga-ocr.'
16
+ ARTICLE = None
17
+
18
+
19
+ def parse_args() -> argparse.Namespace:
20
+ parser = argparse.ArgumentParser()
21
+ parser.add_argument('--theme', type=str)
22
+ parser.add_argument('--live', action='store_true')
23
+ parser.add_argument('--share', action='store_true')
24
+ parser.add_argument('--port', type=int)
25
+ parser.add_argument('--disable-queue',
26
+ dest='enable_queue',
27
+ action='store_false')
28
+ parser.add_argument('--allow-flagging', type=str, default='never')
29
+ parser.add_argument('--allow-screenshot', action='store_true')
30
+ return parser.parse_args()
31
+
32
+
33
+ def download_sample_images() -> list[pathlib.Path]:
34
+ image_dir = pathlib.Path('images')
35
+ if not image_dir.exists():
36
+ image_dir.mkdir()
37
+ for index in range(12):
38
+ url = f'https://raw.githubusercontent.com/kha-white/manga-ocr/master/assets/examples/{index:02d}.jpg'
39
+ urllib.request.urlretrieve(url, image_dir / f'{index:02d}.jpg')
40
+ return sorted(image_dir.rglob('*.jpg'))
41
+
42
+
43
+ def run(image: PIL.Image.Image, mocr: MangaOcr) -> str:
44
+ return mocr(image)
45
+
46
+
47
+ def main():
48
+ gr.close_all()
49
+
50
+ args = parse_args()
51
+
52
+ mocr = MangaOcr()
53
+
54
+ func = functools.partial(run, mocr=mocr)
55
+ func = functools.update_wrapper(func, run)
56
+
57
+ image_paths = download_sample_images()
58
+ examples = [[path.as_posix()] for path in image_paths]
59
+
60
+ gr.Interface(
61
+ func,
62
+ gr.inputs.Image(type='pil', label='Input'),
63
+ gr.outputs.Textbox(type='str', label='Output'),
64
+ examples=examples,
65
+ title=TITLE,
66
+ description=DESCRIPTION,
67
+ article=ARTICLE,
68
+ theme=args.theme,
69
+ allow_screenshot=args.allow_screenshot,
70
+ allow_flagging=args.allow_flagging,
71
+ live=args.live,
72
+ ).launch(
73
+ enable_queue=args.enable_queue,
74
+ server_port=args.port,
75
+ share=args.share,
76
+ )
77
+
78
+
79
+ if __name__ == '__main__':
80
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ manga_ocr==0.1.7
2
+ Pillow==9.1.0