如何在文字上画一个矩形?

How to paint a rectangle over words?

我想在原始单词上绘制矩形并将新单词放在它们上面,但是当我这样做时主矩形被绘制为全屏,如何将其从绘制中排除?

https://i.stack.imgur.com/SmUca.jpg - 绘画前

https://i.stack.imgur.com/1KGqF.jpg - 绘画后

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

您可以检查行数据中是否包含文本:


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)