本期將介紹並演示PaddleOCR+Python+OpenCV實現車牌識別、身份證信息識別和車票信息識別的步驟與效果。
介紹
百度深度學習框架PaddlePaddle開源的OCR項目PaddleOCR近期霸榜github。使用測試后發現識別效果很好,對於簡單的應用(車票車牌身份證等),直接用項目提供的模型即可使用。特殊應用,可自己訓練后使用。
gituhub地址:https://github.com/PaddlePaddle/PaddleOCR
效果展示
分別以車牌識別、身份證信息識別和車票信息識別為例,測試效果如下視頻:
實現步驟
PaddleOCR是基於百度的深度學習框架PaddlePaddle實現的,所以第一步我們需要先安裝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對應文件,另外遇到的安裝問題網上也可以找到答案,我的安裝步驟到此結束。
代碼演示
代碼演示前需要先下載PaddleOCR提供的訓練好的模型共3個,我是Win10 PC端使用下載下面三個,如果是移動端下載上面三個。
github提供的Demo如下將會保存一張識別結果圖:
from paddleocr import PaddleOCR import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont from paddleocr import PaddleOCR, draw_ocr font=cv2.FONT_HERSHEY_SIMPLEX # Paddleocr目前支持中英文、英文、法語、德語、韓語、日語,可以通過修改lang參數進行切換 # 參數依次為`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 memory img_path = './imgs/B.jpg' result = ocr.ocr(img_path, cls=True) # 顯示結果 from PIL import Image image = 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')
識別輸出信息:
輸出結果圖:
我們把輸出結果部分改成OpenCV實現:
from paddleocr import PaddleOCR import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont font=cv2.FONT_HERSHEY_SIMPLEX # Paddleocr目前支持中英文、英文、法語、德語、韓語、日語,可以通過修改lang參數進行切換 # 參數依次為`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 memory def 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 img print('---------------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()
輸出結果圖:
傾斜也可以自動識別:
車票識別: