文本检测时对图片进行方向矫正

        在使用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:如果想要模型的话可以去官网找,或者留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值