找回密碼
 註冊
搜索
查看: 935|回復: 0

[教學] Python 如何用Pytesseract OCR 辨識影像的字元

[複製鏈接]
發表於 2023-11-10 17:55:18 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
1. 到 Github 下載 tesseract-ocr-w64-setup-5.3.3.20231005.exe 來安裝Tesseract。

2. 記錄Tesseract安裝的路徑,預設路徑通常為 C:\Program Files\Tesseract-OCR。

3. 將Tesseract.exe路徑新增到環境變數 "PATH"

4. 安裝pytesseract
  1. pip install pytesseract
複製代碼



代碼
  1. import cv2
  2. import pytesseract
  3. import matplotlib.pyplot as plt
  4. import matplotlib
  5. matplotlib.use('TkAgg') #加入這行

  6. # 載入圖檔
  7. image = cv2.imread('./images_ocr/receipt.png')

  8. # 顯示圖檔
  9. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  10. plt.figure(figsize=(10,6))
  11. plt.imshow(image_RGB)
  12. plt.axis('off')
  13. plt.show()


  14. # 參數設定
  15. custom_config = r'--psm 6'
  16. # OCR 辨識
  17. print(pytesseract.image_to_string(image, config=custom_config))

  18. # 參數設定,只辨識數字
  19. custom_config = r'--psm 6 outputbase digits'
  20. # OCR 辨識
  21. print(pytesseract.image_to_string(image, config=custom_config))


  22. # 參數設定白名單,只辨識有限字元
  23. custom_config = r'-c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz --psm 6'
  24. # OCR 辨識
  25. print(pytesseract.image_to_string(image, config=custom_config))


  26. # 參數設定黑名單,只辨識有限字元
  27. custom_config = r'-c tessedit_char_blacklist=abcdefghijklmnopqrstuvwxyz --psm 6'
  28. # OCR 辨識
  29. print(pytesseract.image_to_string(image, config=custom_config))

  30. # 載入圖檔
  31. image = cv2.imread('./images_ocr/chinese.png')

  32. # 顯示圖檔
  33. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  34. plt.figure(figsize=(10,6))
  35. plt.imshow(image_RGB)
  36. plt.axis('off')
  37. plt.show()

  38. # 辨識多國文字,中文繁體、日文及英文
  39. custom_config = r'-l chi_tra+jpn+eng --psm 6'
  40. # OCR 辨識
  41. print(pytesseract.image_to_string(image, config=custom_config))

  42. # 載入圖檔
  43. image = cv2.imread('./images_ocr/chinese_2.png')

  44. # 顯示圖檔
  45. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  46. plt.figure(figsize=(10,6))
  47. plt.imshow(image_RGB)
  48. plt.axis('off')
  49. plt.show()

  50. # 辨識多國文字,中文繁體、日文及英文
  51. custom_config = r'-l chi_tra+jpn+eng --psm 6'
  52. # OCR 辨識
  53. print(pytesseract.image_to_string(image, config=custom_config))
複製代碼


結果圖:


Python Pytesseract OCR 辨識數字

Python  Pytesseract OCR 辨識數字


文章出處: NetYea 網頁設計

 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-5-10 15:10 , Processed in 0.025268 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表