基于paddleClas对图像判断文字方向

获得判断后的角度,再对图像进行旋转

import cv2
import paddleclas
import time
import logging
import numpy as np
from ppocr.utils.logging import get_logger

logger = get_logger()

img = './PaddleClas/f6cc8c263ed546c793d310335381e0fc.jpeg'
img = './PaddleClas/d7b28b45f4ce4848b524e78744b88862.jpeg'
img = './PaddleClas/dc475c9b472941f394b2ec9f27a03db7.jpeg'
img = './PaddleClas/f9ec3f3ad9f14152abfa39b61ea50e7a.jpeg'
img = './PaddleClas/58cd2e4230f14777ac457c39740b5f8c.jpeg'
img = './PaddleClas/f9844506a175409a9b06b27a075e665b.jpeg'

image_orientation_predictor = paddleclas.PaddleClas(model_name="text_image_orientation")
cls_result = image_orientation_predictor.predict(input_data=img)
tic = time.time()
cls_res = next(cls_result)
logger.info('text_image_orientation save to {}'.format(cls_res))
# [{'class_ids': [0, 1], 'scores': [0.79156, 0.08451], 'label_names': ['0', '90'], 'filename': 'bda44a8b260d4e0a8ec956756e246a5c.png'}
angle = cls_res[0]['label_names'][0]
cv_rotate_code = {
    '90': cv2.ROTATE_90_COUNTERCLOCKWISE,
    '180': cv2.ROTATE_180,
    '270': cv2.ROTATE_90_CLOCKWISE
}
toc = time.time()
print('image_orientation', toc - tic)

logger.info('angle1 {}'.format(cv_rotate_code))
# angle1 {'90': 2, '180': 1, '270': 0}
if angle in cv_rotate_code:
    img_np = cv2.imread(img)
    logger.info('angle {},{}'.format(angle,cv_rotate_code[angle]))
    rotated_img = cv2.rotate(img_np, cv_rotate_code[angle])
    cv2.imshow('Rotated Image', rotated_img)  
    cv2.waitKey(0)  
    cv2.destroyAllWindows()

你可能感兴趣的:(opencv,人工智能,计算机视觉)