我希望在原始單詞上繪制矩形并將新單詞放在它們之上,但是當我這樣做時,主矩形被全屏繪制,如何將其排除在繪畫之外?
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
uj5u.com熱心網友回復:
您可以檢查行資料中是否包含文本:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463209.html
上一篇:為什么DelphiTOpenDialog無法在初始目錄中打開?
下一篇:RGB到V(來自HSV)
