我尝试从图像中检测文本,在图像中我在选择的字符周围绘制边框并将它们缝合在一起以形成另一幅图像,如下所示:
我使用cv2使用以下代码在字符周围绘制边框:
cnts = cv2.findContours(inverted, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, bounding_boxes) = sort_contours(cnts)
ROI_number = 0
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
ROI = inverted[y:y + h, x:x + w]
# ROI = cv2.erode(ROI, kernel, iterations=1)
ROI = cv2.filter2D(ROI, -1, sharpen_kernel)
# ROI = cv2.GaussianBlur(ROI, (5, 5), 0)
# ROI = cv2.filter2D(ROI, -100, sharpen_kernel)
ROI = cv2.bitwise_not(ROI)
ht, wd = ROI.shape
ww = 26
hh = 30
result = np.full((hh, ww), 255, dtype=np.uint8)
xx = (ww - wd) // 2
yy = (hh - ht) // 2
result[yy:yy + ht, xx:xx + wd] = ROI
cv2.imwrite('ROI_{}.jpeg'.format(ROI_number), result)
cv2.rectangle(inverted, (x, y), (x + w, y + h), (36, 255, 12), 0)
ROI_number += 1
我使用numpy的hstack使用以下代码将图像拼接在一起:
def stitch_images(input_path):
imagePaths = []
for image_path in glob.glob(os.path.join(input_path, '*.jpeg')):
imagePaths.append(image_path)
sorted_paths = sorted(imagePaths)
list_im = [sorted_paths[0], sorted_paths[1], sorted_paths[2], sorted_paths[3], sorted_paths[4], sorted_paths[5]]
imgs = [Image.open(i) for i in list_im]
min_shape = sorted([(np.sum(i.size), i.size) for i in imgs])[0][1]
imgs_comb = np.hstack((np.asarray(i.resize(min_shape)) for i in imgs))
imgs_comb = Image.fromarray(imgs_comb)
imgs_comb.save('stitched.jpeg')
但是,无法使用tesseract读取缝合的图像,并出现以下错误:
Tesseract Open Source OCR Engine v4.1.1-rc2-21-gf4ef with Leptonica
Warning: Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 334
Empty page!!
Estimating resolution as 334
Empty page!
参考方案
加载图像,转换为灰度并使用image_to_string
对我来说是可行的。 pytesseract的结果:
418081
码
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perfrom OCR with Pytesseract
data = pytesseract.image_to_string(gray, lang='eng', config='--psm 6')
print(data)
------------------
System information
------------------
Python: 3.7.4
NumPy: 1.14.5
OpenCV: 4.1.0
------------------
Python GPU资源利用 - python我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…
Python sqlite3数据库已锁定 - python我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…
python:ConfigParser对象,然后再阅读一次 - python场景:我有一个配置文件,其中包含要执行的自动化测试的列表。这些测试是长期循环执行的。 配置文件的设计方式使ConfigParser可以读取它。由于有两个三个参数,因此我需要通过每个测试。现在,此配置文件由script(s1)调用,并且按照配置文件中的列表执行测试。Script(s1)第一次读取配置,并且在每次测试完成后都会执行。阅读两次的要求:由于可能会…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
python-docx应该在空单元格已满时返回空单元格 - python我试图遍历文档中的所有表并从中提取文本。作为中间步骤,我只是尝试将文本打印到控制台。我在类似的帖子中已经看过scanny提供的其他代码,但是由于某种原因,它并没有提供我正在解析的文档的预期输出可以在https://www.ontario.ca/laws/regulation/140300中找到该文档from docx import Document from…