如何在图像的文本区域周围制作边框? (即使文本倾斜!)

时间:2019-02-22 07:19:57

标签: opencv imagemagick bounding-box google-vision python-tesseract

我正在尝试从任何消费品广告中截取的屏幕截图中检测并抓取文字。

我的代码可以一定的精度运行,但是无法在倾斜的文本区域周围创建边框。

最近我尝试了 Google Vision API ,它使几乎所有可能的文本区域都成为边界框,并以很高的精度检测该区域中的文本。我很好奇如何实现相同或相似!

我的测试图像:

enter image description here

边界框后的Google Vision API:

enter image description here

提前谢谢:)

2 个答案:

答案 0 :(得分:2)

与Google的Vision API相当,有一些开源视觉软件包可以检测嘈杂的背景图像中的文本。

您可以使用Zhou等人的称为EAST(高效准确的场景文本检测器)的固定卷积层简单体系结构。 https://arxiv.org/abs/1704.03155v2

使用Python:

从以下位置下载预训练模型: https://www.dropbox.com/s/r2ingd0l3zt8hxs/frozen_east_text_detection.tar.gz?dl=1 。 将模型提取到当前文件夹中。

您将需要OpenCV> = 3.4.2来执行以下命令。

import cv2
import math
net = cv2.dnn.readNet("frozen_east_text_detection.pb")   #This is the model we get after extraction
frame = cv2.imread(<image_filename>)
inpWidth = inpHeight = 320  # A default dimension
# Preparing a blob to pass the image through the neural network
# Subtracting mean values used while training the model.
image_blob = cv2.dnn.blobFromImage(frame, 1.0, (inpWidth, inpHeight), (123.68, 116.78, 103.94), True, False)

现在,我们将必须定义输出层,以通过Sigmoid函数搅出所检测文本的位置值及其置信度得分

output_layer = []
output_layer.append("feature_fusion/Conv_7/Sigmoid")
output_layer.append("feature_fusion/concat_3")

最后,我们将通过网络进行正向传播以获得所需的输出。

net.setInput(image_blob)
output = net.forward(output_layer)
scores = output[0]
geometry = output[1]

在这里,我使用了opencv的github页面https://github.com/opencv/opencv/blob/master/samples/dnn/text_detection.py中定义的解码功能,将位置值转换为框坐标。 (第23至75行)。

对于框检测阈值,我使用的值为0.5,对于非最大抑制,我使用的值为0.3。您可以尝试不同的值以获得更好的边界框。

confThreshold = 0.5
nmsThreshold = 0.3
[boxes, confidences] = decode(scores, geometry, confThreshold)
indices = cv2.dnn.NMSBoxesRotated(boxes, confidences, confThreshold, nmsThreshold)

最后,要将方框覆盖在图像中检测到的文本上:

height_ = frame.shape[0]
width_ = frame.shape[1]
rW = width_ / float(inpWidth)
rH = height_ / float(inpHeight)

for i in indices:
    # get 4 corners of the rotated rect
    vertices = cv2.boxPoints(boxes[i[0]])
    # scale the bounding box coordinates based on the respective ratios
    for j in range(4):
        vertices[j][0] *= rW
        vertices[j][1] *= rH
    for j in range(4):
        p1 = (vertices[j][0], vertices[j][1])
        p2 = (vertices[(j + 1) % 4][0], vertices[(j + 1) % 4][1])
        cv2.line(frame, p1, p2, (0, 255, 0), 3)

# To save the image:
cv2.imwrite("maggi_boxed.jpg", frame)

Maggi's Ad with bounding boxes

我没有尝试使用不同的阈值。更改它们肯定会带来更好的结果,并且还消除了徽标作为文本的错误分类。

注意:该模型在英语语料库上进行了训练,因此不会检测到印地语单词。另外,您还可以阅读概述基准测试数据集的论文。

答案 1 :(得分:0)

您需要检查是否有任何库提供文本坐标,然后可以在文本周围绘制框。 OCR库

1)通过python的pyocr和tesseract ocr

2)使用R语言(从PDF提取文本;执行OCR;全部在R内完成)

3)Java / Pyspark中的Tesseract库

4)Apache Tika

5)Python-OpenCV-使用kNN的手写数据的OCR

6)您可以通过OpenCV和Python进行相同的操作。

免费的OCR软件

Google和HP的Tesseract Google的Keep Microsoft Document Imaging(MODI)(假设我们大多数人将使用Windows OS) 微软一注 Microsoft Oxford Project API(此API在一段时间之前是免费的) FreeOCR(再次基于Tesseract引擎) 还有很多,但是这些是最好的,而在所有这些中,如果您要寻找准确性,Microsoft Document Imaging会做得更好。而且,如果您正在寻找手写文本ocr转换,那么Google的Keep会做得更好。

商业产品

Adob​​e Acrobat Pro(RTF文件格式可为您带来最佳效果) 俘虏 艾比 Informatica(不确定Informatica中的哪个模块) IBM Datacapture(Datacap)(IBM Watson) 如果准确性仅是您的主要限制因素,那么诸如“无与伦比的数据访问权”之类的服务(captricity)就可以达到99%的准确性,因为它们会挤占原始人员,并使他们在不影响安全性的情况下转换手写文本。