From 60ec95469a69c4dc38c576ddc9aba9b7ad656314 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 28 Jul 2020 11:18:48 +0800 Subject: [PATCH 1/4] read gif read func --- ppocr/data/det/db_process.py | 6 ++++-- ppocr/utils/utility.py | 16 +++++++++++++++- tools/infer/predict_det.py | 6 ++++-- tools/infer/predict_rec.py | 6 ++++-- tools/infer/predict_system.py | 33 ++++++++++++++++++++------------- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/ppocr/data/det/db_process.py b/ppocr/data/det/db_process.py index b64b8c8d..9534c59e 100644 --- a/ppocr/data/det/db_process.py +++ b/ppocr/data/det/db_process.py @@ -17,7 +17,7 @@ import cv2 import numpy as np import json import sys -from ppocr.utils.utility import initial_logger +from ppocr.utils.utility import initial_logger, check_and_read_gif logger = initial_logger() from .data_augment import AugmentData @@ -100,7 +100,9 @@ class DBProcessTrain(object): def __call__(self, label_infor): img_path, gt_label = self.convert_label_infor(label_infor) - imgvalue = cv2.imread(img_path) + imgvalue, flag = check_and_read_gif(img_path) + if not flag: + imgvalue = cv2.imread(img_path) if imgvalue is None: logger.info("{} does not exist!".format(img_path)) return None diff --git a/ppocr/utils/utility.py b/ppocr/utils/utility.py index 687e2acb..05b788c4 100755 --- a/ppocr/utils/utility.py +++ b/ppocr/utils/utility.py @@ -15,6 +15,7 @@ import logging import os import imghdr +import cv2 def initial_logger(): @@ -62,7 +63,7 @@ def get_image_file_list(img_file): if img_file is None or not os.path.exists(img_file): raise Exception("not found any img file in {}".format(img_file)) - img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff'} + 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): @@ -75,6 +76,19 @@ def get_image_file_list(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: + logging.info("Cannot read {}. This gif image maybe corrupted.") + 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 + + from paddle import fluid diff --git a/tools/infer/predict_det.py b/tools/infer/predict_det.py index 28515b78..75644aeb 100755 --- a/tools/infer/predict_det.py +++ b/tools/infer/predict_det.py @@ -20,7 +20,7 @@ sys.path.append(os.path.join(__dir__, '../..')) import tools.infer.utility as utility from ppocr.utils.utility import initial_logger logger = initial_logger() -from ppocr.utils.utility import get_image_file_list +from ppocr.utils.utility import get_image_file_list, check_and_read_gif import cv2 from ppocr.data.det.east_process import EASTProcessTest from ppocr.data.det.db_process import DBProcessTest @@ -139,7 +139,9 @@ if __name__ == "__main__": if not os.path.exists(draw_img_save): os.makedirs(draw_img_save) for image_file in image_file_list: - img = cv2.imread(image_file) + img, flag = check_and_read_gif(image_file) + if not flag: + img = cv2.imread(image_file) if img is None: logger.info("error in loading image:{}".format(image_file)) continue diff --git a/tools/infer/predict_rec.py b/tools/infer/predict_rec.py index bd96548d..578401fb 100755 --- a/tools/infer/predict_rec.py +++ b/tools/infer/predict_rec.py @@ -20,7 +20,7 @@ sys.path.append(os.path.abspath(os.path.join(__dir__, '../..'))) import tools.infer.utility as utility from ppocr.utils.utility import initial_logger logger = initial_logger() -from ppocr.utils.utility import get_image_file_list +from ppocr.utils.utility import get_image_file_list, check_and_read_gif import cv2 import copy import numpy as np @@ -153,7 +153,9 @@ def main(args): valid_image_file_list = [] img_list = [] for image_file in image_file_list: - img = cv2.imread(image_file, cv2.IMREAD_COLOR) + img, flag = check_and_read_gif(image_file) + if not flag: + img = cv2.imread(image_file) if img is None: logger.info("error in loading image:{}".format(image_file)) continue diff --git a/tools/infer/predict_system.py b/tools/infer/predict_system.py index 1b7aff1c..5402cf92 100755 --- a/tools/infer/predict_system.py +++ b/tools/infer/predict_system.py @@ -27,7 +27,7 @@ import copy import numpy as np import math import time -from ppocr.utils.utility import get_image_file_list +from ppocr.utils.utility import get_image_file_list, check_and_read_gif from PIL import Image from tools.infer.utility import draw_ocr from tools.infer.utility import draw_ocr_box_txt @@ -49,18 +49,23 @@ class TextSystem(object): points[:, 0] = points[:, 0] - left points[:, 1] = points[:, 1] - top ''' - img_crop_width = int(max(np.linalg.norm(points[0] - points[1]), - np.linalg.norm(points[2] - points[3]))) - img_crop_height = int(max(np.linalg.norm(points[0] - points[3]), - np.linalg.norm(points[1] - points[2]))) - pts_std = np.float32([[0, 0], - [img_crop_width, 0], + img_crop_width = int( + max( + np.linalg.norm(points[0] - points[1]), + np.linalg.norm(points[2] - points[3]))) + img_crop_height = int( + max( + np.linalg.norm(points[0] - points[3]), + np.linalg.norm(points[1] - points[2]))) + pts_std = np.float32([[0, 0], [img_crop_width, 0], [img_crop_width, img_crop_height], [0, img_crop_height]]) M = cv2.getPerspectiveTransform(points, pts_std) - dst_img = cv2.warpPerspective(img, M, (img_crop_width, img_crop_height), - borderMode=cv2.BORDER_REPLICATE, - flags=cv2.INTER_CUBIC) + dst_img = cv2.warpPerspective( + img, + M, (img_crop_width, img_crop_height), + borderMode=cv2.BORDER_REPLICATE, + flags=cv2.INTER_CUBIC) dst_img_height, dst_img_width = dst_img.shape[0:2] if dst_img_height * 1.0 / dst_img_width >= 1.5: dst_img = np.rot90(dst_img) @@ -119,13 +124,15 @@ def main(args): is_visualize = True tackle_img_num = 0 for image_file in image_file_list: - img = cv2.imread(image_file) + img, flag = check_and_read_gif(image_file) + if not flag: + img = cv2.imread(image_file) if img is None: logger.info("error in loading image:{}".format(image_file)) continue starttime = time.time() - tackle_img_num += 1 - if not args.use_gpu and args.enable_mkldnn and tackle_img_num % 30 == 0: + tackle_img_num += 1 + if not args.use_gpu and args.enable_mkldnn and tackle_img_num % 30 == 0: text_sys = TextSystem(args) dt_boxes, rec_res = text_sys(img) elapse = time.time() - starttime From b5a1fe643fb66acc0770b8c74a9166de647d19c0 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 28 Jul 2020 11:29:55 +0800 Subject: [PATCH 2/4] if gif is None, return --- ppocr/utils/utility.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ppocr/utils/utility.py b/ppocr/utils/utility.py index 05b788c4..a196e149 100755 --- a/ppocr/utils/utility.py +++ b/ppocr/utils/utility.py @@ -82,6 +82,7 @@ def check_and_read_gif(img_path): ret, frame = gif.read() if not ret: logging.info("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] From 8b499c3769544337c2939607c945732258c36628 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 28 Jul 2020 13:58:14 +0800 Subject: [PATCH 3/4] move import to the front --- ppocr/utils/utility.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ppocr/utils/utility.py b/ppocr/utils/utility.py index a196e149..e27dd1d8 100755 --- a/ppocr/utils/utility.py +++ b/ppocr/utils/utility.py @@ -16,6 +16,7 @@ import logging import os import imghdr import cv2 +from paddle import fluid def initial_logger(): @@ -90,9 +91,6 @@ def check_and_read_gif(img_path): return None, False -from paddle import fluid - - def create_multi_devices_program(program, loss_var_name): build_strategy = fluid.BuildStrategy() build_strategy.memory_optimize = False From aa6e29cf049c0b6cb00f7a39e7011e174c0f98e7 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 28 Jul 2020 16:42:15 +0800 Subject: [PATCH 4/4] remove unused code --- tools/infer/predict_system.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/infer/predict_system.py b/tools/infer/predict_system.py index 5402cf92..f8a62679 100755 --- a/tools/infer/predict_system.py +++ b/tools/infer/predict_system.py @@ -137,14 +137,14 @@ def main(args): dt_boxes, rec_res = text_sys(img) elapse = time.time() - starttime print("Predict time of %s: %.3fs" % (image_file, elapse)) + + drop_score = 0.5 dt_num = len(dt_boxes) - dt_boxes_final = [] for dno in range(dt_num): text, score = rec_res[dno] - if score >= 0.5: + if score >= drop_score: text_str = "%s, %.3f" % (text, score) print(text_str) - dt_boxes_final.append(dt_boxes[dno]) if is_visualize: image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) @@ -153,7 +153,12 @@ def main(args): scores = [rec_res[i][1] for i in range(len(rec_res))] draw_img = draw_ocr( - image, boxes, txts, scores, draw_txt=True, drop_score=0.5) + image, + boxes, + txts, + scores, + draw_txt=True, + drop_score=drop_score) draw_img_save = "./inference_results/" if not os.path.exists(draw_img_save): os.makedirs(draw_img_save)