[OCR][Python]測試tesseract與easyOCR誰比較準跟快

閱讀時間約 17 分鐘

平時都在用tesseract來辨識OCR的部分,在網路上也常常聽說easyOCR比tesseract好用,就拿之前測試的OCR素材來比較看看囉。

以下輸入同樣圖片直接測試,並非絕對誰就比較準,只單純測試數字含英文的部分。

圖片素材就是15碼(英文加數字),檔名為OCR正確結果

保密原則,只好模糊掉數字跟英文

保密原則,只好模糊掉數字跟英文



Tesseract 測試結果

raw-image


Easy OCR 測試結果

raw-image


Tesseract 程式範例

輸入的圖片檔名,OCR_數字為正確的OCR

讀圖辨識結果與檔名比較,得知誤判率

import os
import pytesseract
from PIL import Image
import re
import time
import numpy as np
from collections import Counter
import cv2

def find_errors(ocr_results, file_names):
error_details = []

for ocr, file_name in zip(ocr_results, file_names):
if ocr != file_name:
error_positions = [(i, a, b) for i, (a, b) in enumerate(zip(ocr, file_name)) if a != b]
error_details.append({"ocr": ocr, "file_name": file_name, "error_positions": error_positions})

return error_details

def rename_images():
res_str = 0
ocr_results = [] # 你的OCR結果清單
file_names = [] # 你的檔案名稱清單
all_time = 0
all_count = 0
'''
Change File Name -> OCR.Png
'''
folder_path = "D:/SaveImg/PSN/20240531"
valid_extensions = [".jpg", ".png", ".jpeg"]

pytesseract.pytesseract.tesseract_cmd = r"tesseract路徑"
config = f'--oem 3 --psm 7'
image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and any(f.lower().endswith(ext) for ext in valid_extensions)]
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and f.lower().endswith(".png")]

for i, (image_file, file_1) in enumerate(zip(image_files, files)):

_, extension = os.path.splitext(image_file)
original_path = os.path.join(folder_path, image_file)
file = file_1.split('_')[0]
# 使用 pytesseract 讀取文字
try:
start_time = time.time()
img = cv2.imread(original_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
labels = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
ocr_text = pytesseract.image_to_string(labels, lang="eng", config=config)
print(ocr_text)
except Exception as e:
print(f"Error reading text from {image_file}: {e}")
continue

ocr_text = ocr_text.strip()
# print(f'辨識到的OCR:{ocr_text}')
ocr_results.append(ocr_text) # 你的OCR結果清單
file_names.append(file) # 你的檔案名稱清單
if ocr_text == file:
res_str += 1

end_time = time.time()
elapsed_time = end_time - start_time
all_time += elapsed_time
print(f"處理 第{i} 張圖片共花費 {elapsed_time:.2f} 秒")

print(f'準確率:{(res_str/(i + 1) )*100}%,總共測試了{i + 1}張,誤判{((i + 1)-res_str)}張')

error_details = find_errors(ocr_results, file_names)
print("錯誤字母詳細資訊:")
for error in error_details:
print(f"OCR: {error['ocr']}, 檔案名稱: {error['file_name']}, 錯誤位置: {error['error_positions']}")
# 統計重複的錯誤位置及其次數
error_positions_counter = Counter([pos for error in error_details for pos in error["error_positions"]])
print("錯誤位置及其次數:")
for position, count in error_positions_counter.items():
print(f"位置 {position}: {count} 次")
all_count += count
print(f'總花費時間:{all_time}秒,單測項平均時間:{round((all_time/i),3)}秒')
print(f'誤判率 :{round(all_count/(i*15),2)*100}%')
print(f'總字元數:{i*15},誤判總字元數:{all_count}')

if __name__ == "__main__":
rename_images()

Easy OCR程式範例

import os
from PIL import Image
import re
import time
import numpy as np
from collections import Counter
import easyocr
import cv2

def find_errors(ocr_results, file_names):
error_details = []

for ocr, file_name in zip(ocr_results, file_names):
if ocr != file_name:
error_positions = [(i, a, b) for i, (a, b) in enumerate(zip(ocr, file_name)) if a != b]
error_details.append({"ocr": ocr, "file_name": file_name, "error_positions": error_positions})

return error_details

def rename_images():
res_str = 0
ocr_results = [] # 你的OCR結果清單
file_names = [] # 你的檔案名稱清單
all_time = 0
all_count = 0
count = 0
'''
Change File Name -> OCR.Png
'''
folder_path = "圖片路徑"
valid_extensions = [".jpg", ".png", ".jpeg"]
reader = easyocr.Reader(['ch_sim','en'], gpu=False) # this needs to run only once to load the model into memory

image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and any(f.lower().endswith(ext) for ext in valid_extensions)]
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and f.lower().endswith(".png")]

