eoeooe commited on
Commit
608d66a
·
verified ·
1 Parent(s): 05a12b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,44 +1,53 @@
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==2.10.0')
5
- from paddleocr import PaddleOCR #, draw_ocr
6
  from PIL import Image
7
  import gradio as gr
8
 
9
- # from urllib.request import urlretrieve
10
- # urlretrieve('https://i.imgur.com/aqMBT0i.jpeg', '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] # 2.9
16
- result = ocr.ocr(img_path, cls=True)[0]
17
- # image = Image.open(img_path).convert('RGB')
18
- # boxes = [line[0] for line in result]
19
- print(result) #debug
20
  txts = [line[1][0] for line in result]
21
  return '\n'.join(txts)
22
- # scores = [line[1][1] for line in result]
23
- # im_show = draw_ocr(image, boxes, txts, scores,
24
- # font_path='simfang.ttf')
25
- # im_show = Image.fromarray(im_show)
26
- # im_show.save('result.jpg')
27
- # return 'result.jpg'
28
 
29
- title = 'PaddleOCR'
30
- 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.'
31
- 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>"
32
- examples = [['example.jpg','ch']]
 
33
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
 
34
  gr.Interface(
35
- inference,
36
- [gr.Image(type='filepath', label='Input'),gr.Dropdown(choices=['ch', 'en', 'korean', 'japan', 'chinese_cht', 'ta', 'te', 'ka', 'latin', 'arabic', 'cyrillic', 'devanagari'], value="en", type="value", label='language')],
37
- # gr.outputs.Image(type='file', label='Output'),
 
 
 
 
 
 
 
38
  outputs="text",
39
  title=title,
40
  description=description,
41
  article=article,
42
  examples=examples,
43
  css=css
44
- ).launch(debug=True)
 
1
  import os
2
+ from paddleocr import PaddleOCR
 
 
 
3
  from PIL import Image
4
  import gradio as gr
5
 
6
+ # โหลด OCR models ครั้งเดียว (ไม่ต้องสร้างใหม่ทุก inference)
7
+ ocr_models = {
8
+ 'en': PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False),
9
+ 'ch': PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=False),
10
+ 'korean': PaddleOCR(use_angle_cls=True, lang='korean', use_gpu=False),
11
+ 'japan': PaddleOCR(use_angle_cls=True, lang='japan', use_gpu=False),
12
+ 'chinese_cht': PaddleOCR(use_angle_cls=True, lang='chinese_cht', use_gpu=False),
13
+ 'ta': PaddleOCR(use_angle_cls=True, lang='ta', use_gpu=False),
14
+ 'te': PaddleOCR(use_angle_cls=True, lang='te', use_gpu=False),
15
+ 'ka': PaddleOCR(use_angle_cls=True, lang='ka', use_gpu=False),
16
+ 'latin': PaddleOCR(use_angle_cls=True, lang='latin', use_gpu=False),
17
+ 'arabic': PaddleOCR(use_angle_cls=True, lang='arabic', use_gpu=False),
18
+ 'cyrillic': PaddleOCR(use_angle_cls=True, lang='cyrillic', use_gpu=False),
19
+ 'devanagari': PaddleOCR(use_angle_cls=True, lang='devanagari', use_gpu=False)
20
+ }
21
 
22
+ # ฟังก์ชัน inference
23
  def inference(img, lang):
24
+ ocr = ocr_models.get(lang, ocr_models['en']) # default เป็นอังกฤษ
25
+ result = ocr.ocr(img, cls=True)[0] # cls=True ถ้าต้องการหมุนภาพอัตโนมัติ
 
 
 
 
 
26
  txts = [line[1][0] for line in result]
27
  return '\n'.join(txts)
 
 
 
 
 
 
28
 
29
+ # Gradio Interface
30
+ title = 'PaddleOCR Fast Demo'
31
+ description = 'Upload an image and choose language to extract text using PaddleOCR (supports 80+ languages).'
32
+ article = "<p style='text-align: center'><a href='https://github.com/PaddlePaddle/PaddleOCR'>PaddleOCR Github Repo</a></p>"
33
+ examples = [['example.jpg', 'en']] # ต้องมีไฟล์ example.jpg ในโฟลเดอร์เดียวกัน
34
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
35
+
36
  gr.Interface(
37
+ fn=inference,
38
+ inputs=[
39
+ gr.Image(type='filepath', label='Input Image'),
40
+ gr.Dropdown(
41
+ choices=list(ocr_models.keys()),
42
+ value='en',
43
+ type="value",
44
+ label='Language'
45
+ )
46
+ ],
47
  outputs="text",
48
  title=title,
49
  description=description,
50
  article=article,
51
  examples=examples,
52
  css=css
53
+ ).launch(debug=True)