mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-11 13:30:51 +08:00
65 lines
2.4 KiB
Python
Executable File
65 lines
2.4 KiB
Python
Executable File
# 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 os
|
|
import cv2
|
|
import imghdr
|
|
|
|
|
|
def get_image_file_list(img_file):
|
|
imgs_lists = []
|
|
if img_file is None or not os.path.exists(img_file):
|
|
raise ValueError("not found any img file in {}".format(img_file))
|
|
|
|
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
|
|
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
|
|
imgs_lists.append(img_file)
|
|
elif os.path.isdir(img_file):
|
|
for single_file in os.listdir(img_file):
|
|
file_path = os.path.join(img_file, single_file)
|
|
if os.path.isfile(file_path) and imghdr.what(file_path) in img_end:
|
|
imgs_lists.append(file_path)
|
|
if len(imgs_lists) == 0:
|
|
raise ValueError("not found any img file in {}".format(img_file))
|
|
return imgs_lists
|
|
|
|
|
|
def check_and_read_gif(img_path):
|
|
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
|
|
gif = cv2.VideoCapture(img_path)
|
|
ret, frame = gif.read()
|
|
if not ret:
|
|
print("Cannot read {}. This gif image maybe corrupted.")
|
|
return None, False
|
|
if len(frame.shape) == 2 or frame.shape[-1] == 1:
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
|
|
imgvalue = frame[:, :, ::-1]
|
|
return imgvalue, True
|
|
return None, False
|
|
|
|
|
|
class ClsPostProcess(object):
|
|
""" Convert between text-label and text-index """
|
|
|
|
def __init__(self, label_list, **kwargs):
|
|
super(ClsPostProcess, self).__init__()
|
|
self.label_list = label_list
|
|
|
|
def __call__(self, preds, label=None, *args, **kwargs):
|
|
pred_idxs = preds.argmax(axis=1)
|
|
decode_out = [(self.label_list[idx], preds[i, idx])
|
|
for i, idx in enumerate(pred_idxs)]
|
|
if label is None:
|
|
return decode_out
|
|
label = [(self.label_list[idx], 1.0) for idx in label]
|
|
return decode_out, label |