img_tools.py 4.4 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

import math
import cv2
import numpy as np
LDOUBLEV's avatar
LDOUBLEV committed
18
19
from ppocr.utils.utility import initial_logger
logger = initial_logger()
LDOUBLEV's avatar
LDOUBLEV committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


def get_bounding_box_rect(pos):
    left = min(pos[0])
    right = max(pos[0])
    top = min(pos[1])
    bottom = max(pos[1])
    return [left, top, right, bottom]


def resize_norm_img(img, image_shape):
    imgC, imgH, imgW = image_shape
    h = img.shape[0]
    w = img.shape[1]
    ratio = w / float(h)
    if math.ceil(imgH * ratio) > imgW:
        resized_w = imgW
    else:
        resized_w = int(math.ceil(imgH * ratio))
    resized_image = cv2.resize(img, (resized_w, imgH))
    resized_image = resized_image.astype('float32')
    if image_shape[0] == 1:
        resized_image = resized_image / 255
        resized_image = resized_image[np.newaxis, :]
    else:
        resized_image = resized_image.transpose((2, 0, 1)) / 255
    resized_image -= 0.5
    resized_image /= 0.5
    padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
    padding_im[:, :, 0:resized_w] = resized_image
    return padding_im


tink2123's avatar
tink2123 committed
53
54
55
def resize_norm_img_chinese(img, image_shape):
    imgC, imgH, imgW = image_shape
    # todo: change to 0 and modified image shape
tink2123's avatar
tink2123 committed
56
    max_wh_ratio = 0
tink2123's avatar
tink2123 committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    h, w = img.shape[0], img.shape[1]
    ratio = w * 1.0 / h
    max_wh_ratio = max(max_wh_ratio, ratio)
    imgW = int(32 * max_wh_ratio)
    if math.ceil(imgH * ratio) > imgW:
        resized_w = imgW
    else:
        resized_w = int(math.ceil(imgH * ratio))
    resized_image = cv2.resize(img, (resized_w, imgH))
    resized_image = resized_image.astype('float32')
    if image_shape[0] == 1:
        resized_image = resized_image / 255
        resized_image = resized_image[np.newaxis, :]
    else:
        resized_image = resized_image.transpose((2, 0, 1)) / 255
    resized_image -= 0.5
    resized_image /= 0.5
    padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
    padding_im[:, :, 0:resized_w] = resized_image
    return padding_im


LDOUBLEV's avatar
LDOUBLEV committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def get_img_data(value):
    """get_img_data"""
    if not value:
        return None
    imgdata = np.frombuffer(value, dtype='uint8')
    if imgdata is None:
        return None
    imgori = cv2.imdecode(imgdata, 1)
    if imgori is None:
        return None
    return imgori


def process_image(img,
                  image_shape,
                  label=None,
                  char_ops=None,
                  loss_type=None,
tink2123's avatar
tink2123 committed
97
                  max_text_length=None,
tink2123's avatar
tink2123 committed
98
99
                  tps=None,
                  infer_mode=False):
tink2123's avatar
tink2123 committed
100
    if not infer_mode or char_ops.character_type == "en" or tps != None:
tink2123's avatar
tink2123 committed
101
102
        norm_img = resize_norm_img(img, image_shape)
    else:
tink2123's avatar
tink2123 committed
103
        norm_img = resize_norm_img_chinese(img, image_shape)
LDOUBLEV's avatar
LDOUBLEV committed
104
105
    norm_img = norm_img[np.newaxis, :]
    if label is not None:
LDOUBLEV's avatar
LDOUBLEV committed
106
        # char_num = char_ops.get_char_num()
LDOUBLEV's avatar
LDOUBLEV committed
107
108
        text = char_ops.encode(label)
        if len(text) == 0 or len(text) > max_text_length:
LDOUBLEV's avatar
LDOUBLEV committed
109
110
            logger.info(
                "Warning in ppocr/data/rec/img_tools.py:line106: Wrong data type."
tink2123's avatar
tink2123 committed
111
112
                "Excepted string with length between 1 and {}, but "
                "got '{}'. Label is '{}'".format(max_text_length, len(text),label))
LDOUBLEV's avatar
LDOUBLEV committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
            return None
        else:
            if loss_type == "ctc":
                text = text.reshape(-1, 1)
                return (norm_img, text)
            elif loss_type == "attention":
                beg_flag_idx = char_ops.get_beg_end_flag_idx("beg")
                end_flag_idx = char_ops.get_beg_end_flag_idx("end")
                beg_text = np.append(beg_flag_idx, text)
                end_text = np.append(text, end_flag_idx)
                beg_text = beg_text.reshape(-1, 1)
                end_text = end_text.reshape(-1, 1)
                return (norm_img, beg_text, end_text)
            else:
                assert False, "Unsupport loss_type %s in process_image"\
                    % loss_type
    return (norm_img)