YI-X commited on
Commit
bbf5d0a
·
verified ·
1 Parent(s): 445c2d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -3,32 +3,40 @@ from paddleocr import PaddleOCR
3
  from PIL import Image
4
  import numpy as np
5
 
6
- # 初始化PaddleOCR(会自动下载模型)
7
- ocr = PaddleOCR(use_angle_cls=True, lang='ch')
 
 
 
8
 
9
  def ocr_image(image):
10
  if image is None:
11
- return "请上传图片"
12
-
13
- # 转换为numpy数组
14
  img_array = np.array(image)
15
 
16
  # 执行OCR
17
- result = ocr.ocr(img_array, cls=True)
 
 
 
 
18
 
19
- # 提取所有识别的文字
20
  texts = []
21
- for line in result:
22
- for word_info in line:
23
- texts.append(word_info[1][0])
24
 
25
- return '\n'.join(texts) if texts else "未识别到文字"
26
 
 
27
  iface = gr.Interface(
28
  fn=ocr_image,
29
  inputs=gr.Image(type="pil", label="上传图片"),
30
  outputs=gr.Textbox(label="识别结果", lines=6),
31
- title="中文OCR识别器(PaddleOCR)",
32
- description="上传包含中文文字的图片"
33
  )
 
34
  iface.launch()
 
3
  from PIL import Image
4
  import numpy as np
5
 
6
+ # 初始化OCR模型,指定使用你选的PP-OCRv5_mobile_rec识别模型
7
+ # use_angle_cls用于识别旋转的文字,这里先关闭
8
+ ocr = PaddleOCR(use_angle_cls=False,
9
+ lang='ch', # 使用中英文混合模型
10
+ text_recognition_model_name="PP-OCRv5_mobile_rec")
11
 
12
  def ocr_image(image):
13
  if image is None:
14
+ return "请先上传一张清晰的图片。"
15
+
16
+ # 将PIL图片转换为numpy数组,因为PaddleOCR需要这种格式
17
  img_array = np.array(image)
18
 
19
  # 执行OCR
20
+ result = ocr.ocr(img_array, cls=False)
21
+
22
+ # 解析结果并提取文字
23
+ if not result or not result[0]:
24
+ return "未识别到文字。请确保图片清晰且包含文字。"
25
 
 
26
  texts = []
27
+ for line in result[0]:
28
+ # line的格式是:[[框坐标], (识别文字, 置信度)]
29
+ texts.append(line[1][0])
30
 
31
+ return '\n'.join(texts) if texts else "未识别到文字。"
32
 
33
+ # 创建Gradio界面
34
  iface = gr.Interface(
35
  fn=ocr_image,
36
  inputs=gr.Image(type="pil", label="上传图片"),
37
  outputs=gr.Textbox(label="识别结果", lines=6),
38
+ title="中英文OCR识别器 (PP-OCRv5)",
39
+ description="上传包含中文或英文文字的清晰图片,AI将提取所有文字。基于PaddlePaddle/PP-OCRv5_mobile_rec模型。"
40
  )
41
+
42
  iface.launch()