在使用ctpn或者pse等文本检测算法时,首先要对文本图片进行矫正,本篇博文使用的是opencv2调用Tensorflow矫正模型达到矫正文本图片的目的,逻辑是使用矫正模型检测是图片的翻转角度,根据矫正模型输出的度数来利用opencv做flip操作。
angleNet = cv2.dnn.readNetFromTensorflow(config['pse']['AngleModelPb'], config['pse']['AngleModelPbtxt'])
def angle_detect(self, img, adjust=False):
h, w = img.shape[:2]
ROTATE = [0, 90, 180, 270]
if adjust:
thesh = 0.05
xMin, yMin, xMax, yMax = int(thesh * w), int(thesh * h), w - int(thesh * w), h - int(thesh * h)
img = img[yMin: yMax, xMin: xMax] # cut the edge of image
inputBlob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(224, 224), swapRB=True,
mean=[103.939, 116.779, 123.68], crop=False)
angleNet.setInput(inputBlob)
pred = self.angleNet.forward()
index = np.argmax(pred, axis=1)[0]
return ROTATE[index]
def rotate(self, img):
# rotate the image and return the angle
angle = 0
img = np.array(img)
(h, w) = img.shape[:2]
angle = angle_detect(img=np.copy(img)) # angle detection
if angle == 90:
img = cv2.transpose(img)
img = cv2.flip(img, flipCode=0) # counter clock wise
elif angle == 180:
img = cv2.flip(img, flipCode=-1) # flip the image both horizontally and vertically
elif angle == 270:
img = cv2.transpose(img)
img = cv2.flip(img, flipCode=1) # clock wise rotation
return angle, img
if __name__ == '__main__':
rotate(img)
PS:如果想要模型的话可以去官网找,或者留言。