ndfweb.cn

paddleocr package使用說明


2021-08-02 15:19:50 (5027)



快速上手

1.1 安裝whl包

pip安裝

pip install "paddleocr>=2.0.1" # 推薦使用2.0.1+版本

本地構建並安裝

python3 setup.py bdist_wheelpip3 install dist/paddleocr-x.x.x-py3-none-any.whl # x.x.x是paddleocr的版本號

2 使用

2.1 代碼使用

paddleocr whl包會自動下載ppocr輕量級模型作為默認模型,可以根據第3節自定義模型進行自定義更換。

  • 檢測+方向分類器+識別全流程

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 memoryimg_path = 'PaddleOCR/doc/imgs/11.jpg'result = ocr.ocr(img_path, cls=True)for line in result:    print(line)# 顯示結果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='/path/to/PaddleOCR/doc/fonts/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_ocrocr = PaddleOCR() # need to run only once to download and load model into memoryimg_path = 'PaddleOCR/doc/imgs/11.jpg'result = ocr.ocr(img_path)for line in result:    print(line)# 顯示結果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='/path/to/PaddleOCR/doc/fonts/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 PaddleOCRocr = PaddleOCR(use_angle_cls=True) # need to run only once to download and load model into memoryimg_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_ocrocr = PaddleOCR() # need to run only once to download and load model into memoryimg_path = 'PaddleOCR/doc/imgs/11.jpg'result = ocr.ocr(img_path, rec=False)for line in result:    print(line)# 顯示結果from PIL import Imageimage = Image.open(img_path).convert('RGB')im_show = draw_ocr(image, result, txts=None, scores=None, font_path='/path/to/PaddleOCR/doc/fonts/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 PaddleOCRocr = PaddleOCR() # need to run only once to download and load model into memoryimg_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 PaddleOCRocr = PaddleOCR(use_angle_cls=True) # need to run only once to download and load model into memoryimg_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]

2.2 通過命令行使用

查看幫助信息

paddleocr -h
  • 檢測+方向分類器+識別全流程

paddleocr --image_dir PaddleOCR/doc/imgs/11.jpg --use_angle_cls true

結果是一個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]]......
  • 檢測+識別

paddleocr --image_dir PaddleOCR/doc/imgs/11.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]]......
  • 方向分類器+識別

paddleocr --image_dir PaddleOCR/doc/imgs_words/ch/word_1.jpg --use_angle_cls true --det false

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

['韓國小館', 0.9907421]
  • 單獨執行檢測

paddleocr --image_dir PaddleOCR/doc/imgs/11.jpg --rec false

結果是一個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]]......
  • 單獨執行識別

paddleocr --image_dir PaddleOCR/doc/imgs_words/ch/word_1.jpg --det false

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

['韓國小館', 0.9907421]
  • 單獨執行方向分類器

paddleocr --image_dir PaddleOCR/doc/imgs_words/ch/word_1.jpg --use_angle_cls true --det false --rec false

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

['0', 0.9999924]

3 自定義模型

當內置模型無法滿足需求時,需要使用到自己訓練的模型。 首先,參照inference.md 第一節轉換將檢測、分類和識別模型轉換為inference模型,然後按照如下方式使用

3.1 代碼使用

from paddleocr import PaddleOCR, draw_ocr# 模型路徑下必須含有model和params文件ocr = PaddleOCR(det_model_dir='{your_det_model_dir}', rec_model_dir='{your_rec_model_dir}', rec_char_dict_path='{your_rec_char_dict_path}', cls_model_dir='{your_cls_model_dir}', use_angle_cls=True)img_path = 'PaddleOCR/doc/imgs/11.jpg'result = ocr.ocr(img_path, cls=True)for line in result:    print(line)# 顯示結果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='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.jpg')

3.2 通過命令行使用

paddleocr --image_dir PaddleOCR/doc/imgs/11.jpg --det_model_dir {your_det_model_dir} --rec_model_dir {your_rec_model_dir} --rec_char_dict_path {your_rec_char_dict_path} --cls_model_dir {your_cls_model_dir} --use_angle_cls true

4 使用網絡圖片或者numpy數組作為輸入

