__init__.py
665 Bytes
from cv2 import (
imread,
cvtColor,
COLOR_BGR2GRAY,
GaussianBlur,
threshold,
THRESH_BINARY,
)
from pytesseract import image_to_string
def ocr(image, lang='eng', tess_opt='--psm 6'):
if isinstance(image, str):
image = imread(image) # Color
gray = cvtColor(image, COLOR_BGR2GRAY) # Grayscale
blurred = GaussianBlur(gray, (3, 3), 0) # Smoothing
ret, thresh = threshold(blurred, 127, 255, THRESH_BINARY) # Black & white
height, width, _ = image.shape
width = int(width*7/10)
cropped = thresh[0:height, 0:width] # Buang tanda tangan
return image_to_string((cropped), lang=lang, config=tess_opt)