I have the photo in which I wanted to get the co-ordinate of text and highlight the text.
Text to highlight and get co-ordinate = 'was the age of wisdom'
I try to get the co-ordinate by providing the first and last word but didn't get the required solution
import regex as re
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
import cv2
import matplotlib.pyplot as plt
filename = 'C:\\Users\\vicky\\Downloads\\image.png'
img = cv2.imread(filename)
from pytesseract import Output
d = pytesseract.image_to_data(img, output_type=Output.DICT)
#print(d)
n_boxes = len(d['level'])
word1 = 'was'
word2 = 'wisdom,'
for i in range(n_boxes):
#print(d['text'][i]) ## each word
if(d['text'][i] == word1):
(x, y) = (d['left'][i], d['top'][i])
#print(x,y)
if(d['text'][i] == word2):
(x1, y1,w1,h1) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
#print(x1, y1, w1, h1)
print(f'The coordinates of text are ({x}, {y}) and ({x1+w1}, {y1+h1})')
cv2.rectangle(img, (x, y), (x1 + w1, y1 + h1), (0, 0, 139), 5)
break
cv2.imwrite('result2.png', img)
The resulted image I got
If anybody know any other approach than this and also want to highlight and fill the text with pale color.