天天看點

python OCR

from cnocr import CnOcr
import re


def ocr_front(path):
    ocr = CnOcr()
    s = ocr.ocr(path)
    text = ''
    for txt in s:
        text += txt['text']
    # print(text)
    dict_txt = {'國籍': text.split('居')[0],
                '證件類别': text.split('國')[1].split('簽')[0],
                text.split('證')[1][:4]: text.split('關')[1].split('有')[0],
                '有' + text.split('有')[1][:3]: text.split('限')[1]
                }
    return dict_txt


def ocr_back(path):


    ocr = CnOcr()
    s = ocr.ocr(path)
    text = ''
    for txt in s:
        text += txt['text']
    # print(text)
    dict_txt = {'姓名': text[text.find('名')+1:text.find('别')-1],
                '性别': text[text.find('别')+1:text.find('民族')],
                '出生': re.sub(r'\D', '', text[text.find('生')+1:text.find('住')]),
                '住址': text[text.find('址')+1:text.find('公民')],
                '公民身份号碼': text[text.find('份号碼')+3:text.find('份号碼')+21]
                }
    return dict_txt


if __name__ == '__main__':
    Idc_front = ocr_front('Id_Card_n.jpeg')
    Idc_back = ocr_back('123456.png')
    for key in Idc_front:
        print("{}:{}".format(key, Idc_front[key]))
    for key in Idc_back:
        print("{}:{}".format(key, Idc_back[key]))
           

cnocr再訓練:

一、擷取源代碼

git clone https://github.com/breezedeus/cnocr.git
           

二、準備訓練資料

python OCR

 引用+參考:CNOCR重訓練_蕭班的部落格-CSDN部落格_cnocr 訓練

如侵權,聯系删除