| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2020-05-12 21:12:52 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2020-07-27 11:37:55 +00:00
										 |  |  | import imghdr | 
					
						
							| 
									
										
										
										
											2020-07-28 11:18:48 +08:00
										 |  |  | import cv2 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 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)) | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_check_global_params(mode): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     check_params = ['use_gpu', 'max_text_length', 'image_shape', \ | 
					
						
							|  |  |  |                     'image_shape', 'character_type', 'loss_type'] | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |     if mode == "train_eval": | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         check_params = check_params + [ \ | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             '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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 21:12:52 +08:00
										 |  |  | 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)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 11:18:48 +08:00
										 |  |  |     img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'} | 
					
						
							| 
									
										
										
										
											2020-07-27 11:37:55 +00:00
										 |  |  |     if os.path.isfile(img_file) and imghdr.what(img_file) in img_end: | 
					
						
							| 
									
										
										
										
											2020-05-12 21:12:52 +08:00
										 |  |  |         imgs_lists.append(img_file) | 
					
						
							|  |  |  |     elif os.path.isdir(img_file): | 
					
						
							|  |  |  |         for single_file in os.listdir(img_file): | 
					
						
							| 
									
										
										
										
											2020-07-27 11:37:55 +00:00
										 |  |  |             file_path = os.path.join(img_file, single_file) | 
					
						
							|  |  |  |             if imghdr.what(file_path) in img_end: | 
					
						
							|  |  |  |                 imgs_lists.append(file_path) | 
					
						
							| 
									
										
										
										
											2020-05-12 21:12:52 +08:00
										 |  |  |     if len(imgs_lists) == 0: | 
					
						
							|  |  |  |         raise Exception("not found any img file in {}".format(img_file)) | 
					
						
							|  |  |  |     return imgs_lists | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 11:18:48 +08:00
										 |  |  | 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: | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             logger = logging.getLogger('ppocr') | 
					
						
							|  |  |  |             logger.info("Cannot read {}. This gif image maybe corrupted.") | 
					
						
							| 
									
										
										
										
											2020-07-28 11:29:55 +08:00
										 |  |  |             return None, False | 
					
						
							| 
									
										
										
										
											2020-07-28 11:18:48 +08:00
										 |  |  |         if len(frame.shape) == 2 or frame.shape[-1] == 1: | 
					
						
							|  |  |  |             frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB) | 
					
						
							|  |  |  |         imgvalue = frame[:, :, ::-1] | 
					
						
							|  |  |  |         return imgvalue, True | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     return None, False |