TShopping

 找回密碼
 註冊
搜索
查看: 614|回復: 0
打印 上一主題 下一主題

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

[複製鏈接]
跳轉到指定樓層
1#
發表於 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))
複製代碼


結果圖:




文章出處: NetYea 網頁設計


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-5-3 02:28 , Processed in 0.067842 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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