I need to implement an accurate text detection algorithm for extracting text from an image. Currently, I am using EasyOCR, but the results are not satisfactory. For instance, when processing the image, the expected output is
부동산 매매 계약서
but the code produces
부 동 산 떼 더 겨 약 서
Therefore, I need to explore alternative methods to improve the accuracy of text recognition.
Is it possible to increase the resolution and apply EasyOCR? Can anyone solve this problem?
!pip install easyocr
import cv2
import easyocr
import numpy as np
def extract_text_from_image(image):
# Read the image
#image = cv2.imread(image_path)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding to create a binary image
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours in the binary image
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Find the contour with the maximum area (foreground)
max_contour = max(contours, key=cv2.contourArea)
# Create a mask for the foreground contour
mask = np.zeros_like(binary)
cv2.drawContours(mask, [max_contour], 0, 255, -1)
# Apply the mask to the original image
preprocessed_image = cv2.bitwise_and(image, image, mask=mask)
# Initialize the EasyOCR reader
reader = easyocr.Reader(['en','ko'])
# Convert the preprocessed image to grayscale
preprocessed_gray = cv2.cvtColor(preprocessed_image, cv2.COLOR_BGR2GRAY)
# Perform OCR on the preprocessed image
results = reader.readtext(preprocessed_gray)
# Concatenate the extracted text into a single line
extracted_text = ''
for result in results:
extracted_text += result[1] + ' '
return extracted_text.strip()
img = cv2.imread(file_path)
text = extract_text_from_image(img)