天天看點

PaddleOCR使用

目錄

    • 一 概述
    • 二 快速安裝
      • 1 win10 python安裝
      • 2 C++編譯
      • 3 生成dll,c#調用
    • 三 OCR模型清單
      • 1 模型差別
      • 2 模型分類
    • 四 中文OCR模型快速使用
      • 1 以超輕量級模型為例:
      • 2 單張圖像或者圖像集合預測
    • 五 paddleocr package使用說明
      • 1 安裝whl包
      • 2 代碼使用
    • 六 DB 場景文字檢測
    • 七 快速了解文本識别模型CRNN
    • 八 SLIM模型
    • 九 其他開源項目
    • 十 VS 運作庫MT、MD的差別

一 概述

  項目源碼:https://github.com/PaddlePaddle/PaddleOCR。

  項目說明:https://mp.weixin.qq.com/s/i1Dm18qp93jzWnMoqRU2gA。

二 快速安裝

  本節内容來自這裡。

1 win10 python安裝

1. 安裝PaddlePaddle Fluid v2.0

pip3 install --upgrade pip

如果您的機器安裝的是CUDA9或CUDA10,請運作以下指令安裝

python3 -m pip install paddlepaddle-gpu==2.0.0b0 -i https://mirror.baidu.com/pypi/simple

如果您的機器是CPU,請運作以下指令安裝

python3 -m pip install paddlepaddle==2.0.0b0 -i https://mirror.baidu.com/pypi/simple

更多的版本需求,請參照安裝文檔中的說明進行操作。

2. 克隆PaddleOCR repo代碼

【推薦】git clone https://github.com/PaddlePaddle/PaddleOCR

如果因為網絡問題無法pull成功,也可選擇使用碼雲上的托管:

git clone https://gitee.com/paddlepaddle/PaddleOCR

注:碼雲托管代碼可能無法實時同步本github項目更新,存在3~5天延時,請優先使用推薦方式。

3. 安裝第三方庫

cd PaddleOCR

pip3 install -r requirments.txt

  注意,windows環境下,建議從這裡下載下傳shapely安裝包完成安裝, 直接通過pip安裝的shapely庫可能出現[winRrror 126] 找不到指定子產品的問題。

2 C++編譯

  參考清單:

https://blog.csdn.net/u010477528/article/details/109078267

  環境:

1、C++預測庫fluid_inference:https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/05_inference_deployment/inference/windows_cpp_inference.html

2、opencv

3、cmake 3.17.5選項:

PaddleOCR使用
PaddleOCR使用

4、vs2019,Release編譯。

3 生成dll,c#調用

  系列文章 如何使用PaddleDetection做一個完整項目(三)

  PaddleDetection在windows下的部署(二)

  設定:

PaddleOCR使用

三 OCR模型清單

  本節内容來自這裡。

1 模型差別

  PaddleOCR提供的可下載下傳模型包括推理模型、訓練模型、預訓練模型、slim模型,模型差別說明如下:

模型類型 模型格式 簡介
推理模型 model、params 用于python預測引擎推理,詳情
訓練模型、預訓練模型 .pdmodel、.pdopt、*.pdparams 訓練過程中儲存的checkpoints模型,儲存的是模型的參數,多用于模型名額評估和恢複訓練
slim模型 *.nb 用于lite部署

2 模型分類

  文本檢測模型、文本識别模型、文本方向分類模型

四 中文OCR模型快速使用

  本節内容來自這裡。

1 以超輕量級模型為例:

mkdir inference && cd inference
# 下載下傳超輕量級中文OCR模型的檢測模型并解壓
wget https://paddleocr.bj.bcebos.com/20-09-22/mobile/det/ch_ppocr_mobile_v1.1_det_infer.tar && tar xf ch_ppocr_mobile_v1.1_det_infer.tar
# 下載下傳超輕量級中文OCR模型的識别模型并解壓
wget https://paddleocr.bj.bcebos.com/20-09-22/mobile/rec/ch_ppocr_mobile_v1.1_rec_infer.tar && tar xf ch_ppocr_mobile_v1.1_rec_infer.tar
# 下載下傳超輕量級中文OCR模型的文本方向分類器模型并解壓
wget https://paddleocr.bj.bcebos.com/20-09-22/cls/ch_ppocr_mobile_v1.1_cls_infer.tar && tar xf ch_ppocr_mobile_v1.1_cls_infer.tar
cd ..
           

  解壓完畢後應有如下檔案結構:

|-inference
    |-ch_ppocr_mobile_v1.1_det_infer
        |- model
        |- params
    |-ch_ppocr_mobile_v1.1_rec_infer
        |- model
        |- params
    |-ch_ppocr_mobile-v1.1_cls_infer
        |- model
        |- params
    ...
           

2 單張圖像或者圖像集合預測

  以下代碼實作了文本檢測、識别串聯推理,在執行預測時,需要通過參數

image_dir

指定單張圖像或者圖像集合的路徑、參數

det_model_dir

