paddleocr的使用

import paddlehub as hub
# 加载移动端预训练模型
# ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
# 服务端可以加载大模型,效果更好
import cv2
import os
from math import *

def r(image,degree):
    height, width = image.shape[:2]
    # 旋转后的尺寸
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

    matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

    matRotation[0, 2] += (widthNew - width) / 2  # 重点在这步,目前不懂为什么加这步
    matRotation[1, 2] += (heightNew - height) / 2  # 重点在这步

    image = cv2.warpAffine(image, matRotation, (widthNew, heightNew), borderValue=(0, 0, 0))
    return image
#D:\MASKpicture\datas\huhui\Mask R-CNN\test_pig  C:\Users\Administrator\Documents\WXWork\1688849977344659\Cache\File\2020-10\picture
pathss = r'C:\Users\Administrator\Documents\WXWork\1688849977344659\Cache\File\2020-10\picture'
def OCr():
    ocr = hub.Module(name="chinese_ocr_db_crnn_server")
    # 读取测试文件夹test.txt中的照片路径
    os.environ['CUDA_VISIBLE_DEVICES'] = '1'
    np_images = [cv2.imread(os.path.join(pathss,path)) for path in os.listdir(pathss)]
    # Cards = [cv2.imread(r'C:\Users\Administrator\Desktop\untitled\f\i\15.jpg')]
    # np_images = [Cards,Cards]
    results = ocr.recognize_text(
                        images=np_images,         # 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
                        use_gpu=True,            # 是否使用 GPU;若使用GPU,请先设置CUDA_VISIBLE_DEVICES环境变量
                        output_dir=r'C:\Users\Administrator\Desktop\untitled\f\ocr_result',  # 图片的保存路径,默认设为 ocr_result;
                        visualization=False,       # 是否将识别结果保存为图片文件;
                        box_thresh=0.2,           # 检测文本框置信度的阈值;
                        text_thresh=0.2)          # 识别中文文本置信度的阈值;
    return results

# print(results,'********************')
def gettexts():
    results = OCr()
    result = results[0]
    data = result['data']
    texts = []
    for infomation in data:
        texts.append(infomation['text'])
    return texts
import time
start_time = time.time()
f = open('earlabel_spin.txt','w')
for dir in os.listdir(r'C:\Users\Administrator\Documents\WXWork\1688849977344659\Cache\File\2020-10\a'):
    image = cv2.imread(os.path.join(r'C:\Users\Administrator\Documents\WXWork\1688849977344659\Cache\File\2020-10\a',dir))
    paths = r'C:\Users\Administrator\Documents\WXWork\1688849977344659\Cache\File\2020-10\picture\1.jpg'
    cv2.imwrite(paths,image)
    flag = False
    texts = gettexts()
    for text in texts:
        flag |= text.startswith('0')
    if flag == False:
        image = cv2.imread(paths)
        image = r(image,180)
        cv2.imwrite(paths,image)
        texts = gettexts()
        for text in texts:
            flag |= text.startswith('0')
        if flag == False:
            image = cv2.imread(paths)
            image = r(image, 90)
            cv2.imwrite(paths, image)
            texts = gettexts()
            for text in texts:
                flag |= text.startswith('0')
            if flag == False:
                image = cv2.imread(paths)
                image = r(image, 270)
                cv2.imwrite(paths, image)
                texts = gettexts()
                for text in texts:
                    flag |= text.startswith('0')
    for text in texts:
        if text.startswith('0'):
            print(text)
            f.write(dir + ' ' + text + '\n')
            break
end_time = time.time()
print("花费时长为:", end_time - start_time)

        # print('text: ', infomation['text'])
    # print('*'*15)
        # print('text: ', infomation['text'], '\nconfidence: ', infomation['confidence'], '\ntext_box_position: ', infomation['text_box_position'])
    # cphm = data[0]['text']
    # cllx = data[1]['text']
    # syr = data[2]['text']
    # zz = data[3]['text']
    # syxz = data[4]['text']
    # ppxh = data[5]['text']
    # clsbdh = data[6]['text']
    # fdjhm = data[7]['text']
    # zcrq = data[8]['text']
    # fzrq = data[9]['text']
    # Dic = {'车牌号码':cphm,'车辆类型':cllx,'所有人':syr,'住址':zz,'使用性质':syxz,\
    #        '品牌型号':ppxh,'车辆识别代号':clsbdh,'发动机号码':fdjhm,'注册日期':zcrq,'发证日期':fzrq}
    # print(Dic)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值