Python OCR 圖片轉文字進階:讀光OCR之行檢測模型+行識別模型
- 介紹
- 阿里云文字識別OCR(讀光OCR)
- 前置條件
- 模型1:行檢測模型
- 模型1:行識別模型
- 代碼:main.py
介紹
什么是OCR?
OCR是“Optical Character Recognition”的縮寫,中文意為“光學字符識別”。它是一種技術,可以識別和轉換打印在紙張或圖像上的文字和字符為機器可處理的格式,如計算機文本文件。通過使用OCR技術,可以快速地將紙質文檔數字化,從而使文本可以被編輯、搜索和分析。這項技術廣泛應用于各種場合,如圖書館和檔案館的文獻數字化、 pdf 文件的文本搜索、以及掃描文檔中的條形碼和二維碼等。
阿里云文字識別OCR(讀光OCR)
阿里云文字識別OCR(讀光OCR),是一款由阿里巴巴達摩院打造的OCR產品,用于識別圖片、文檔、卡證等文件所包含的文字信息。
前置條件
1、準備電腦環境(我當前用的是 4060 顯卡)
2、安裝環境(conda、python)
3、下載模型(通過下方鏈接地址下載模型)
模型1:行檢測模型
https://www.modelscope.cn/models/iic/cv_resnet18_ocr-detection-db-line-level_damo/summary
模型1:行識別模型
https://www.modelscope.cn/models/iic/cv_convnextTiny_ocr-recognition-general_damo/summary
代碼:main.py
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasksfrom PIL import Image, ImageDraw# 加載行檢測模型
ocr_detection = pipeline(Tasks.ocr_detection, model='damo/cv_resnet18_ocr-detection-db-line-level_damo')# 目標圖片路徑
pic_path = 'ipp.jpg'
result = ocr_detection(pic_path)
print(result)# 多邊形坐標
polygons = result["polygons"]
fg_name = "cropped_image_"
# 打開圖片
image = Image.open(pic_path)# 創建新圖片
cropped_images = []# 遍歷每個多邊形
for i, polygon_coords in enumerate(polygons):# 獲取多邊形邊界框x_min, y_min = min(polygon_coords[::2]), min(polygon_coords[1::2])x_max, y_max = max(polygon_coords[::2]), max(polygon_coords[1::2])# 根據邊界框裁剪圖像cropped_region = image.crop((x_min, y_min, x_max, y_max))# 將裁剪后的圖片保存到列表中cropped_images.append(cropped_region)# 保存裁剪后的圖片
for i, cropped_image in enumerate(cropped_images):cropped_image.save(f"{fg_name}{i}.jpg")print("圖片已成功裁剪并保存。")# 加載行識別模型
ocr_recognition = pipeline(Tasks.ocr_recognition, model='damo/cv_convnextTiny_ocr-recognition-general_damo')# 循環識別獲取結果
for i, cropped_image in enumerate(cropped_images):img_url = f"{fg_name}{i}.jpg"result = ocr_recognition(img_url)print(result)
from modelscope.pipelines import pipeline
: 導入ModelScope的pipeline模塊,這個模塊允許用戶通過配置任務類型和模型來快速構建一個模型管道。from modelscope.utils.constant import Tasks
: 導入ModelScope的常量定義,其中Tasks是一個枚舉類型,定義了不同的任務類型。ocr_detection = pipeline(Tasks.ocr_detection, model='damo/cv_resnet18_ocr-detection-db-line-level_damo')
: 創建一個文本檢測的管道,使用的是damo/cv_resnet18_ocr-detection-db-line-level_damo這個模型。pic_path = 'ipp.jpg'
: 設置要處理的圖片的路徑。result = ocr_detection(pic_path)
: 將圖片路徑輸入到文本檢測管道中,得到檢測結果。polygons = result["polygons"]
: 從結果中提取多邊形坐標,這些坐標代表了文本區域的邊界。image = Image.open(pic_path)
: 使用Pillow庫打開指定的圖片。cropped_images = []
: 初始化一個空列表,用于存儲裁剪后的圖像。for i, polygon_coords in enumerate(polygons):
: 遍歷每個文本區域的多邊形坐標。x_min, y_min = min(polygon_coords[::2]), min(polygon_coords[1::2])
: 計算每個多邊形的最小x和y坐標。x_max, y_max = max(polygon_coords[::2]), max(polygon_coords[1::2])
: 計算每個多邊形的最小x和y坐標。cropped_region = image.crop((x_min, y_min, x_max, y_max))
: 根據計算出的邊界框裁剪圖像。cropped_images.append(cropped_region)
: 將裁剪后的圖像保存到列表中。for i, cropped_image in enumerate(cropped_images):
: 遍歷裁剪后的圖像列表。cropped_image.save(f"{fg_name}{i}.jpg")
: 將每個裁剪后的圖像保存到文件系統。print("圖片已成功裁剪并保存。")
: 打印成功消息。ocr_recognition = pipeline(Tasks.ocr_recognition, model='damo/cv_convnextTiny_ocr-recognition-general_damo')
: 創建一個文本識別的管道,使用的是damo/cv_convnextTiny_ocr-recognition-general_damo這個模型。for i, cropped_image in enumerate(cropped_images):
: 遍歷裁剪后的圖像列表。img_url = f"{fg_name}{i}.jpg"
: 為每個裁剪后的圖像創建一個文件路徑。result = ocr_recognition(img_url)
: 將裁剪后的圖像路徑輸入到文本識別管道中,得到識別結果。print(result)
: 打印識別結果。