百度飛槳PaddleOCR文本提取簡單使用及draw_ocr函數(shù)報錯問題解決
百度飛槳PaddleOCR文本識別簡單使用
一、前言
PaddleOCR自發(fā)布以來憑借學術前沿算法和產(chǎn)業(yè)落地實踐,受到了產(chǎn)學研各方的喜愛,并被廣泛應用于眾多知名開源項目,例如:Umi-OCR、OmniParser、MinerU、RAGFlow等,已成為廣大開發(fā)者心中的開源OCR領域的首選工具。2025年5月20日,飛槳團隊發(fā)布PaddleOCR 3.0,全面適配飛槳框架3.0正式版,進一步提升文字識別精度,支持多文字類型識別和手寫體識別,滿足大模型應用對復雜文檔高精度解析的旺盛需求,結合文心大模型4.5 Turbo顯著提升關鍵信息抽取精度,并新增對昆侖芯、昇騰等國產(chǎn)硬件的支持。(摘自PaddleOCR 文檔)
二、安裝(Python 13)
這里選用cpu版本,安裝paddlepaddle
python -m pip install paddlepaddle==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/
安裝paddleOCR
pip install paddleocr
三、paddleOCR
基本文本提取分為兩個模塊一個是檢測文本(檢測文本位置),另一個是識別文本(識別文本內(nèi)容)
- 文本檢測
from paddleocr import TextDetection
model = TextDetection()
output = model.predict("C:\\Users\\Lenovo\\Pictures\\Screenshots\\屏幕截圖 2025-07-10 101653.png")
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
- 文本識別
from paddleocr import TextRecognition
model = TextRecognition()
output = model.predict(input="C:\\Users\\Lenovo\\Pictures\\Screenshots\\屏幕截圖 2025-07-10 101653.png")
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
- 結合起來
from paddleocr import PaddleOCR
ocr = PaddleOCR(
use_doc_orientation_classify=False, # 通過 use_doc_orientation_classify 參數(shù)指定不使用文檔方向分類模型
use_doc_unwarping=False, # 通過 use_doc_unwarping 參數(shù)指定不使用文本圖像矯正模型
use_textline_orientation=False, # 通過 use_textline_orientation 參數(shù)指定不使用文本行方向分類模型
)
# ocr = PaddleOCR(lang="en") # 通過 lang 參數(shù)來使用英文模型
# ocr = PaddleOCR(ocr_version="PP-OCRv4") # 通過 ocr_version 參數(shù)來使用 PP-OCR 其他版本
# ocr = PaddleOCR(device="gpu") # 通過 device 參數(shù)使得在模型推理時使用 GPU
# ocr = PaddleOCR(
# text_detection_model_name="PP-OCRv5_server_det",
# text_recognition_model_name="PP-OCRv5_server_rec",
# use_doc_orientation_classify=False,
# use_doc_unwarping=False,
# use_textline_orientation=False,
# ) # 更換 PP-OCRv5_server 模型
result = ocr.predict("C:\\Users\\Lenovo\\Pictures\\Screenshots\\2.png")
for res in result:
res.print()
res.save_to_img("C:\\Users\\Lenovo\\Pictures\\Screenshots")
#res.save_to_json("output")
以上代碼來自(使用教程 - PaddleOCR 文檔)
四、代碼拓展(代碼運行時間較長請耐心等待)
現(xiàn)在將代碼拓展為可以從文件資源管理器選取文件,并指定存儲位置
from paddleocr import PaddleOCR
import tkinter as tk
from tkinter import filedialog
import os
def select_image():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="選擇要識別的圖片",
filetypes=[("圖片文件", "*.png *.jpg *.jpeg *.bmp")]
)
return file_path
def select_output_dir():
root = tk.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(title="選擇結果保存目錄")
return dir_path if dir_path else "./output" # 默認輸出目錄
if __name__ == "__main__":
image_path = select_image()
if not image_path:
print("未選擇圖片,程序退出")
exit()
output_dir = select_output_dir()
# 初始化OCR并處理圖片
ocr = PaddleOCR(
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
result = ocr.predict(image_path)
for res in result:
res.print()
filename = os.path.splitext(os.path.basename(image_path))
img_output_path = os.path.join(output_dir, f"{filename}_ocr_res.png")
res.save_to_img(img_output_path)
print(f"圖片結果已保存至: {img_output_path}")
導入必要的庫:PaddleOCR 用于 OCR 識別,tkinter 用于文件對話框,os 用于路徑操作
-
定義兩個 GUI 函數(shù):
- select_image():打開圖片選擇對話框
- select_output_dir():打開目錄選擇對話框
主程序邏輯: a. 讓用戶選擇圖片文件 b. 讓用戶選擇輸出目錄 c. 初始化 PaddleOCR 引擎(禁用文檔方向識別等非必要功能) d. 執(zhí)行 OCR 識別 e. 處理結果:打印結果并保存為圖片
五、代碼運行效果

六、注意點
- 文件路徑地址不能直接復制如"C:\Users\Lenovo\Downloads\general_ocr_002.png",要修改為"C:\Users\Lenovo\Downloads\general_ocr_002.png"
- 當路徑包含中文時,OpenCV無法正確識別路徑,會出現(xiàn)亂碼并報錯,可以通過Windows設置--->時間和語言--->語言和區(qū)域--->管理語言設置--->更改系統(tǒng)區(qū)域設置--->勾選beta版就ok了
draw_ocr函數(shù)報錯問題解決(雖然我沒有用到)
在瀏覽官網(wǎng)文件之前,我先去看了幾篇大佬的文章比如
python+paddleocr 進行圖像識別、找到文字在屏幕中的位置_python paddleocr-CSDN博客
PaddleOCR—圖片文字識別提取—快速使用教程_paddleocr使用教程-CSDN博客
maybe我太蠢了,程序花了好久都沒能調(diào)通,中途也求助了ai(大概花了我7個米的tokens)都沒能搞好,其中一個問題就是
cannot import name 'PaddleOCR' from 'paddleocr'
這個問題我也在社區(qū)搜索過了cannot import name ‘PaddleOCR‘ from ‘paddleocr‘_cannot import name 'paddleocr' from partially init-CSDN博客,但是我犯錯的原因可能與他們不同,無論怎么修改,錯誤一直存在,困擾了我良久,最后在github社區(qū)找到一個評論(現(xiàn)在突然找不到了),就是說draw_ocr
在utility.py中,而這個文件按上述方法好像沒有下載(也有可能是版本問題),就一直報錯,之后我就到https://github.com/PaddlePaddle/PaddleOCR/blame/7b759798887596629e83c01436d5256543805060/tools/infer/utility.py#L208下載,再放到paddocr文件夾中,然后在開頭重新導入
from paddleocr.utility import draw_ocr
最終解決了問題。。。
以上就是所有內(nèi)容,歡迎各位戴佬批評指正