三个命令行安装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
大部分的应该是可以跑的
测试代码
# 第一个简单测试的可以跑
# from paddleocr import PaddleOCR
# import cv2
# ocr=PaddleOCR(use_angle_cls = True,use_gpu= False) #使用CPU预加载,不用GPU
# text=ocr.ocr("E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg",cls=True) #打开图片文件
# #打印所有文本信息
# for t in text:
# print(t[1][0])
# 这个跑不了 ---原因 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
# 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
# from paddleocr import PaddleOCR, draw_ocr
# from PIL import Image
# def my_ocr(img_path):
# # need to run only once to download and load model into memory
# # Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# # 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
# ocr = PaddleOCR(use_angle_cls=True, lang="ch")
# result = ocr.ocr(img_path, cls=True)
# for line in result:
# print(line)
# image = Image.open(img_path).convert('RGB')
# boxes = [line[0] for line in result]
# txts = [line[1][0] for line in result]
# scores = [line[1][1] for line in result]
# im_show = draw_ocr(image, boxes, txts, scores, font_path="E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf")
# im_show = Image.fromarray(im_show)
# im_show.save('result.jpg')
# if __name__ == '__main__':
# my_ocr("E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg")
# pass
# 这个跑不了 ---原因 可能同上 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
# 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
# -*- coding:utf-8 -*-
# @Time : 2021/6/24 16:39
# @Author : JulyLi
# @File : test.py
# @Software: PyCharm
# import cv2
# import paddleocr
# from PIL import Image, ImageDraw, ImageFont
# import numpy as np
# res = paddleocr.PaddleOCR(use_gpu=False).ocr(img=r'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg')
# img = Image.open(r'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/1.jpg')
# im = np.array(img)
# for i in range(len(res)):
# # cv2.rectangle(im, (int(res[i][0][0][0]), int(res[i][0][0][1])),
# # (int(res[i][0][2][0]), int(res[i][0][2][1])), (255, 0, 0), 1)
# # 使用cv2.putText不能显示中文,需要使用下面的代码代替
# cv2.putText(im, d['text'][i], (x, y-8), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1)
# pilimg = Image.fromarray(im)
# pilimg.resize((800, 600), Image.ANTIALIAS)
# draw = ImageDraw.Draw(pilimg)
# # 参数1:字体文件路径,参数2:字体大小
# font = ImageFont.truetype("E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf", 15, encoding="utf-8")
# # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
# text, sroce = res[i][1]
# print (text, sroce)
# draw.text((res[i][0][0][0], res[i][0][0][1]), text, (255, 0, 0), font=font)
# im = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
# cv2.imwrite("res.jpg", im)
# cv2.imshow("recoText", im)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返回的不是一个数组,是一个字符串,需要进行转换。
# 这个跑不了 ---原因 可能同二 解决:更换paddleOcr 版本 最新版本是2.6 V2.6的版本调用ocr.ocr 返
# 回的不是一个数组,是一个字符串,需要进行转换。有些s第二尽是不知所云,瞎讲。
# from paddleocr import PaddleOCR, draw_ocr
# # 模型路径下必须含有model和params文件
# 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
# img_path = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/11.jpg'
# result = ocr.ocr(img_path, cls=True)
# for line in result:
# # print(line)
# print (line)
# # print (line[1][0])
# # print (line[1][1])
# # 显示结果
# from PIL import Image
# image = Image.open(img_path).convert('RGB')
# boxes = [line[0][0] for line in result]
# txts = [line[1][0] for line in result]
# scores = [line[1][1] for line in result]
# # print (scores)
# im_show = draw_ocr(image, boxes, txts, scores, font_path='E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf')
# im_show = Image.fromarray(im_show)
# im_show.save('result.jpg') #结果图片保存在代码同级文件夹中。
#可以跑
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import fitz
import os
# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
def ocrImg(language,img_path,result_img):
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False,lang=language) # need to run only once to download and load model into memory
img_path = img_path
result = ocr.ocr(img_path, cls=True)
for line in result:
# print(line[-1][0], line[-1][1])
print(line)
# 显示结果
image = Image.open(img_path).convert('RGB')
# 修改的关键
boxes = [result[0] for line in result for result in line] # Nested loop added
txts = [result[1][0] for line in result for result in line] # Nested loop added
scores = [result[1][1] for line in result for result in line] # Nested loop adde
# boxes = [line[0] for line in result]
# txts = [line[1][0] for line in result]
# scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='E:/2024/PaddleOCR-release-2.6//doc/fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save(result_img)
im_show.show(result_img)
def pdf_to_jpg(name,language):
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False,lang=language) # need to run only once to download and load model into memory
pdfdoc=fitz.open(name)
temp = 0
for pg in range(pdfdoc.page_count):
page = pdfdoc[pg]
rotate = int(0)
# 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
zoom_x = 2.0
zoom_y =2.0
trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
pm = page.get_pixmap(matrix=trans, alpha=False)
pm._writeIMG('temp.jpg',1)
#ocr识别
result =ocr.ocr('temp.jpg', cls=True)
#提取文件名
xx=os.path.splitext(name)
filename=xx[0].split('\\')[-1]+'.txt'
#存储结果
with open(filename,mode='a') as f:
for line in result:
if line[1][1]>0.5:
print(line[1][0].encode('utf-8').decode('utf-8'))
f.write(line[1][0].encode('utf-8').decode('utf-8')+'\n')
print(pg)
if __name__ == '__main__':
language = 'ch'
img_path = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/11.jpg'
#只是保持没有展示,需要展示的话,加个show(),算了还是加上吧。。。碎碎念
result_img = 'E:/2024/PaddleOCR-release-2.6/ppocr_img/imgs/testas11.jpg'
ocrImg(language,img_path,result_img)
# 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)
界面是好看的,至少比我写的好,后面的话应该是在这个上加填表的功能。看看能不搞出来
剩下的就是将识别出来的信息进行对应。各位大佬有无好的方法推荐。不甚感激。