[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:
        # 将文件流转换为ndarray
        nparr = 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_name

    except 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 None
    ocr_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

相关推荐

  1. [PaddlePaddle] PaddleOCR图像模型部署

    2024-07-11 07:08:02       22 阅读
  2. [PaddlePaddle] PaddleSpeech语言模型部署

    2024-07-11 07:08:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 07:08:02       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 07:08:02       55 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 07:08:02       46 阅读
  4. Python语言-面向对象

    2024-07-11 07:08:02       56 阅读

热门阅读

  1. 一起来了解深度学习中的“梯度”

    2024-07-11 07:08:02       20 阅读
  2. linux之内存泄漏分析

    2024-07-11 07:08:02       17 阅读
  3. Kotlin Class

    2024-07-11 07:08:02       19 阅读
  4. uniapp vue3微信小程序如何获取dom元素

    2024-07-11 07:08:02       21 阅读
  5. ROI 接口便捷修改

    2024-07-11 07:08:02       15 阅读
  6. rknn部署rk3588

    2024-07-11 07:08:02       19 阅读
  7. 深入探索Apache Flink:流处理的艺术与实践

    2024-07-11 07:08:02       19 阅读
  8. python 之修改host配置

    2024-07-11 07:08:02       21 阅读
  9. 使用Python + Scrapy + Django构建企业级爬虫平台

    2024-07-11 07:08:02       23 阅读
  10. Elasticsearch 自定义评分和脚本评分

    2024-07-11 07:08:02       16 阅读