use the geometry volume to derive the width and height of
the bounding box
h = xData0[x] + xData2[x]
w = xData1[x] + xData3[x]
compute both the starting and ending (x, y)-coordinates for
the text prediction bounding box
endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x]))
endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x]))
startX = int(endX - w)
startY = int(endY - h)
add the bounding box coordinates and probability score to
our respective lists
rects.append((startX, startY, endX, endY))
confidences.append(scoresData[x])
对于每一行,我们开始遍历列。 我们需要通过忽略概率不够高的区域来过滤掉弱文本检测。
当图像通过网络时,EAST 文本检测器自然会减小体积大小——我们的体积大小实际上比我们的输入图像小 4 倍,因此我们乘以 4 以将坐标带回原始图像。
提取角度数据。 然后我们分别更新我们的矩形和置信度列表。 我们快完成了! 最后一步是对我们的边界框应用非极大值抑制来抑制弱重叠边界框,然后显示结果文本预测:
apply non-maxima suppression to suppress weak, overlapping bounding
boxes
boxes = non_max_suppression(np.array(rects), probs=confidences)
loop over the bounding boxes
for (startX, startY, endX, endY) in boxes:
scale the bounding box coordinates based on the respective
ratios
startX = int(startX * rW)
startY = int(startY * rH)
endX = int(endX * rW)
endY = int(endY * rH)
draw the bounding box on the image
cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2)
show the output image
cv2.imshow(“Text Detection”, orig)
cv2.waitKey(0)
正如我在上一节中提到的,我无法在我的 OpenCV 4 安装 (cv2.dnn.NMSBoxes) 中使用非最大值抑制,因为 Python 绑定没有返回值,最终导致 OpenCV 出错。我无法完全在 OpenCV 3.4.2 中进行测试,因此它可以在 v3.4.2 中运行。
相反,我使用了 imutils 包(第 114 行)中提供的非最大值抑制实现。结果看起来还是不错的;但是,我无法将我的输出与 NMSBoxes 函数进行比较以查看它们是否相同。 循环我们的边界框,将坐标缩放回原始图像尺寸,并将输出绘制到我们的原始图像。原始图像会一直显示,直到按下某个键。
作为最后的实现说明,我想提一下,我们用于循环分数和几何体的两个嵌套 for 循环将是一个很好的例子,说明您可以利用 Cython 显着加速您的管道。我已经使用 OpenCV 和 Python 在快速优化的“for”像素循环中展示了 Cython 的强大功能。
========================================================================
您准备好将文本检测应用于图像了吗?
下载frozen_east_text_detection,地址:
oyyd/frozen_east_text_detection.pb (github.com)
。 从那里,您可以在终端中执行以下命令(注意两个命令行参数):
$ python text_detection.py --image images/lebron_james.jpg \
–east frozen_east_text_detection.pb
您的结果应类似于下图: