检测后如何从图像中提取文本区域

时间:2019-05-20 11:14:36

标签: python opencv neural-network deep-learning object-detection

我正在尝试使用opencv python从图像中提取所有文本区域。我已经成功检测到文本区域,但是无法提取出来。

我提取了文本区域的较小子矩阵,但无法将它们汇总为一个更大的矩阵,我们将其视为图像中的文本区域。

import numpy as np
import cv2
from imutils.object_detection import non_max_suppression
import matplotlib.pyplot as plt
%matplotlib inline
from PIL import Image
# pip install imutils

image1 = cv2.imread("lebron_james.jpg") 
#image1=cv2.cvtColor(image1,cv2.COLOR_RGB2BGR)
(height1, width1) = image1.shape[:2]
size = 320
(height2, width2) = (size, size)  
image2 = cv2.resize(image1, (width2, height2))  

net = cv2.dnn.readNet("frozen_east_text_detection.pb")
blob = cv2.dnn.blobFromImage(image2, 1.0, (width2, height2), (123.68, 116.78, 103.94), swapRB=True, crop=False)
net.setInput(blob)

(scores, geometry) = net.forward(["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"])
(rows, cols) = scores.shape[2:4]  # grab the rows and columns from score volume
rects = []  # stores the bounding box coordiantes for text regions
confidences = []  # stores the probability associated with each bounding box region in rects

for y in range(rows):
    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]
    angles = geometry[0, 4, y]

    for x in range(cols):

        if scoresdata[x] < 0.5:  # if score is less than min_confidence, ignore
            continue
        # print(scoresdata[x])
        offsetx = x * 4.0
        offsety = y * 4.0
        # EAST detector automatically reduces volume size as it passes through the network
        # extracting the rotation angle for the prediction and computing their sine and cos

        angle = angles[x]
        cos = np.cos(angle)
        sin = np.sin(angle)

        h = xdata0[x] + xdata2[x]
        w = xdata1[x] + xdata3[x]
        #  print(offsetx,offsety,xdata1[x],xdata2[x],cos)
        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)

        # appending the confidence score and probabilities to list
        rects.append((startx, starty, endx, endy))
        confidences.append(scoresdata[x])


# applying non-maxima suppression to supppress weak and overlapping bounding boxes
boxes = non_max_suppression(np.array(rects), probs=confidences)

iti=[]
rW = width1 / float(width2)
rH = height1 / float(height2)
for (startx, starty, endx, endy) in boxes:
    startx = int(startx * rW)
    starty = int(starty * rH)
    endx = int(endx * rW)
    endy = int(endy * rH)

    cv2.rectangle(image1, (startx, starty), (endx, endy), (255, 0,0), 2)

#print(image1)
plt.imshow(image1)
cv2.waitKey(0)

我已经尝试过:

rects.append((startx, starty, endx, endy))
confidences.append(scoresdata[x])
it=image1[np.ix_([startx,endx],[starty,endy])]
pt=Image.fromarray(it)
fig.add_subplot(1, cols, x)
print(it)
plt.imshow(it)

1 个答案:

答案 0 :(得分:0)

您可能在裁剪图像中混合了列/行。您可以尝试以下方法进行裁剪:

it=image1[starty:endy, startx:endx]