python 3.10.3 和 飞浆ocr 3.2使用

依赖

paddlepaddle==3.2.0
paddleocr==3.2.0

运行测试

paddleocr ocr -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png --use_doc_orientation_classify False --use_doc_unwarping False --use_textline_orientation False  

代码

from paddleocr import PaddleOCR
import cv2

ocr = PaddleOCR(
    use_doc_orientation_classify=False,  # 文档整体方向分类 比如你拍的合同是 90° 横过来的、扫描件是 180° 倒过来的),开启后会自动把文档旋转回正常方向再识别。
    use_doc_unwarping=False,  # 文档弯曲矫正 比如拍的书本内页有弧度、褶皱的纸质文档拍照后文字变形),开启后会先把弯曲的文档还原成平整的,再识别文字。
    use_textline_orientation=False,  # 文档里既有横排文字,又有竖排文字;某一行文字是 90° 旋转的),开启后会逐行矫正文字方向,避免把「竖排的字」识别成乱码。
    text_detection_model_dir=r'C:\Users\123\.paddlex\official_models\PP-OCRv5_server_det',
    text_detection_model_name='PP-OCRv5_server_det',
    text_recognition_model_dir=r'C:\Users\123\.paddlex\official_models\en_PP-OCRv5_mobile_rec',
    text_recognition_model_name='en_PP-OCRv5_mobile_rec',
    text_det_limit_side_len=300,  # 核心:适中尺寸,平衡精度和速度
    text_det_limit_type='min' # 缩放最小边到736,放大小数字区域
    #ocr_version='PP-OCRv5',
    #lang='en',
)


regions = [[[219, 196], [721, 196], [721, 399], [219, 399]]]
x = [x[0] for i in regions for x in i]
y = [x[1] for i in regions for x in i]
min_x, min_y = min(x), min(y)
max_x, max_y = max(x), max(y)

image = cv2.imread(r'C:\Users\123\Desktop\12.png')
image = image[min_y:max_y, min_x:max_x]

image = cv2.copyMakeBorder(
    image,
    top=50,          # 上边框50像素
    bottom=50,       # 下边框50像素
    left=50,         # 左边框50像素
    right=50,        # 右边框50像素
    borderType=cv2.BORDER_CONSTANT,  # 固定颜色边框
    value=(255, 255, 255)            # 边框颜色:纯白色
)



cv2.imshow('', image)
cv2.waitKey(0)

res = ocr.predict(image)
for i in res:
    print(i['rec_texts'])
class FeijiangOcr(object):
    '''飞浆ocr'''

    def __init__(self, weights_file):
        self.ocr = PaddleOCR(
            text_detection_model_dir=os.path.join(weights_file, 'PP-OCRv5_server_det'),
            text_detection_model_name='PP-OCRv5_server_det', # 文字检测模型

            text_recognition_model_dir=os.path.join(weights_file, 'en_PP-OCRv5_mobile_rec'),
            text_recognition_model_name='en_PP-OCRv5_mobile_rec', # 文字识别
            text_recognition_batch_size=1, # 识别阶段的批量推理批次大小

            doc_orientation_classify_model_dir=os.path.join(weights_file, 'PP-LCNet_x1_0_doc_ori'),  # 文档方向分类
            doc_orientation_classify_model_name='PP-LCNet_x1_0_doc_ori',

            doc_unwarping_model_dir=os.path.join(weights_file, 'UVDoc'),  # 文字矫正
            doc_unwarping_model_name='UVDoc',

            textline_orientation_model_dir=os.path.join(weights_file, 'PP-LCNet_x1_0_textline_ori'), # 单行文本方向分类 单行文字旋转问题
            textline_orientation_model_name='PP-LCNet_x1_0_textline_ori',
            textline_orientation_batch_size=1,

            # ocr_version='PP-OCRv5',
            # lang='en',
        )

    def extract_min_rectangle(self, regions: list):
        '''提取最小矩形,且检测框在最小矩形内'''
        x = [x[0] for i in regions for x in i]
        y = [x[1] for i in regions for x in i]
        min_x, min_y = min(x), min(y)
        max_x, max_y = max(x), max(y)
        return min_x, min_y, max_x, max_y


    def crop_img__add_border(self, image, regions):
        '''裁剪图并加边框(不加边框识别时因为文字太大导致不能识别)'''
        x1, y1, x2, y2 = self.extract_min_rectangle(regions)
        crop_image = image[y1:y2, x1:x2]  # 裁剪图
        crop_image = cv2.copyMakeBorder(
            crop_image,
            top=50,  # 上边框50像素
            bottom=50,  # 下边框50像素
            left=50,  # 左边框50像素
            right=50,  # 右边框50像素
            borderType=cv2.BORDER_CONSTANT,  # 固定颜色边框
            value=(255, 255, 255)  # 边框颜色:纯白色
        )
        return crop_image


    def ocr_predict(self, image, regions, scores_threshold):
        '''识别'''
        if regions:
            image = self.crop_img__add_border(image, regions) # 裁剪图并加边框

        res = self.ocr.predict(
            image,
            use_doc_orientation_classify=False,  # 文档整体方向分类 比如你拍的合同是 90° 横过来的、扫描件是 180° 倒过来的),开启后会自动把文档旋转回正常方向再识别。
            use_doc_unwarping=False,  # 文档弯曲矫正 比如拍的书本内页有弧度、褶皱的纸质文档拍照后文字变形),开启后会先把弯曲的文档还原成平整的,再识别文字。
            use_textline_orientation=False,  # 文档里既有横排文字,又有竖排文字;某一行文字是 90° 旋转的),开启后会逐行矫正文字方向,避免把「竖排的字」识别成乱码。

            text_det_limit_side_len=300,  # 核心:适中尺寸,平衡精度和速度
            text_det_limit_type='min',  # 缩放最小边到300,放大小数字区域

            text_det_thresh=0.3,  # 判定「单个像素是否属于文字区域」的置信度
            text_det_box_thresh=0.5,  # 判定「整个文字框是否有效」的置信度临界值
            text_det_unclip_ratio=1.5,  # 检测框的膨胀系数 检测出的文本框通常偏小,值越大,框越大
            text_rec_score_thresh=scores_threshold,  # 判定「识别结果是否有效」的临界值

            return_word_box=False,  # 是否返回单字级别的检测框
        )
        text = ''.join(res[0]['rec_texts']) # 识别结果
        logger.success(f'ocr结果:【{text}】')
        return text

通过网盘分享的文件:飞浆V5模型
链接: https://pan.baidu.com/s/1TJUUmbZbQF_HMtRmNCC6LA 提取码: 1111


依赖

# python==3.7.3
paddleocr==2.7.0.2
paddlepaddle==2.5.2
loguru==0.7.3
from paddleocr import PaddleOCR
import cv2
import numpy as np

if __name__ == '__main__':
    OCR = PaddleOCR(
        use_doc_orientation_classify=False,  # 检测文档方向
        use_doc_unwarping=False,  # 矫正扭曲文档
        use_textline_orientation=False)  # 识别文本行方向
    
    image_path = r'C:\Users\123\Desktop\2.jpg'
    image = cv2.imread(image_path)
    result = OCR.ocr(image_path, cls=False)
    for box in result[0]:
        print(f'区域:【{box[0]}】,结果:【{box[1]}】')
        points = np.array(box[0], dtype=np.int32).reshape((-1, 1, 2))
        cv2.polylines(image, [points], isClosed=True, color=(0, 0, 255), thickness=1)

    cv2.imshow('', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

在这里插入图片描述

源码下载
链接: https://pan.baidu.com/s/1RwxzllNdwhiICHKGpIu2wQ 提取码: cvfr

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