指定檢測inference模型的路徑、參數

rec_model_dir

指定識别inference模型的路徑、參數

use_angle_cls

指定是否使用方向分類器、參數

cls_model_dir

指定方向分類器inference模型的路徑、參數

use_space_char

指定是否預測空格字元。可視化識别結果預設儲存到

./inference_results

檔案夾裡面。

# 預測image_dir指定的單張圖像
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v1.1_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v1.1_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v1.1_cls_infer/" --use_angle_cls=True --use_space_char=True

# 預測image_dir指定的圖像集合
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/" --det_model_dir="./inference/ch_ppocr_mobile_v1.1_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v1.1_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v1.1_cls_infer/" --use_angle_cls=True --use_space_char=True

# 如果想使用CPU進行預測,需設定use_gpu參數為False
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v1.1_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v1.1_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v1.1_cls_infer/" --use_angle_cls=True --use_space_char=True --use_gpu=False
           
  • 通用中文OCR模型

  請按照上述步驟下載下傳相應的模型,并且更新相關的參數,示例如下:

# 預測image_dir指定的單張圖像
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_server_v1.1_det_infer/"  --rec_model_dir="./inference/ch_ppocr_server_v1.1_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v1.1_cls_infer/" --use_angle_cls=True --use_space_char=True
           
  • 注意:

  如果希望使用不支援空格的識别模型,在預測的時候需要注意:請将代碼更新到最新版本,并添加參數

--use_space_char=False

  如果不希望使用方向分類器,在預測的時候需要注意:請将代碼更新到最新版本,并添加參數

--use_angle_cls=False

  更多的文本檢測、識别串聯推理使用方式請參考文檔教程中基于Python預測引擎推理。

五 paddleocr package使用說明

  本節内容來自這裡。

1 安裝whl包

pip安裝:

pip install paddleocr
           

本地建構并安裝:

python3 setup.py bdist_wheel
pip3 install dist/paddleocr-x.x.x-py3-none-any.whl # x.x.x是paddleocr的版本号
           

2 代碼使用

  • 檢測+分類+識别全流程
from paddleocr import PaddleOCR, draw_ocr
# Paddleocr目前支援中英文、英文、法語、德語、韓語、日語,可以通過修改lang參數進行切換
# 參數依次為`ch`, `en`, `french`, `german`, `korean`, `japan`。
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs/11.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
    print(line)

# 顯示結果
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='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
           

  結果是一個list,每個item包含了文本框,文字和識别置信度

