当前位置:   article > 正文

一点奇怪的想法——paddleocr复现使用_paddleocr lang

paddleocr lang

三个命令行安装paddleocr

conda create -n paddle02 python=3.8

activete paddle02

pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

pip install paddleocr -i https://mirror.baidu.com/pypi/simple

大部分的应该是可以跑的

测试代码

  1. # 第一个简单测试的可以跑
  2. # from paddleocr import PaddleOCR
  3. # import cv2
  4. # ocr=PaddleOCR(use_angle_cls = True,use_gpu= False) #使用CPU预加载,不用GPU
  5. # text=ocr.ocr("E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg",cls=True) #打开图片文件
  6. # #打印所有文本信息
  7. # for t in text:
  8. # print(t[1][0])
  9. # 这个跑不了 ---原因 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
  10. # 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
  11. # from paddleocr import PaddleOCR, draw_ocr
  12. # from PIL import Image
  13. # def my_ocr(img_path):
  14. # # need to run only once to download and load model into memory
  15. # # Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
  16. # # 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
  17. # ocr = PaddleOCR(use_angle_cls=True, lang="ch")
  18. # result = ocr.ocr(img_path, cls=True)
  19. # for line in result:
  20. # print(line)
  21. # image = Image.open(img_path).convert('RGB')
  22. # boxes = [line[0] for line in result]
  23. # txts = [line[1][0] for line in result]
  24. # scores = [line[1][1] for line in result]
  25. # im_show = draw_ocr(image, boxes, txts, scores, font_path="E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf")
  26. # im_show = Image.fromarray(im_show)
  27. # im_show.save('result.jpg')
  28. # if __name__ == '__main__':
  29. # my_ocr("E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg")
  30. # pass
  31. # 这个跑不了 ---原因 可能同上 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
  32. # 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
  33. # -*- coding:utf-8 -*-
  34. # @Time : 2021/6/24 16:39
  35. # @Author : JulyLi
  36. # @File : test.py
  37. # @Software: PyCharm
  38. # import cv2
  39. # import paddleocr
  40. # from PIL import Image, ImageDraw, ImageFont
  41. # import numpy as np
  42. # res = paddleocr.PaddleOCR(use_gpu=False).ocr(img=r'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg')
  43. # img = Image.open(r'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg')
  44. # im = np.array(img)
  45. # for i in range(len(res)):
  46. # # cv2.rectangle(im, (int(res[i][0][0][0]), int(res[i][0][0][1])),
  47. # # (int(res[i][0][2][0]), int(res[i][0][2][1])), (255, 0, 0), 1)
  48. # # 使用cv2.putText不能显示中文,需要使用下面的代码代替
  49. # cv2.putText(im, d['text'][i], (x, y-8), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1)
  50. # pilimg = Image.fromarray(im)
  51. # pilimg.resize((800, 600), Image.ANTIALIAS)
  52. # draw = ImageDraw.Draw(pilimg)
  53. # # 参数1:字体文件路径,参数2:字体大小
  54. # font = ImageFont.truetype("E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf", 15, encoding="utf-8")
  55. # # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
  56. # text, sroce = res[i][1]
  57. # print (text, sroce)
  58. # draw.text((res[i][0][0][0], res[i][0][0][1]), text, (255, 0, 0), font=font)
  59. # im = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
  60. # cv2.imwrite("res.jpg", im)
  61. # cv2.imshow("recoText", im)
  62. # cv2.waitKey(0)
  63. # cv2.destroyAllWindows()
  64. # 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返回的不是一个数组,是一个字符串,需要进行转换。
  65. # 这个跑不了 ---原因 可能同二 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
  66. # 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
  67. # from paddleocr import PaddleOCR, draw_ocr
  68. # # 模型路径下必须含有model和params文件
  69. # ocr = PaddleOCR(use_angle_cls=True,use_gpu=False)#det_model_dir='{your_det_model_dir}', rec_model_dir='{your_rec_model_dir}', rec_char_dict_path='{your_rec_char_dict_path}', cls_model_dir='{your_cls_model_dir}', use_angle_cls=True
  70. # img_path = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/11.jpg'
  71. # result = ocr.ocr(img_path, cls=True)
  72. # for line in result:
  73. # # print(line)
  74. # print (line)
  75. # # print (line[1][0])
  76. # # print (line[1][1])
  77. # # 显示结果
  78. # from PIL import Image
  79. # image = Image.open(img_path).convert('RGB')
  80. # boxes = [line[0][0] for line in result]
  81. # txts = [line[1][0] for line in result]
  82. # scores = [line[1][1] for line in result]
  83. # # print (scores)
  84. # im_show = draw_ocr(image, boxes, txts, scores, font_path='E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf')
  85. # im_show = Image.fromarray(im_show)
  86. # im_show.save('result.jpg') #结果图片保存在代码同级文件夹中。
  87. #可以跑
  88. from paddleocr import PaddleOCR, draw_ocr
  89. from PIL import Image
  90. import fitz
  91. import os
  92. # Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
  93. # 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
  94. def ocrImg(language,img_path,result_img):
  95. ocr = PaddleOCR(use_angle_cls=True, use_gpu=False,lang=language) # need to run only once to download and load model into memory
  96. img_path = img_path
  97. result = ocr.ocr(img_path, cls=True)
  98. for line in result:
  99. # print(line[-1][0], line[-1][1])
  100. print(line)
  101. # 显示结果
  102. image = Image.open(img_path).convert('RGB')
  103. # 修改的关键
  104. boxes = [result[0] for line in result for result in line] # Nested loop added
  105. txts = [result[1][0] for line in result for result in line] # Nested loop added
  106. scores = [result[1][1] for line in result for result in line] # Nested loop adde
  107. # boxes = [line[0] for line in result]
  108. # txts = [line[1][0] for line in result]
  109. # scores = [line[1][1] for line in result]
  110. im_show = draw_ocr(image, boxes, txts, scores, font_path='E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf')
  111. im_show = Image.fromarray(im_show)
  112. im_show.save(result_img)
  113. im_show.show(result_img)
  114. def pdf_to_jpg(name,language):
  115. ocr = PaddleOCR(use_angle_cls=True, use_gpu=False,lang=language) # need to run only once to download and load model into memory
  116. pdfdoc=fitz.open(name)
  117. temp = 0
  118. for pg in range(pdfdoc.page_count):
  119. page = pdfdoc[pg]
  120. rotate = int(0)
  121. # 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
  122. zoom_x = 2.0
  123. zoom_y =2.0
  124. trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
  125. pm = page.get_pixmap(matrix=trans, alpha=False)
  126. pm._writeIMG('temp.jpg',1)
  127. #ocr识别
  128. result =ocr.ocr('temp.jpg', cls=True)
  129. #提取文件名
  130. xx=os.path.splitext(name)
  131. filename=xx[0].split('\\')[-1]+'.txt'
  132. #存储结果
  133. with open(filename,mode='a') as f:
  134. for line in result:
  135. if line[1][1]>0.5:
  136. print(line[1][0].encode('utf-8').decode('utf-8'))
  137. f.write(line[1][0].encode('utf-8').decode('utf-8')+'\n')
  138. print(pg)
  139. if __name__ == '__main__':
  140. language = 'ch'
  141. img_path = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/11.jpg'
  142. #只是保持没有展示,需要展示的话,加个show(),算了还是加上吧。。。碎碎念
  143. result_img = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/testas11.jpg'
  144. ocrImg(language,img_path,result_img)
  145. # pdf_to_jpg(r'F:/1docx.pdf','ch')

然后就是复现了这个 Umi-OCR-main

地址在--hiroi-sora/Umi-OCR_plugins: Umi-OCR 插件库 (github.com)

需要下载这个文件Releases · hiroi-sora/Umi-OCR_plugins (github.com) 

界面是好看的,至少比我写的好,后面的话应该是在这个上加填表的功能。看看能不搞出来

剩下的就是将识别出来的信息进行对应。各位大佬有无好的方法推荐。不甚感激。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/670446
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&