4.1 網絡圖片

  • 代碼使用

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 memoryimg_path = 'http://n.sinaimg.cn/ent/transform/w630h933/20171222/o111-fypvuqf1838418.jpg'result = ocr.ocr(img_path, cls=True)for line in result:    print(line)# 顯示結果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='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.jpg')
  • 命令行模式

paddleocr --image_dir http://n.sinaimg.cn/ent/transform/w630h933/20171222/o111-fypvuqf1838418.jpg --use_angle_cls=true

4.2 numpy數組

僅通過代碼使用時支持numpy數組作為輸入

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 memoryimg_path = 'PaddleOCR/doc/imgs/11.jpg'img = cv2.imread(img_path)# img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY), 如果你自己訓練的模型支持灰度圖,可以將這句話的注釋取消result = ocr.ocr(img, cls=True)for line in result:    print(line)# 顯示結果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='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.jpg')

5 參數說明

字段說明默認值
use_gpu是否使用GPUTRUE
gpu_mem初始化占用的GPU內存大小8000M
image_dir通過命令行調用時執行預測的圖片或文件夾路徑
det_algorithm使用的檢測算法類型DB
det_model_dir檢測模型所在文件夾。傳參方式有兩種,1. None: 自動下載內置模型到 ~/.paddleocr/det;2.自己轉換好的inference模型路徑,模型路徑下必須包含model和params文件None
det_max_side_len檢測算法前向時圖片長邊的最大尺寸,當長邊超出這個值時會將長邊resize到這個大小,短邊等比例縮放960
det_db_threshDB模型輸出預測圖的二值化閾值0.3
det_db_box_threshDB模型輸出框的閾值,低於此值的預測框會被丟棄0.5
det_db_unclip_ratioDB模型輸出框擴大的比例2
det_east_score_threshEAST模型輸出預測圖的二值化閾值0.8
det_east_cover_threshEAST模型輸出框的閾值,低於此值的預測框會被丟棄0.1
det_east_nms_threshEAST模型輸出框NMS的閾值0.2
rec_algorithm使用的識別算法類型CRNN
rec_model_dir識別模型所在文件夾。傳參方式有兩種,1. None: 自動下載內置模型到 ~/.paddleocr/rec;2.自己轉換好的inference模型路徑,模型路徑下必須包含model和params文件None
rec_image_shape識別算法的輸入圖片尺寸"3,32,320"
rec_char_type識別算法的字符類型,中英文(ch)、英文(en)、法語(french)、德語(german)、韓語(korean)、日語(japan)ch
rec_batch_num進行識別時,同時前向的圖片數30
max_text_length識別算法能識別的最大文字長度25
rec_char_dict_path識別模型字典路徑,當rec_model_dir使用方式2傳參時需要修改為自己的字典路徑./ppocr/utils/ppocr_keys_v1.txt
use_space_char是否識別空格TRUE
drop_score對輸出按照分數(來自於識別模型)進行過濾,低於此分數的不返回0.5
use_angle_cls是否加載分類模型FALSE
cls_model_dir分類模型所在文件夾。傳參方式有兩種,1. None: 自動下載內置模型到 ~/.paddleocr/cls;2.自己轉換好的inference模型路徑,模型路徑下必須包含model和params文件None
cls_image_shape分類算法的輸入圖片尺寸"3, 48, 192"
label_list分類算法的標簽列表['0', '180']
cls_batch_num進行分類時,同時前向的圖片數30
enable_mkldnn是否啟用mkldnnFALSE
use_zero_copy_run是否通過zero_copy_run的方式進行前向FALSE
lang模型語言類型,目前支持 目前支持中英文(ch)、英文(en)、法語(french)、德語(german)、韓語(korean)、日語(japan)ch
det前向時使用啟動檢測TRUE
rec前向時是否啟動識別TRUE
cls前向時是否啟動分類 (命令行模式下使用use_angle_cls控製前向是否啟動分類)FALSE


本文版权:http://www.ndfweb.cn/news-885.html
  NDF俱乐部
  国际域名注册
  建站咨询
简体中文 NDF网站建设淘宝店 | ICO图标在线生成 | 外贸网站建设 | 联系我们
©2007-2024 NDF Corporation 鲁ICP备08005967号 Sitemap - RSSRSS订阅