kothariyashhh commited on
Commit
34bb4b0
·
verified ·
1 Parent(s): 4ab7e15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip install paddlepaddle==2.4.2')
3
+ # os.system('pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html')
4
+ os.system('pip install paddleocr')
5
+ from paddleocr import PaddleOCR, draw_ocr
6
+ from PIL import Image
7
+ import gradio as gr
8
+ import torch
9
+
10
+ torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
11
+
12
+ def inference(img, lang):
13
+ ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
14
+ img_path = img
15
+ result = ocr.ocr(img_path, cls=True)[0]
16
+
17
+ boxes = [line[0] for line in result]
18
+ txts = [line[1][0] for line in result]
19
+ scores = [line[1][1] for line in result]
20
+
21
+ image = Image.open(img_path).convert('RGB')
22
+ im_show = draw_ocr(image, boxes, txts=None, scores=None, # https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/tools/infer/utility.py#L365
23
+ font_path='simfang.ttf')
24
+ im_show = Image.fromarray(im_show)
25
+ im_show.save('result.jpg')
26
+
27
+ return 'result.jpg', result, '\n'.join(txts)
28
+
29
+ # return 'result.jpg'
30
+
31
+ title = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Spanish and English. 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.'
32
+ article = ""
33
+ description = ""
34
+ examples = [['english.png','en'],['spanish.png','es']]
35
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
36
+ app = gr.Interface(
37
+ inference,
38
+ [gr.Image(type='filepath', label='Input'),gr.Dropdown(choices=['es', 'en'], type="value", value='ch', label='language')],
39
+ # gr.outputs.Image(type='file', label='Output'),
40
+ outputs=["image", "text", "text"],
41
+ title=title,
42
+ description=description,
43
+ article=article,
44
+ examples=examples,
45
+ css=css,
46
+ # enable_queue=True
47
+ )
48
+ app.queue(max_size=10)
49
+ app.launch(debug=True)