版上各位大神好,
小弟最近在研究OCR辨識的相關技術,
自己搜尋版上以及其他網站的教學並嘗試自己試做,
目前只要不管是以實際拍照或者螢幕截圖的方式,
只要遇到「數字」皆會出現無法識別的問題...
有經過二值化、色彩對比、亮度等圖片調整,再進行判讀,
目前嘗試使用pytesseract及easyocr兩種套件進行練習皆會遇到這樣的疑問,
程式碼
圖1
from PIL import Image
import pytesseract, cv2, numpy as np, easyocr, datetime, os, requests
path = "img"
jpg = "{}/original/3333.jpg".format(path)
img = cv2.imread(jpg, cv2.IMREAD_REDUCED_GRAYSCALE_2)
img = img[105:165, 115:600]
contrast, brightness = 500, 0
output = img * (contrast/127 + 1) - contrast + brightness
ret, output = cv2.threshold(output, 127, 255, cv2.THRESH_BINARY_INV)
af_img = "{}/after_treatment/{}.jpg".format(path, datetime.datetime.now().strftime("%Y%m%d"))
cv2.imwrite(af_img, output, [cv2.IMWRITE_JPEG_QUALITY, 80])
reader.readtext(af_img)
圖2
from PIL import Image
import pytesseract, cv2, numpy as np, easyocr, datetime, os, requests
path = "img"
reader = easyocr.Reader(['en'])
jpg = "{}/original/2222.jpg".format(path)
now_date, now_datetime = datetime.datetime.now().strftime("%Y-%m-%d"), datetime.datetime.now().strftime("%H : %M")
def img_ocr(img):
img = cv2.imread(img, cv2.IMREAD_REDUCED_COLOR_8)
img = img[230:270, 115:190]
# 曝光& 亮度調整
contrast, brightness = 200, 0
output = img * (contrast/127 + 1) - contrast + brightness
output = 255-output
af_img = "{}/after_treatment/{}.jpg".format(path, datetime.datetime.now().strftime("%Y%m%d"))
cv2.imwrite(af_img, output, [cv2.IMWRITE_JPEG_QUALITY, 80])
result = reader.readtext(af_img)[0][1].replace(" ","")
return "{} 度".format(result)
原始圖片
圖1
圖2
處理後
圖1
圖2
※由於圖片內容包含其他資訊不便公開,因此僅供自行剪裁後的圖片。
※程式小白,傷眼抱歉,敬請叮嚀指教!
給你兩個方向