'How to paint a rectangle over words?
I want rectangles to be painted over the original words and put new words on top of them, but when i do that the main rectangle is painted full screen, how to exclude it from painting?
https://i.stack.imgur.com/SmUca.jpg - before painting
https://i.stack.imgur.com/1KGqF.jpg - after painting
img = cv2.imread('C:\\Users\\booba\\PycharmProjects\\MySchoolProject\\21.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
config = r'--oem 3 --psm 4'
data = pytesseract.image_to_data(img, config=config, lang='eng')
for i, el in enumerate(data.splitlines()):
if i == 0:
continue
el = el.split()
try:
x, y, w, h = int(el[6]), int(el[7]), int(el[8]), int(el[9])
a = cv2.rectangle(img, (x,y), (w + x, h + y), (0, 0, 255), 2)
font = cv2.FONT_HERSHEY_COMPLEX
print(el[11])
cv2.putText(img, el[11], (x, y), font, 1, (0,0,0), 3)
cv2.putText(img, el[11], (x, y), font, 1, (255, 255, 255), 2)
except IndexError:
continue
Solution 1:[1]
You can check if a text is included in the line data:
import cv2
import pytesseract
img = cv2.imread('test-ocr.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
config = r'--oem 3 --psm 4'
data = pytesseract.image_to_data(img, config=config, lang='eng')
print(data)
for i, line in enumerate(data.splitlines()):
if i == 0:
# ignore header line
continue
el = line.split()
x, y, w, h = int(el[6]), int(el[7]), int(el[8]), int(el[9])
if len(el) > 11:
text = el[11]
if text:
# only draw box if text was found
a = cv2.rectangle(img, (x,y), (w + x, h + y), (0, 0, 255), 2)
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text, (x, y), font, 1, (0,0,0), 3)
cv2.putText(img, text, (x, y), font, 1, (255, 255, 255), 2)
cv2.imwrite('boxes.png', img)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gonzalo Odiard |
