我想突出显示网站屏幕截图中的特定单词/句子。
截取屏幕截图后,我使用提取文本pytesseract
and cv2
。效果很好,我可以获得有关它的文本和数据。
import pytesseract
import cv2
if __name__ == "__main__":
img = cv2.imread('test.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
result = pytesseract.image_to_data(img, lang='eng', nice=0, output_type=pytesseract.Output.DICT)
print(result)
使用结果对象我可以找到所需的单词和句子。
问题是如何返回图像并突出显示这些单词?
我应该查看其他库还是有办法获取像素值然后突出显示文本?
理想情况下,我想获得每个单词的开始和结束坐标,如何做到这一点?
您可以使用pytesseract.image_to_boxes
方法来获取图像中识别的每个字符的边界框位置。如果需要,您还可以使用该方法在某些特定字符周围绘制边界框。下面的代码在我识别的图像周围绘制矩形。
import cv2
import pytesseract
import matplotlib.pyplot as plt
filename = 'sf.png'
# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image
# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img)use
print(pytesseract.image_to_string(img)) #print identified text
# draw the bounding boxes on the image
for b in boxes.splitlines():
b = b.split()
cv2.rectangle(img, ((int(b[1]), h - int(b[2]))), ((int(b[3]), h - int(b[4]))), (0, 255, 0), 2)
plt.imshow(img)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)