日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

适合小白的几个入门级Python ocr识别库

發布時間:2024/8/1 python 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 适合小白的几个入门级Python ocr识别库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

適合小白的幾個入門級Python ocr識別庫

  • 1、pytesseract
  • 2、PaddleOCR
  • 3、easyocr
  • 4、muggle_ocr
  • 5、dddd_ocr
  • 6、其他

工作生活中經常會遇到需要提取圖片中文字信息的情況,以前都是手動自己把圖片里的字敲出來,但隨著這幾年人工智能技術的愈發成熟,市面上有越來越多的ocr產品了,基本上能大部分正常圖片的文字提取需求。當然有時候需要提取文字的圖片數量較多或者有某個應用程序編寫需求時,就需要借助代碼來實現了,這里介紹幾個比較適合新手小白的python ocr庫,簡單實用,可滿足絕大多數常規的圖片文字提取、驗證碼識別需求。

1、pytesseract

pytesseract需要配合安裝在本地的tesseract-ocr.exe文件一起使用,tesseract-ocr.exe安裝教程可參考這里:Tesseract Ocr文字識別,需要注意的是安裝時一定要選中中文包,默認是只支持英文識別。

python庫安裝命令如下:

pip install pytesseract

待識別圖片如下:

代碼實現:

import pytesseract from PIL import Image text = pytesseract.image_to_string(Image.open(r"d:\Desktop\39DEE621-40EA-4ad1-90CC-79EB51D39347.png")) print(text)

識別結果輸出:

Using Tesseract OCR with Python # import the necessary packages from PIL import Image import pytesseract import ergperse import cv2 import os# construct the argument parse and parse the arguments ap = argparse.ArgunentParser() ap.add_argument("-i", "--image", required-True, help="path to input image to be OCR'd") ap.add_argument("-p", "--preprocess", typesstr, default="thresh", helpe"type of preprocessing to be done") args = vars (ap.parse_args())

2、PaddleOCR

PaddleOCR是百度開源的一款基于深度學習的ocr識別庫,對中文的識別精度相當不錯,可以應付絕大多數的文字提取需求。

需要依次安裝三個依賴庫,安裝命令如下,其中shapely庫可能會受系統影響安裝報錯,具體解決方案參考這篇博客:百度OCR(文字識別)服務使用入坑指南

pip install paddlepaddle pip install shapely pip install paddleocr

待識別圖片如下:

代碼實現:

ocr = PaddleOCR(use_angle_cls=True, lang="ch")# 輸入待識別圖片路徑img_path = r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png"# 輸出結果保存路徑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)im_show = Image.fromarray(im_show)im_show.show()

識別結果輸出如下,會顯示出每個區域字體識別的置信度,以及其坐標位置信息:

Namespace(cls=False, cls_batch_num=30, cls_image_shape='3, 48, 192', cls_model_dir='C:\\Users\\Administrator/.paddleocr/cls', cls_thresh=0.9, det=True, det_algorithm='DB', det_db_box_thresh=0.5, det_db_thresh=0.3, det_db_unclip_ratio=2.0, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_max_side_len=960, det_model_dir='C:\\Users\\Administrator/.paddleocr/det', enable_mkldnn=False, gpu_mem=8000, image_dir=None, ir_optim=True, label_list=['0', '180'], lang='ch', max_text_length=25, rec=True, rec_algorithm='CRNN', rec_batch_num=30, rec_char_dict_path='./ppocr/utils/ppocr_keys_v1.txt', rec_char_type='ch', rec_image_shape='3, 32, 320', rec_model_dir='C:\\Users\\Administrator/.paddleocr/rec/ch', use_angle_cls=True, use_gpu=True, use_space_char=True, use_tensorrt=False, use_zero_copy_run=False) dt_boxes num : 16, elapse : 0.04799485206604004 cls num : 16, elapse : 0.1860027313232422 rec_res num : 16, elapse : 0.4859299659729004 [[[6.0, 2.0], [85.0, 2.0], [85.0, 31.0], [6.0, 31.0]], ['幫助文檔', 0.99493873]] [[[309.0, 13.0], [324.0, 13.0], [324.0, 28.0], [309.0, 28.0]], ['X', 0.9667116]] [[[82.0, 50.0], [120.0, 50.0], [120.0, 71.0], [82.0, 71.0]], ['目錄', 0.993418]] [[[136.0, 50.0], [176.0, 50.0], [176.0, 71.0], [136.0, 71.0]], ['標題', 0.99969745]] [[[13.0, 53.0], [60.0, 53.0], [60.0, 70.0], [13.0, 70.0]], ['快捷鍵', 0.9995322]] [[[191.0, 49.0], [314.0, 49.0], [314.0, 72.0], [191.0, 72.0]], ['文本樣式列表', 0.9967863]] [[[61.0, 84.0], [120.0, 84.0], [120.0, 101.0], [61.0, 101.0]], ['代碼片', 0.9997086]] [[[134.0, 81.0], [181.0, 84.0], [180.0, 104.0], [132.0, 101.0]], ['表格', 0.9891155]] [[[187.0, 84.0], [232.0, 84.0], [232.0, 101.0], [187.0, 101.0]], ['注腳', 0.99958]] [[[13.0, 115.0], [90.0, 115.0], [90.0, 135.0], [13.0, 135.0]], ['自定義列表', 0.99823236]] [[[109.0, 115.0], [219.0, 115.0], [219.0, 135.0], [109.0, 135.0]], ['LaTeX數學公式', 0.98812836]] [[[237.0, 115.0], [315.0, 115.0], [315.0, 135.0], [237.0, 135.0]], ['插入甘特圖', 0.9982792]] [[[12.0, 148.0], [94.0, 148.0], [94.0, 167.0], [12.0, 167.0]], ['插入UML圖', 0.9926085]] [[[113.0, 148.0], [249.0, 148.0], [249.0, 167.0], [113.0, 167.0]], ['插入Mermaid流程圖', 0.996088]] [[[11.0, 176.0], [153.0, 176.0], [153.0, 200.0], [11.0, 200.0]], ['插入Flowchart流程圖', 0.9780351]] [[[174.0, 179.0], [237.0, 179.0], [237.0, 200.0], [174.0, 200.0]], ['插入類圖', 0.9519753]]

3、easyocr

github上一萬多個star的開源ocr項目(github地址:EasyOCR),支持80多種語言的識別,識別精度超高。

python庫安裝命令如下:

pip install easyocr

待識別圖片如下:

代碼實現:

import easyocr#設置識別中英文兩種語言 reader = easyocr.Reader(['ch_sim','en'], gpu = False) # need to run only once to load model into memory result = reader.readtext(r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png", detail = 0) print(result)

初次運行需要在線下載檢測模型和識別模型,建議在網速好點的環境運行:

Using CPU. Note: This module is much faster with a GPU. Downloading detection model, please wait. This may take several minutes depending upon your network connection. Downloading recognition model, please wait. This may take several minutes depending upon your network connection.

識別結果輸出如下,沒有遺漏任何一個文字,精度甚至要優于前面的PaddleOCR:

['幫助文檔', '快捷鍵', '目錄', '標題', '文本樣式', '列表', '鏈接', '代碼片', '表格', '注腳', '注釋', '自定義列表', 'LaTex 數學公式', '插入甘犄圖', '插入UML圖', '插入Mernaid流程圖', '插入 Flowchart流程圖', '插入類圖']

4、muggle_ocr

muggle_ocr是一款輕量級的ocr識別庫,從名字也可以看出來,專為麻瓜設計!使用也非常簡單,但其強項主要是用于識別各類驗證碼,一般文字提取效果就稍差了。

python庫安裝命令如下:

pip install muggle_ocr

待識別驗證碼如下:

代碼實現:

import muggle_ocr# 初始化sdk;model_type 包含了 ModelType.OCR/ModelType.Captcha 兩種模式,分別對應常規圖片與驗證碼 sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)with open(r"d:\Desktop\四位驗證碼.png", "rb") as f:img = f.read()text = sdk.predict(image_bytes=img) print(text)

識別結果輸出如下:

MuggleOCR Session [captcha] Loaded. 3n3d

5、dddd_ocr

dddd_ocr也是一個用于識別驗證碼的開源庫,又名帶帶弟弟ocr,爬蟲界大佬sml2h3開發,識別效果也是非常不錯,對一些常規的數字、字母驗證碼識別有奇效。

python庫安裝命令如下:

pip install dddd_ocr

待識別驗證碼如下:

代碼實現:

import ddddocrocr = ddddocr.DdddOcr()with open("d:\Desktop\四位驗證碼2.png", 'rb') as f:img_bytes = f.read()res = ocr.classification(img_bytes)print(res)

識別結果輸出如下,可以看出即使有一些線條干擾,還是準確的識別出了四個字母:

jepv

6、其他

還有其他優秀的ocr識別庫,以后慢慢更新

總結

以上是生活随笔為你收集整理的适合小白的几个入门级Python ocr识别库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。