欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > [PaddlePaddle飞桨] PaddleOCR-光学字符识别-小模型部署

[PaddlePaddle飞桨] PaddleOCR-光学字符识别-小模型部署

2024/10/24 9:21:25 来源:https://blog.csdn.net/qq_45831414/article/details/140333027  浏览:    关键词:[PaddlePaddle飞桨] PaddleOCR-光学字符识别-小模型部署

PaddleOCR的GitHub项目地址
推荐环境:

PaddlePaddle >= 2.1.2
Python >= 3.7
CUDA >= 10.1 
CUDNN >= 7.6

pip下载指令:

python -m pip install paddlepaddle-gpu==2.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple  pip install paddleocr==2.7.3

小模型配置代码:

from paddleocr import PaddleOCR# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
OCR = PaddleOCR(lang="ch",use_angle_cls=True,use_gpu=True
)  # need to run only once to download and load model into memory

图片文件保存代码:

import io
import cv2
import os
import uuid
import numpy as np
def save_image_file(file_path, file_name, file_content):# 生成一个唯一的文件名unique_filename = str(uuid.uuid4()) + os.path.splitext(file_name)[1]# 确保目录存在if not os.path.exists(file_path):os.makedirs(file_path)try:# 将文件流转换为ndarraynparr = np.frombuffer(file_content.read(), np.uint8)file_nd_array = cv2.imdecode(nparr, cv2.IMREAD_COLOR)# 构建完整的文件路径file_path_with_file_name = os.path.join(file_path, unique_filename)# 保存图像到文件cv2.imwrite(filename=file_path_with_file_name, img=file_nd_array)return file_path_with_file_nameexcept Exception as e:print(f"Error saving file: {e}")return None

获取OCR结果代码:

import os
# 获取指定文件的OCR结果(数组)
def get_text_with_ocr(file_path_with_file_name):if not os.path.exists(file_path_with_file_name):return Noneocr_result = OCR.ocr(file_path_with_file_name)# for idx in range(len(ocr_result)):#     res = ocr_result[idx]#     for line in res:#         print(line)return ocr_result

图像文字提取代码:

# OCR(图像文字提取)
def optical_character_recognition(file_content, file_name):file_path_without_file_name = '.' + STATIC_IMAGE_PATH + "/"if not os.path.exists(file_path_without_file_name):os.makedirs(file_path_without_file_name)file_path_with_file_name = save_image_file(file_path_without_file_name, file_name, file_content)ocr_result = get_text_with_ocr(file_path_with_file_name)# 提取文本信息text_only = '\n'.join([item[1][0] for sublist in ocr_result for item in sublist])return text_only

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com