utility.py 5.9 KB
Newer Older
L
LDOUBLEV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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 logging
L
LDOUBLEV 已提交
16
import os
littletomatodonkey's avatar
littletomatodonkey 已提交
17
import imghdr
L
LDOUBLEV 已提交
18
import cv2
文幕地方's avatar
文幕地方 已提交
19 20 21
import random
import numpy as np
import paddle
D
Double_V 已提交
22 23 24
import importlib.util
import sys
import subprocess
L
LDOUBLEV 已提交
25 26


W
WenmuZhou 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def print_dict(d, logger, delimiter=0):
    """
    Recursively visualize a dict and
    indenting acrrording by the relationship of keys.
    """
    for k, v in sorted(d.items()):
        if isinstance(v, dict):
            logger.info("{}{} : ".format(delimiter * " ", str(k)))
            print_dict(v, logger, delimiter + 4)
        elif isinstance(v, list) and len(v) >= 1 and isinstance(v[0], dict):
            logger.info("{}{} : ".format(delimiter * " ", str(k)))
            for value in v:
                print_dict(value, logger, delimiter + 4)
        else:
            logger.info("{}{} : {}".format(delimiter * " ", k, v))
L
LDOUBLEV 已提交
42 43 44


def get_check_global_params(mode):
W
WenmuZhou 已提交
45 46
    check_params = ['use_gpu', 'max_text_length', 'image_shape', \
                    'image_shape', 'character_type', 'loss_type']
L
LDOUBLEV 已提交
47
    if mode == "train_eval":
W
WenmuZhou 已提交
48
        check_params = check_params + [ \
L
LDOUBLEV 已提交
49 50 51 52 53 54
            'train_batch_size_per_card', 'test_batch_size_per_card']
    elif mode == "test":
        check_params = check_params + ['test_batch_size_per_card']
    return check_params


littletomatodonkey's avatar
littletomatodonkey 已提交
55
def _check_image_file(path):
U
user1018 已提交
56
    img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'pdf'}
littletomatodonkey's avatar
littletomatodonkey 已提交
57 58 59
    return any([path.lower().endswith(e) for e in img_end])


L
LDOUBLEV 已提交
60 61 62 63 64
def get_image_file_list(img_file):
    imgs_lists = []
    if img_file is None or not os.path.exists(img_file):
        raise Exception("not found any img file in {}".format(img_file))

U
user1018 已提交
65
    img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'pdf'}
littletomatodonkey's avatar
littletomatodonkey 已提交
66
    if os.path.isfile(img_file) and _check_image_file(img_file):
L
LDOUBLEV 已提交
67 68 69
        imgs_lists.append(img_file)
    elif os.path.isdir(img_file):
        for single_file in os.listdir(img_file):
littletomatodonkey's avatar
littletomatodonkey 已提交
70
            file_path = os.path.join(img_file, single_file)
littletomatodonkey's avatar
littletomatodonkey 已提交
71
            if os.path.isfile(file_path) and _check_image_file(file_path):
littletomatodonkey's avatar
littletomatodonkey 已提交
72
                imgs_lists.append(file_path)
L
LDOUBLEV 已提交
73 74
    if len(imgs_lists) == 0:
        raise Exception("not found any img file in {}".format(img_file))
75
    imgs_lists = sorted(imgs_lists)
L
LDOUBLEV 已提交
76 77 78
    return imgs_lists


U
user1018 已提交
79
def check_and_read(img_path):
L
LDOUBLEV 已提交
80 81 82 83
    if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
        gif = cv2.VideoCapture(img_path)
        ret, frame = gif.read()
        if not ret:
W
WenmuZhou 已提交
84 85
            logger = logging.getLogger('ppocr')
            logger.info("Cannot read {}. This gif image maybe corrupted.")
L
LDOUBLEV 已提交
86
            return None, False
L
LDOUBLEV 已提交
87 88 89
        if len(frame.shape) == 2 or frame.shape[-1] == 1:
            frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
        imgvalue = frame[:, :, ::-1]
U
user1018 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        return imgvalue, True, False
    elif os.path.basename(img_path)[-3:] in ['pdf']:
        import fitz
        from PIL import Image
        imgs = []
        with fitz.open(img_path) as pdf:
            for pg in range(0, pdf.pageCount):
                page = pdf[pg]
                mat = fitz.Matrix(2, 2)
                pm = page.getPixmap(matrix=mat, alpha=False)

                # if width or height > 2000 pixels, don't enlarge the image
                if pm.width > 2000 or pm.height > 2000:
                    pm = page.getPixmap(matrix=fitz.Matrix(1, 1), alpha=False)

                img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples)
                img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
                imgs.append(img)
            return imgs, False, True
    return None, False, False
110 111 112 113 114


def load_vqa_bio_label_maps(label_map_path):
    with open(label_map_path, "r", encoding='utf-8') as fin:
        lines = fin.readlines()
115 116 117 118 119 120 121 122 123 124 125 126 127
    old_lines = [line.strip() for line in lines]
    lines = ["O"]
    for line in old_lines:
        # "O" has already been in lines
        if line.upper() in ["OTHER", "OTHERS", "IGNORE"]:
            continue
        lines.append(line)
    labels = ["O"]
    for line in lines[1:]:
        labels.append("B-" + line)
        labels.append("I-" + line)
    label2id_map = {label.upper(): idx for idx, label in enumerate(labels)}
    id2label_map = {idx: label.upper() for idx, label in enumerate(labels)}
128
    return label2id_map, id2label_map
文幕地方's avatar
文幕地方 已提交
129 130 131 132 133 134


def set_seed(seed=1024):
    random.seed(seed)
    np.random.seed(seed)
    paddle.seed(seed)
135 136


D
Double_V 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
def check_install(module_name, install_name):
    spec = importlib.util.find_spec(module_name)
    if spec is None:
        print(f'Warnning! The {module_name} module is NOT installed')
        print(
            f'Try install {module_name} module automatically. You can also try to install manually by pip install {install_name}.'
        )
        python = sys.executable
        try:
            subprocess.check_call(
                [python, '-m', 'pip', 'install', install_name],
                stdout=subprocess.DEVNULL)
            print(f'The {module_name} module is now installed')
        except subprocess.CalledProcessError as exc:
            raise Exception(
                f"Install {module_name} failed, please install manually")
    else:
        print(f"{module_name} has been installed.")


157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
class AverageMeter:
    def __init__(self):
        self.reset()

    def reset(self):
        """reset"""
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        """update"""
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count