# detecting mirror text by applying OCR on original and after flipping the image
from PIL import Image
import pytesseract
import numpy as np
import matplotlib.pyplot as plt
import cv2
def count_chars(res):
ct = 0
for i in range(len(res["text"])):
if res["conf"][i]<70:
continue
word = res["text"][i]
print(word)
ct += len(word)
return ct
def is_mirror(results_org, results_mirror):
print("Original Image:")
ct_org = count_chars(results_org)
print("\nMirrored Image:")
ct_mirror = count_chars(results_mirror)
if ct_org<ct_mirror:
return True
else:
return False
def preprocess(img):
norm_img = np.zeros((img.shape[0], img.shape[1]))
img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]
img = cv2.GaussianBlur(img, (1,1), 0)
return img
def draw_Bbox(img, results):
img = img.copy()
for i in range(0, len(results["text"])):
x = results["left"][i]
y = results["top"][i]
w = results["width"][i]
h = results["height"][i]
text = results["text"][i]
conf = int(results["conf"][i])
if conf > 70:
text = "".join([c if ord(c) < 128 else "" for c in text]).strip()
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 200), 2)
return img
f = 'img1.jpeg'
img = cv2.imread(f)
mirror_img = np.fliplr(img)
results_org = pytesseract.image_to_data(img, output_type = 'dict')
results_mirror = pytesseract.image_to_data(mirror_img, output_type = 'dict')
img_org = draw_Bbox(img, results_org)
img_mirror = draw_Bbox(mirror_img, results_mirror)
print("\nIs image mirrored:",is_mirror(results_org, results_mirror))
fig = plt.figure()
ax1 = fig.add_subplot(3,1,1)
ax1.imshow(img_org)
ax2 = fig.add_subplot(3,1,2)
ax2.imshow(img_mirror)
ax3 = fig.add_subplot(3,1,3)
ax3.imshow(mirror_img)
plt.show()