end = time.time()
show timing information on text prediction
print(“[INFO] text detection took {:.6f} seconds”.format(end - start))
我们使用 cv2.dnn.readNet 将神经网络加载到内存中,方法是将路径传递给 EAST 检测器。
然后,我们通过将其转换为 blob 来准备我们的图像。要阅读有关此步骤的更多信息,请参阅深度学习:OpenCV 的 blobFromImage 工作原理。 为了预测文本,我们可以简单地将 blob 设置为输入并调用 net.forward。 这些行被抓取时间戳包围,以便我们可以打印经过的时间。 通过将 layerNames 作为参数提供给 net.forward,我们指示 OpenCV 返回我们感兴趣的两个特征图:
-
用于导出输入图像中文本的边界框坐标的输出几何图
-
同样,分数图,包含给定区域包含文本的概率
我们需要一个一个地循环这些值中的每一个:
grab the number of rows and columns from the scores volume, then
initialize our set of bounding box rectangles and corresponding
confidence scores
(numRows, numCols) = scores.shape[2:4]
rects = []
confidences = []
loop over the number of rows
for y in range(0, numRows):
extract the scores (probabilities), followed by the geometrical
data used to derive potential bounding box coordinates that
surround text
scoresData = scores[0, 0, y]
xData0 = geometry[0, 0, y]
xData1 = geometry[0, 1, y]
xData2 = geometry[0, 2, y]
xData3 = geometry[0, 3, y]
anglesData = geometry[0, 4, y]
我们首先获取分数卷的维度(,然后初始化两个列表:
-
rects :存储文本区域的边界框 (x, y) 坐标
-
置信度:将与每个边界框关联的概率存储在 rects 中
我们稍后将对这些区域应用非极大值抑制。 循环遍历行。提取当前行 y 的分数和几何数据。 接下来,我们遍历当前选定行的每个列索引:
loop over the number of columns
for x in range(0, numCols):
if our score does not have sufficient probability, ignore it
if scoresData[x] < args[“min_confidence”]:
continue
compute the offset factor as our resulting feature maps will
be 4x smaller than the input image
(offsetX, offsetY) = (x * 4.0, y * 4.0)
extract the rotation angle for the prediction and then
compute the sin and cosine
angle = anglesData[x]
cos = np.cos(angle)
sin = np.sin(angle)
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 以将坐标带回原始图像。
提取角度数据。 然后我们分别更新我们的矩形和置信度列表。 我们快完成了! 最后一步是对我们的边界框应用非极大值抑制来抑制弱重叠边界框,然后显示结果文本预测: