項目背景
在當今數(shù)字化時代,圖像文字識別(Optical Character Recognition, OCR)技術的應用越來越廣泛。
OCR技術可以將印刷體文字轉化為可編輯的文本格式,從而方便進行文本分析、數(shù)據(jù)挖掘等操作。Python作為一種簡潔、易用的編程語言,提供了豐富的圖像處理和機器學習庫,使得實現(xiàn)圖像文字識別變得簡單而高效。
本文將介紹如何使用Python實現(xiàn)大批量識別圖片文字,并將文字保存到txt文檔中!
項目介紹
本項目使用的文字識別模型來自飛槳開源模型: chinese_ocr_db_crnn_server Module
該模型基于 chinese_text_detection_db_server 檢測得到的文本框,繼續(xù)識別文本框中的中文文字。之后對檢測文本框進行角度分類。最終識別文字算法采用 CRNN(Convolutional Recurrent Neural Network) 即卷積遞歸神經(jīng)網(wǎng)絡。其是DCNN和RNN的組合,專門用于識別圖像中的序列式對象。與CTC loss配合使用,進行文字識別,可以直接從文本詞級或行級的標注中學習,不需要詳細的字符級的標注。該Module是一個通用的OCR模型,支持直接預測。
項目主要功能:
- 支持識別多種圖片類型
support_type = ["bmp", "dib", "jpeg", "jpg", "jpe", "jp2", "png", "webp", "pbm", "pgm", "pxm", "pnm", "tiff", "tif"]
- 支持自定義圖片路徑
- 支持大批量的圖片識別
- 支持將每一張圖片的文字寫入對應的txt文件中
運行項目
第一步: 從文末的源碼地址,下載項目并解壓到本地電腦
第二步: 配置運行環(huán)境,使用pycharm導入項目,在【terminal】下運行下面命令:
pip install -r requirements.txt
如果下載第三方庫比較慢,可以考慮換一下pip的下載源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
阿里云 http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/
第三步: 運行前可以替換image_path為自定義的路徑,默認圖片保存位置為當前py文件下的image文件夾。
代碼詳解
1、導入需要的庫,paddlehub用于搭建模型,cv2用于讀取圖片,os模塊創(chuàng)建文件
import paddlehub as hub
import cv2
import warnings
import os
2、識別圖片,傳入圖片地址,,并將識別出的文字保存到對應的txt文本中
def ocr_images(images):
if images:
for r in images:
file_path = r.get("file_path")
ocr_result_txt_path = r.get("result_txt_path")
ocr = hub.Module(name="chinese_ocr_db_crnn_server", enable_mkldnn=True)
ocr_result = ocr.recognize_text(images=[cv2.imread(file_path)])
with open(ocr_result_txt_path, "w") as f:
for i in ocr_result[0]["data"]:
f.write(i["text"])
print(ocr_result_txt_path, "寫入完成!")
3、獲取指定路徑下的所有圖片地址,或者同級文件夾images中的所有圖片地址
def return_images_files(images_path=None):
# 返回指定路徑下的所有圖片絕對地址,如果沒有指定,當前路徑下的images文件夾
if images_path is None:
# 獲取當前 py 文件所在的絕對地址
cwd = os.getcwd()
# 拼接 images 文件夾地址
images_path = os.path.join(cwd, "images")
# 如果沒有 images 文件夾,就新建一個
if "images" not in os.listdir(cwd):
os.mkdir(images_path)
# 拼接 ocr_result 文件夾地址
ocr_result_path = os.path.join(images_path, "ocr_result")
if "ocr_result" not in os.listdir(images_path):
os.mkdir(ocr_result_path)
# 獲取文件夾下所有文件
files = [os.path.abspath(os.path.join(r, f)) for r, _, fs in os.walk(images_path) for f in fs]
# 判斷文件夾下是否有文件
if files:
result = []
for f in files:
f_type = f.split(".")[1]
# 判斷文件類型是否存在
if f_type in support_type:
# 返回圖片地址和識別結果txt的文件名
result_txt_name = f.split(images_path)[1].replace("\\", "").split(".")[0] + ".txt"
result_txt_path = os.path.join(ocr_result_path, result_txt_name)
result_dict = {
"file_path": f,
"result_txt_path": result_txt_path
}
result.append(result_dict)
return result
以上是部分源碼,,有需要的朋友可以在文末的源碼地址免費獲??!文章來源:http://www.zghlxwxcb.cn/news/detail-557001.html
源碼地址
鏈接:https://pan.baidu.com/s/1TRcxqttjnGfg6xPwnHuCuA?pwd=akxs
提取碼:akxs文章來源地址http://www.zghlxwxcb.cn/news/detail-557001.html
到了這里,關于Python實現(xiàn)OCR大批量識別圖片文字,并將文字保存到txt文檔中,文末源碼直接拿!的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!