I am working on a text recognition project which needs to detect and recognise text from the image. There are two short lines text in the image (320px * 320 px). The first line is the abbreviation of country code. The second line is the dialling code. The whole image can be rotated in an arbitrary angle. Below are some examples.
image one
image two
image three
Because the text is very short, method like hough transform (detect long line), fourier transform and profile projection cannot perform well. I am using contour detection to detect the angle of text block. However, it cannot work well if the text block is triangular. Moreover, text will become upside down, left-side down and right-side down after de-skewing if the text block is rectangular. Can somebody suggest?
file = r"/home/hank/Desktop/af_36.jpg"
image = cv2.imread(os.path.normpath(file))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
dilation = cv2.dilate(thresh, kernel, iterations=1)
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [contours[i] for i in range(len(contours)) if
not (hierarchy[0][i][3] >= 0 and hierarchy[0][i][2] == -1)]
angles = []
for cnt in contours:
rect = cv2.minAreaRect(cnt)
angles.append(rect[2])
angle = sum(angles)/len(angles)
print(angle)