gpt4 book ai didi

python - 通过边界框从图像中提取选定的文本

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:25 25 4
gpt4 key购买 nike

image

我正在尝试通过图像上的边界框来获取选定的文本。就像边界框只选择了单词一样,我想获取该文本并将其转换为文本文件。请查看我的代码并提供一些评论,以便我可以实现该功能。

到目前为止,我已经将 PDF 文件转换为在文本上带有边框的图像。

import numpy as np
import csv
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
from pytesseract import Output
import cv2

pdf = wi(filename="samplecompany.pdf", resolution=100)
pdfImg = pdf.convert('jpg')
j = 1
for img in pdfImg.sequence:
page = wi(image=img)
page.save(filename=str(j)+".jpg")
img1 = cv2.imread(str(j)+".jpg")

d = pytesseract.image_to_data(img1, output_type=Output.DICT)
n_boxes = len(d['level'])
print(n_boxes)
for i in range(n_boxes):
(x, y, w, h) = (d['left'][i], d['top']
[i], d['width'][i], d['height'][i])
print((x, y, w, h))
cv2.rectangle(img1, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.imwrite(str(j)+".jpg", img1)

cv2.waitKey(0)
j += 1

这段代码工作正常我需要从我创建的图像中获取所需的文本。使用边界框位置

最佳答案

You can use this code to get custom text from a an image and change and modify accordingly and this is also save your text to an text file

import io
import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import Output
from wand.image import Image as wi
import sys


pdf = wi(filename="Resume.pdf", resolution=100)
pdfImg = pdf.convert('jpg')
j = 1
imgBlobs = []
img1= []
for img in pdfImg.sequence:
page = wi(image=img)
page.save(filename=str(j)+".jpg")
img1.append(cv2.imread(str(j)+".jpg"))
j += 1

extracted_text = []

for img2 in img1:
d = pytesseract.image_to_data(img2, output_type=Output.DICT)
n_boxes = len(d['level'])
print(n_boxes)
extracted_text.append(d['text'][9])
(x, y, w, h) = (d['left'][9], d['top'][9], d['width'][9], d['height'][9])
cv2.rectangle(img2, (x, y), (x + w, y + h), (0, 255, 0), 2)


cv2.imshow('img', img2)

print(d)


with open('Prototype.txt', 'w') as filehandle:
for listitem in extracted_text:
filehandle.write('%s\n' % listitem)

关于python - 通过边界框从图像中提取选定的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56442156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com