基于OpenCV實(shí)現(xiàn)文字識(shí)別步驟與代碼展示
點(diǎn)擊上方“小白學(xué)視覺(jué)”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時(shí)間送達(dá)
本期將介紹并演示PaddleOCR+Python+OpenCV實(shí)現(xiàn)車牌識(shí)別、身份證信息識(shí)別和車票信息識(shí)別的步驟與效果。
百度深度學(xué)習(xí)框架PaddlePaddle開源的OCR項(xiàng)目PaddleOCR近期霸榜github。使用測(cè)試后發(fā)現(xiàn)識(shí)別效果很好,對(duì)于簡(jiǎn)單的應(yīng)用(車票車牌身份證等),直接用項(xiàng)目提供的模型即可使用。特殊應(yīng)用,可自己訓(xùn)練后使用。

PaddleOCR是基于百度的深度學(xué)習(xí)框架PaddlePaddle實(shí)現(xiàn)的,所以第一步我們需要先安裝PaddlePaddle模塊。直接使用pip安裝即可:
——指令:pip install paddlepaddle
第二步:安裝PaddleOCR。同樣是pip安裝:
——GPU版安裝:
python -m pip install paddlepaddle-gpu==2.0.0 -i https://mirror.baidu.com/pypi/simple——CPU版安裝:
python -m pip install paddlepaddle==2.0.0 -i https://mirror.baidu.com/pypi/simple如果要在GPU模式下使用除了有GPU外還需要安裝CUDA 10.1和CUDNN對(duì)應(yīng)文件,另外遇到的安裝問(wèn)題網(wǎng)上也可以找到答案,我的安裝步驟到此結(jié)束。
代碼演示前需要先下載PaddleOCR提供的訓(xùn)練好的模型共3個(gè),我們是Win10 PC端使用下載下面三個(gè),如果是移動(dòng)端下載上面三個(gè)。


github提供的Demo如下將會(huì)保存一張識(shí)別結(jié)果圖:
from paddleocr import PaddleOCRimport cv2import numpy as npfrom PIL import Image, ImageDraw, ImageFontfrom paddleocr import PaddleOCR, draw_ocrfont=cv2.FONT_HERSHEY_SIMPLEX# Paddleocr目前支持中英文、英文、法語(yǔ)、德語(yǔ)、韓語(yǔ)、日語(yǔ),可以通過(guò)修改lang參數(shù)進(jìn)行切換# 參數(shù)依次為`ch`, `en`, `french`, `german`, `korean`, `japan`。ocr = PaddleOCR(use_angle_cls=True, lang="ch",use_gpu=False,rec_model_dir='./models/ch_ppocr_server_v2.0_rec_infer/',cls_model_dir='./models/ch_ppocr_mobile_v2.0_cls_infer/',det_model_dir='./models/ch_ppocr_server_v2.0_det_infer/') # need to run only once to download and load model into memoryimg_path = './imgs/B.jpg'result = ocr.ocr(img_path, cls=True)# 顯示結(jié)果from PIL import Imageimage = Image.open(img_path).convert('RGB')boxes = [line[0] for line in result]txts = [line[1][0] for line in result]scores = [line[1][1] for line in result]im_show = draw_ocr(image, boxes, txts, scores, font_path='./simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.png')


我們把輸出結(jié)果部分改成OpenCV實(shí)現(xiàn):
from paddleocr import PaddleOCRimport cv2import numpy as npfrom PIL import Image, ImageDraw, ImageFontfont=cv2.FONT_HERSHEY_SIMPLEX# Paddleocr目前支持中英文、英文、法語(yǔ)、德語(yǔ)、韓語(yǔ)、日語(yǔ),可以通過(guò)修改lang參數(shù)進(jìn)行切換# 參數(shù)依次為`ch`, `en`, `french`, `german`, `korean`, `japan`。ocr = PaddleOCR(use_angle_cls=True, lang="ch",use_gpu=False,rec_model_dir='./models/ch_ppocr_server_v2.0_rec_infer/',cls_model_dir='./models/ch_ppocr_mobile_v2.0_cls_infer/',det_model_dir='./models/ch_ppocr_server_v2.0_det_infer/') # need to run only once to download and load model into memorydef putText_Chinese(img,strText,pos,color,fontSize):fontpath = "./simsun.ttc" # <== 這里是宋體路徑font = ImageFont.truetype(fontpath, fontSize)img_pil = Image.fromarray(img)draw = ImageDraw.Draw(img_pil)draw.text(pos,strText, font=font, fill=color)img = np.array(img_pil)return imgprint('---------------PaddleOCR Start---------------------')img_path = './pics/18.jpg'img = cv2.imread(img_path)cv2.imshow("src", img)result = ocr.ocr(img_path, cls=True)#print(result)for line in result:print('----------------------------')print(line)pt1 = ((int)(line[0][0][0]),(int)(line[0][0][1]))pt2 = ((int)(line[0][1][0]),(int)(line[0][1][1]))pt3 = ((int)(line[0][2][0]),(int)(line[0][2][1]))pt4 = ((int)(line[0][3][0]),(int)(line[0][3][1]))cv2.line(img,pt1,pt2,(0,0,255),1,cv2.LINE_AA)cv2.line(img,pt2,pt3,(0,0,255),1,cv2.LINE_AA)cv2.line(img,pt3,pt4,(0,0,255),1,cv2.LINE_AA)cv2.line(img,pt1,pt4,(0,0,255),1,cv2.LINE_AA)img = putText_Chinese(img,line[1][0],(pt1[0],pt1[1]-35),(255,0,255),50)cv2.imshow("OCR-Result", img)cv2.imwrite("result.png", img)cv2.waitKey()cv2.destroyAllWindows()






交流群
歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺(jué)、傳感器、自動(dòng)駕駛、計(jì)算攝影、檢測(cè)、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競(jìng)賽等微信群(以后會(huì)逐漸細(xì)分),請(qǐng)掃描下面微信號(hào)加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三?+?上海交大?+?視覺(jué)SLAM“。請(qǐng)按照格式備注,否則不予通過(guò)。添加成功后會(huì)根據(jù)研究方向邀請(qǐng)進(jìn)入相關(guān)微信群。請(qǐng)勿在群內(nèi)發(fā)送廣告,否則會(huì)請(qǐng)出群,謝謝理解~

