有一些开源视觉包能够检测嘈杂背景图像中的文本,可与 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)
我没有尝试过不同的阈值。更改它们肯定会带来更好的结果,并且还会消除将徽标误分类为文本的问题。
注意:该模型是在英语语料库上训练的,因此不会检测到印地语单词。您还可以阅读这篇论文,其中概述了它作为基准的测试数据集。