close

1. 到 Github 下載 tesseract-ocr-w64-setup-5.3.3.20231005.exe 來安裝Tesseract。

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

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

 

  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. # 載入圖檔
  8. image = cv2.imread('./images_ocr/receipt.png')
  9.  
  10. # 顯示圖檔
  11. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  12. plt.figure(figsize=(10,6))
  13. plt.imshow(image_RGB)
  14. plt.axis('off')
  15. plt.show()
  16.  
  17.  
  18. # 參數設定
  19. custom_config = r'--psm 6'
  20. # OCR 辨識
  21. print(pytesseract.image_to_string(image, config=custom_config))
  22.  
  23. # 參數設定,只辨識數字
  24. custom_config = r'--psm 6 outputbase digits'
  25. # OCR 辨識
  26. print(pytesseract.image_to_string(image, config=custom_config))
  27.  
  28.  
  29. # 參數設定白名單,只辨識有限字元
  30. custom_config = r'-c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz --psm 6'
  31. # OCR 辨識
  32. print(pytesseract.image_to_string(image, config=custom_config))
  33.  
  34.  
  35. # 參數設定黑名單,只辨識有限字元
  36. custom_config = r'-c tessedit_char_blacklist=abcdefghijklmnopqrstuvwxyz --psm 6'
  37. # OCR 辨識
  38. print(pytesseract.image_to_string(image, config=custom_config))
  39.  
  40. # 載入圖檔
  41. image = cv2.imread('./images_ocr/chinese.png')
  42.  
  43. # 顯示圖檔
  44. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  45. plt.figure(figsize=(10,6))
  46. plt.imshow(image_RGB)
  47. plt.axis('off')
  48. plt.show()
  49.  
  50. # 辨識多國文字,中文繁體、日文及英文
  51. custom_config = r'-l chi_tra+jpn+eng --psm 6'
  52. # OCR 辨識
  53. print(pytesseract.image_to_string(image, config=custom_config))
  54.  
  55. # 載入圖檔
  56. image = cv2.imread('./images_ocr/chinese_2.png')
  57.  
  58. # 顯示圖檔
  59. image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  60. plt.figure(figsize=(10,6))
  61. plt.imshow(image_RGB)
  62. plt.axis('off')
  63. plt.show()
  64.  
  65. # 辨識多國文字,中文繁體、日文及英文
  66. custom_config = r'-l chi_tra+jpn+eng --psm 6'
  67. # OCR 辨識
  68. print(pytesseract.image_to_string(image, config=custom_config))
複製代碼



結果圖:
Python 如何用Pytesseract OCR 辨識影像

Python 如何用Pytesseract OCR 辨識影像

文章出處: NetYea 網頁設計

arrow
arrow

    NetYea 網頁設計 發表在 痞客邦 留言(0) 人氣()