vumichien commited on
Commit
b808e5c
1 Parent(s): 0ae75d2
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ from transformers import AutoFeatureExtractor, AutoTokenizer, VisionEncoderDecoderModel
4
+ import re
5
+ import jaconv
6
+
7
+ #load model
8
+ model_path = "kha-white/manga-ocr-base"
9
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_path)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
11
+ model = VisionEncoderDecoderModel.from_pretrained(model_path)
12
+
13
+
14
+ def post_process(text):
15
+ text = ''.join(text.split())
16
+ text = text.replace('…', '...')
17
+ text = re.sub('[・.]{2,}', lambda x: (x.end() - x.start()) * '.', text)
18
+ text = jaconv.h2z(text, ascii=True, digit=True)
19
+ return text
20
+
21
+ def infer(image):
22
+ image = image.convert('L').convert('RGB')
23
+ pixel_values = feature_extractor(image, return_tensors="pt").pixel_values
24
+ ouput = model.generate(pixel_values)[0]
25
+ text = tokenizer.decode(ouput, skip_special_tokens=True)
26
+ text = post_process(text)
27
+ return text
28
+
29
+
30
+ iface = gr.Interface(
31
+ fn=infer,
32
+ inputs=[gr.inputs.Image(label="Input", type="pil")],
33
+ outputs="text",
34
+ layout="horizontal",
35
+ theme="huggingface",
36
+ title="Optical character recognition for Japanese text",
37
+ description="A simple interface for OCR from Japanese manga",
38
+ article= "Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. ",
39
+ allow_flagging='never',
40
+ )
41
+ iface.launch(enable_queue=True, share=True, debug=True)
examples/01.png ADDED
examples/02.png ADDED
examples/03.png ADDED
examples/04.png ADDED
examples/05.png ADDED
examples/06.png ADDED
examples/07.png ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fugashi
2
+ unidic_lite
3
+ jaconv