DerrylNessie commited on
Commit
9668774
1 Parent(s): 86bdf66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -46
app.py CHANGED
@@ -1,52 +1,40 @@
1
- import re
2
- import jaconv
3
- import gradio as gr
4
- from transformers import AutoTokenizer, AutoFeatureExtractor, VisionEncoderDecoderModel
5
- from PIL import Image
6
- import torch
7
- import cv2
8
  import os
9
  os.system('pip install paddlepaddle')
10
  os.system('pip install paddleocr')
11
  from paddleocr import PaddleOCR, draw_ocr
 
 
 
12
 
13
- tokenizer = AutoTokenizer.from_pretrained("kha-white/manga-ocr-base")
14
-
15
- model = VisionEncoderDecoderModel.from_pretrained("kha-white/manga-ocr-base")
16
-
17
- feature_extractor = AutoFeatureExtractor.from_pretrained("kha-white/manga-ocr-base")
18
-
19
- examples = ["japan.jpg"]
20
-
21
- def post_process(text):
22
- text = ''.join(text.split())
23
- text = text.replace('…', '...')
24
- text = re.sub('[・.]{2,}', lambda x: (x.end() - x.start()) * '.', text)
25
- text = jaconv.h2z(text, ascii=True, digit=True)
26
- return text
27
-
28
- def manga_ocr(img):
29
- ocr = PaddleOCR(use_angle_cls=True, lang='japan',use_gpu=False)
30
- img_path = img.name
31
- result = ocr.ocr(img_path, cls=True)
32
- image = Image.open(img_path).convert('RGB')
33
- pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
34
- output = model.generate(pixel_values)[0]
35
- text = tokenizer.decode(output, skip_special_tokens=True)
36
- text = post_process(text)
37
- return text
38
-
39
- iface = gr.Interface(
40
- fn=manga_ocr,
41
- inputs=[gr.inputs.Image(label="Input", type="pil")],
42
- outputs="text",
43
- layout="horizontal",
44
- theme="huggingface",
45
- title="Manga OCR",
46
- description="Optical Character Recognization for Japanese Texts with focus on Mangas. The model is trained by kha-white with Github link: <a href=\"https://github.com/kha-white/manga-ocr\">manga-ocr</a> while the Space App is made by me.",
47
- allow_flagging='never',
48
  examples=examples,
49
- article = "Author: <a href=\"https://huggingface.co/gryan-galario\">Gryan Galario</a>",
50
- )
51
-
52
- iface.launch()
 
 
 
 
 
 
 
 
1
  import os
2
  os.system('pip install paddlepaddle')
3
  os.system('pip install paddleocr')
4
  from paddleocr import PaddleOCR, draw_ocr
5
+ from PIL import Image
6
+ import gradio as gr
7
+ import torch
8
 
9
+ torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
10
+
11
+ def inference(img, lang):
12
+ ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
13
+ img_path = img.name
14
+ result = ocr.ocr(img_path, cls=True)
15
+ image = Image.open(img_path).convert('RGB')
16
+ boxes = [line[0] for line in result]
17
+ txts = [line[1][0] for line in result]
18
+ scores = [line[1][1] for line in result]
19
+ im_show = draw_ocr(image, boxes, txts, scores,
20
+ font_path='simfang.ttf')
21
+ im_show = Image.fromarray(im_show)
22
+ im_show.save('result.jpg')
23
+ return 'result.jpg'
24
+
25
+ title = 'PaddleOCR'
26
+ description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
27
+ article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
28
+ examples = [['example.jpg','en']]
29
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
30
+ gr.Interface(
31
+ inference,
32
+ [gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')],
33
+ gr.outputs.Image(type='file', label='Output'),
34
+ title=title,
35
+ description=description,
36
+ article=article,
 
 
 
 
 
 
 
37
  examples=examples,
38
+ css=css,
39
+ enable_queue=True
40
+ ).launch(debug=True)