for i, (image_file, file_1) in enumerate(zip(image_files, files)):

_, extension = os.path.splitext(image_file)
original_path = os.path.join(folder_path, image_file)
file = file_1.split('_')[0]
# 使用 pytesseract 讀取文字
try:
start_time = time.time()
img = cv2.imread(original_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
labels = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
ocr_text = reader.readtext(labels, detail = 0)
print(ocr_text)
except Exception as e:
print(f"Error reading text from {image_file}: {e}")
continue
# 使用 join 方法將列表轉換為字符串
result_string = ''.join(ocr_text)
# print(f'辨識到的OCR:{ocr_text}')
ocr_results.append(result_string) # 你的OCR結果清單
file_names.append(file) # 你的檔案名稱清單
if ocr_text == file:
res_str += 1

end_time = time.time()
elapsed_time = end_time - start_time
all_time += elapsed_time
print(f"處理 第{i} 張圖片共花費 {elapsed_time:.2f} 秒")

print(f'準確率:{(res_str/(i + 1) )*100}%,總共測試了{i + 1}張,誤判{((i + 1)-res_str)}張')

error_details = find_errors(ocr_results, file_names)
print("錯誤字母詳細資訊:")
for error in error_details:
print(f"OCR: {error['ocr']}, 檔案名稱: {error['file_name']}, 錯誤位置: {error['error_positions']}")
# 統計重複的錯誤位置及其次數
error_positions_counter = Counter([pos for error in error_details for pos in error["error_positions"]])
print("錯誤位置及其次數:")
for position, count in error_positions_counter.items():
print(f"位置 {position}: {count} 次")
all_count += count
print(f'總花費時間:{all_time}秒,單測項平均時間:{round((all_time/i),3)}秒')
print(f'誤判率 :{round(all_count/(i*15),2)*100}%')
print(f'總字元數:{i*15},誤判總字元數:{all_count}')

if __name__ == "__main__":
rename_images()



52會員
110內容數
Python程式設計師,不是在學習就是在學習的路上
留言0
查看全部
發表第一個留言支持創作者!
你可能也想看
What is OCR? Discover the Advantages and Applications of OCROCR (Optical Character Recognition) technology revolutionizes the conversion of texts from physical documents into digital data, enhancing processing
avatar
Anice H.
2024-05-14
光學字元辨識(OCR)技術:提升數位轉型的關鍵利器什麼是 OCR 光學字元辨識技術?它是當今數位時代中不可或缺的重要工具之一,能夠將紙本文件、圖片或 PDF 中的文字快速、準確地轉換成數位檔案,從而極大地提升了資料處理效率,成為各行各業數位轉型的關鍵利器。不僅如此,OCR 技術還能有效減少人為錯誤,幫助企業提升整體營運效率,從而在市場競爭中取得優勢
avatar
Anice H.
2024-05-05
Python中的函式操作們(上)今天來介紹python的函式 函式在python中是非常重要的一環,因為到了後期,程式會越來越複雜。 而函式可以想成是容易管理的小程式,當我們需要使用時,只需呼叫即可。
Thumbnail
avatar
媗日
2024-04-25
Python四大容器大解析古有四大名著,現今Python四大容器🤣 哪四個?list串列,tuple元組,dict字典,set集合。 那這四個怎麼分? 一起來看看吧! (以下有手寫與上機實際測試請付費觀看) 以上我精心整理主要會使用到的功能 當然python功能太多了,肯定不只。 實際操作: 大概就這樣?(
Thumbnail
avatar
媗日
2024-04-06
【Python】Mac平台上的Jython和JES安裝指導先來名詞解釋jython跟JES: jython是一種實現了Python語言的Java平台版本的解釋器。它允許開發人員在Java虛擬機(JVM)上運行Python代碼,從而實現了Python語言與Java平台的無縫集成。 JES(Jython Environment for Students)是
Thumbnail
avatar
W. C. Chen
2024-03-27
【Python】Python在ETL處理的事先準備 - CSV、Excel、SQLite和SQLAlchemyETL是資料倉儲領域中一個重要的概念,全稱為Extract-Transform-Load,中文可譯為"抽取-轉換-載入"。ETL的作用是將來自不同來源的資料抽取出來,經過清理、轉換、整合等處理後,最終將處理好的資料載入到資料倉儲或其他單一的資料存放區
Thumbnail
avatar
W. C. Chen
2024-03-25
Tesseract OCR - 繁體中文【安裝篇】Tesseract OCR 光學字元辨識
Thumbnail
avatar
匿名李
2022-02-28