kothariyashhh commited on
Commit
458bd3f
·
verified ·
1 Parent(s): db61f2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -1,49 +1,61 @@
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 = 'PaddleOCR demo supports Spanish and English.'
 
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'), 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)
 
 
 
 
1
  import os
 
 
 
2
  from paddleocr import PaddleOCR, draw_ocr
3
  from PIL import Image
4
  import gradio as gr
5
  import torch
6
 
7
+ # Download example image
8
  torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
9
 
10
  def inference(img, lang):
11
+ # Initialize OCR with the selected language
12
+ ocr = PaddleOCR(use_angle_cls=True, lang=lang, use_gpu=False)
13
  img_path = img
14
  result = ocr.ocr(img_path, cls=True)[0]
15
 
16
+ # Extract boxes and text
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
+ # Load image and draw OCR results
22
  image = Image.open(img_path).convert('RGB')
23
+ im_show = draw_ocr(image, boxes, txts=None, scores=None, font_path='simfang.ttf')
 
24
  im_show = Image.fromarray(im_show)
25
  im_show.save('result.jpg')
26
 
27
+ return 'result.jpg', '\n'.join(txts)
28
+
29
+ def update_example(lang):
30
+ # Automatically load the example based on selected language
31
+ if lang == 'es':
32
+ return 'spanish.png'
33
+ elif lang == 'en':
34
+ return 'english.png'
35
+ return None
36
 
37
+ # Title and description for the Gradio app
38
+ title = 'Gradio demo for PaddleOCR'
39
  article = ""
40
+ description = "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."
41
+
42
+ # Gradio Interface
43
  app = gr.Interface(
44
  inference,
45
+ inputs=[
46
+ gr.Image(type='filepath', label='Input'),
47
+ gr.Dropdown(choices=['es', 'en'], value='en', label='Language', interactive=True)
48
+ ],
49
+ outputs=["image", "text"],
50
  title=title,
51
  description=description,
52
  article=article,
53
+ examples=[['english.png','en'], ['spanish.png','es']],
54
+ css=".output_image, .input_image {height: 40rem !important; width: 100% !important;}",
55
+ )
56
+
57
+ # Add function to update input based on language selection
58
+ app.set_input_callback(update_example, ['language'])
59
+
60
+ # Launch the app
61
+ app.launch(debug=True)