[[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['純臻營養護發素', 0.964739]]
[[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['産品資訊/參數', 0.98069626]]
[[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起訂)', 0.9676722]]
......
           
  • 檢測+識别
from paddleocr import PaddleOCR, draw_ocr
ocr = PaddleOCR() # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs/11.jpg'
result = ocr.ocr(img_path)
for line in result:
    print(line)

# 顯示結果
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='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
           

  結果是一個list,每個item包含了文本框,文字和識别置信度

[[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['純臻營養護發素', 0.964739]]
[[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['産品資訊/參數', 0.98069626]]
[[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起訂)', 0.9676722]]
......
           
  • 分類+識别
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True) # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False, cls=True)
for line in result:
    print(line)
           

  結果是一個list,每個item隻包含識别結果和識别置信度

['南韓小館', 0.9907421]
           
  • 單獨執行檢測
from paddleocr import PaddleOCR, draw_ocr
ocr = PaddleOCR() # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs/11.jpg'
result = ocr.ocr(img_path, rec=False)
for line in result:
    print(line)

# 顯示結果
from PIL import Image

image = Image.open(img_path).convert('RGB')
im_show = draw_ocr(image, result, txts=None, scores=None, font_path='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
           

  結果是一個list,每個item隻包含文本框

[[26.0, 457.0], [137.0, 457.0], [137.0, 477.0], [26.0, 477.0]]
[[25.0, 425.0], [372.0, 425.0], [372.0, 448.0], [25.0, 448.0]]
[[128.0, 397.0], [273.0, 397.0], [273.0, 414.0], [128.0, 414.0]]
......
           
  • 單獨執行識别
from paddleocr import PaddleOCR
ocr = PaddleOCR() # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False)
for line in result:
    print(line)
           

  結果是一個list,每個item隻包含識别結果和識别置信度

['南韓小館', 0.9907421]
           
  • 單獨執行分類
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True) # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False, rec=False, cls=True)
for line in result:
    print(line)
           

  結果是一個list,每個item隻包含分類結果和分類置信度

['0', 0.9999924]
['0', 0.9999924]
           

六 DB 場景文字檢測

  本節内容來自這裡。

  文章Real-time Scene Text Detection with Differentiable Binarization 提出的文字檢測模型 DB 是 Differentiable Binarization 縮寫。

  基于分割的方法通常需要設着一個門檻值來判斷像素是否屬于文字區域。

B i , j = { 1   if  P i , j > = t , 0   otherwise . what t is the predefined threshold and (i, j) indicates the coordinate point in the map. B_{i,j} = \begin{cases} 1\ \ \text{if } P_{i,j} >= t, \\ 0\ \ \text{otherwise}. \\ \end{cases}\\ \text{what t is the predefined threshold and (i, j) indicates the coordinate point in the map.} Bi,j​={1  if Pi,j​>=t,0  otherwise.​what t is the predefined threshold and (i, j) indicates the coordinate point in the map.

  作者發現對每個像素分類的模型最終産生的機率分布會呈現出邊界比較高機率的樣子。是以為了讓分割效果更加穩定引入了監督資訊,

PaddleOCR使用

  引入的方式是在普通的像素級的分類模型上增加一個輔助分支,來動态預測每個點的分割的門檻值 Tij 然後用下面的公式來判斷某個像素是否屬于文字區域。

B ^ i , j = 1 1 + e − k ( P i , j − T i , j ) \hat{B}_{i,j}=\frac{1}{1+e^{-k(P_{i,j}-T_{i,j})}} B^i,j​=1+e−k(Pi,j​−Ti,j​)1​

  訓練的時候需要一個 thresh_map 的真值,這個真值的生成方式是把文本區域 G 擴大到G_d 計算 G_d内的點到 G 的邊的标準化的最小距離 dis, thresh_map 的值就是 1-dis , G 的邊界兩邊都是很高的門檻值,到 G 的中部會越來越小, 文章設着最大門檻值為0.7 最小為 0.3

  有了這個 thresh_map 和預測輸出的 P 代如公式二就能還原出哪些點是文本區域

  由于正負樣本通常是不均勻的,是以要對不是文本區域的像素做采樣,采樣的政策是和 OHEM 的方式類似,選取被預測文文字區域高的像素作為負樣本。

  最後最預測的時候并不需要門檻值的分支。有點兒類似增加了邊界像素權重的意思。主要的好處是省去了複雜的後處理過程。但是感覺比較難訓練,相對而言, PSENet 是預測多個縮小的文字區域,最後用廣度優先的方式來合并成一個文本區域,訓練會簡單些,隻是有個後處理步驟。他們都能對相鄰很近的文本區域有比較好的效果。

PaddleOCR使用

七 快速了解文本識别模型CRNN

  本節内容來自這裡。

八 SLIM模型

  SLIM推薦模型及分析:https://blog.csdn.net/nihaomafb/article/details/53741813

  詳解SLIM與GLSLIM推薦模型:https://blog.csdn.net/weixin_39064571/article/details/78835939

九 其他開源項目

  chineseocr_lite:https://github.com/ouyanghuiyu/chineseocr_lite。

  darknet-ocr:https://github.com/chineseocr/darknet-ocr。

  PaddleOCR和ChineseOCR的對比:https://blog.csdn.net/CHYabc123456hh/article/details/107846268

  easyocrhttps://www.easyproject.cn/easyocr/zh-cn/index.jsp。

  本節内容來自這裡。

  本節内容來自這裡。

十 VS 運作庫MT、MD的差別

  VC項目屬性→配置屬性→C/C++→代碼生成→運作時庫 可以采用的方式有:多線程(/MT)、多線程調試(/MTd)、多線程DLL(/MD)、多線程調試DLL(/MDd)、單線程(/ML)、單線程調試(/MLd)。

  目前Win7、Win10等,選擇MD問題不大,但是XP等一些比較老的系統需要選擇MT。

  兩者的差別:

  /MT是 "multithread, static version ” 意思是多線程靜态的版本,定義了它後,編譯器把LIBCMT.lib 安置到OBJ檔案中,讓連結器使用LIBCMT.lib 處理外部符号。

  /MD是 "multithread- and DLL-specific version” ,意思是多線程DLL版本,定義了它後,編譯器把 MSVCRT.lib 安置到OBJ檔案中,它連接配接到DLL的方式是靜态連結,實際上工作的庫是

MSVCR80.DLL

  即:

  靜态運作時庫:

LIBCMT.lib

  動态運作時庫:

MSVCRT.li

b +

MSVCR80.DLL

  是以,當你用CMAKE生成工程檔案時,若CMAKE是用/MT生成的(檢視工程原始目錄的CMakeLists.txt),則它所調用的運作時庫為:LIBCMT.lib,若生成的工程的運作時庫(Runtime Library)你選擇/MD,則此工程在編譯後連結的時候,将會調用動态運作時庫:MSVCRT.lib + MSVCR80.DLL,明顯,兩次對同一個某運作時庫裡的函數調用的庫不同,則會出現重定義的錯誤。若此工程生成的是庫檔案,則其他工程調用此庫時也必須是/MT。

  其他說明:

  其中以小寫“d”結尾的選項表示的DEBUG版本的,沒有“d”的為RELEASE版本。大型項目中必須要求所有元件和第三方庫的運作時庫是統一的,否則将會出現LNK2005井噴。

  單線程運作時庫選項/ML和/MLd在VS2003以後就被廢了。

繼續閱讀