| 
									
										
										
										
											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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from __future__ import absolute_import | 
					
						
							|  |  |  | from __future__ import division | 
					
						
							|  |  |  | from __future__ import print_function | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import numpy as np | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 13:49:24 +08:00
										 |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2021-06-05 03:58:17 +00:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-12 12:56:44 +08:00
										 |  |  | __dir__ = os.path.dirname(os.path.abspath(__file__)) | 
					
						
							| 
									
										
										
										
											2020-06-12 13:49:24 +08:00
										 |  |  | sys.path.append(__dir__) | 
					
						
							| 
									
										
										
										
											2020-08-12 12:56:44 +08:00
										 |  |  | sys.path.append(os.path.abspath(os.path.join(__dir__, '..'))) | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 15:57:21 +08:00
										 |  |  | os.environ["FLAGS_allocator_strategy"] = 'auto_growth' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | import paddle | 
					
						
							| 
									
										
										
										
											2020-06-02 19:03:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | from ppocr.data import create_operators, transform | 
					
						
							| 
									
										
										
										
											2020-11-09 16:40:24 +08:00
										 |  |  | from ppocr.modeling.architectures import build_model | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | from ppocr.postprocess import build_post_process | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | from ppocr.utils.save_load import init_model | 
					
						
							| 
									
										
										
										
											2020-11-09 16:40:24 +08:00
										 |  |  | from ppocr.utils.utility import get_image_file_list | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | import tools.program as program | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     global_config = config['Global'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # build post process | 
					
						
							|  |  |  |     post_process_class = build_post_process(config['PostProcess'], | 
					
						
							|  |  |  |                                             global_config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # build model | 
					
						
							|  |  |  |     if hasattr(post_process_class, 'character'): | 
					
						
							| 
									
										
										
										
											2021-06-04 02:46:45 +00:00
										 |  |  |         char_num = len(getattr(post_process_class, 'character')) | 
					
						
							|  |  |  |         if config['Architecture']["algorithm"] in ["Distillation", | 
					
						
							|  |  |  |                                                    ]:  # distillation model | 
					
						
							|  |  |  |             for key in config['Architecture']["Models"]: | 
					
						
							|  |  |  |                 config['Architecture']["Models"][key]["Head"][ | 
					
						
							|  |  |  |                     'out_channels'] = char_num | 
					
						
							|  |  |  |         else:  # base rec model | 
					
						
							|  |  |  |             config['Architecture']["Head"]['out_channels'] = char_num | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     model = build_model(config['Architecture']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-05 06:52:45 +00:00
										 |  |  |     init_model(config, model) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # create data ops | 
					
						
							|  |  |  |     transforms = [] | 
					
						
							| 
									
										
										
										
											2020-11-09 16:40:24 +08:00
										 |  |  |     for op in config['Eval']['dataset']['transforms']: | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         op_name = list(op)[0] | 
					
						
							|  |  |  |         if 'Label' in op_name: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         elif op_name in ['RecResizeImg']: | 
					
						
							|  |  |  |             op[op_name]['infer_mode'] = True | 
					
						
							| 
									
										
										
										
											2020-11-09 16:40:24 +08:00
										 |  |  |         elif op_name == 'KeepKeys': | 
					
						
							| 
									
										
										
										
											2020-12-30 16:15:49 +08:00
										 |  |  |             if config['Architecture']['algorithm'] == "SRN": | 
					
						
							|  |  |  |                 op[op_name]['keep_keys'] = [ | 
					
						
							|  |  |  |                     'image', 'encoder_word_pos', 'gsrm_word_pos', | 
					
						
							|  |  |  |                     'gsrm_slf_attn_bias1', 'gsrm_slf_attn_bias2' | 
					
						
							|  |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2021-08-24 03:49:26 +00:00
										 |  |  |             elif config['Architecture']['algorithm'] == "SAR": | 
					
						
							|  |  |  |                 op[op_name]['keep_keys'] = [ | 
					
						
							|  |  |  |                     'image', 'valid_ratio' | 
					
						
							|  |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2020-12-30 16:15:49 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 op[op_name]['keep_keys'] = ['image'] | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         transforms.append(op) | 
					
						
							|  |  |  |     global_config['infer_mode'] = True | 
					
						
							|  |  |  |     ops = create_operators(transforms, global_config) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-25 12:49:45 +00:00
										 |  |  |     save_res_path = config['Global'].get('save_res_path', | 
					
						
							|  |  |  |                                          "./output/rec/predicts_rec.txt") | 
					
						
							|  |  |  |     if not os.path.exists(os.path.dirname(save_res_path)): | 
					
						
							|  |  |  |         os.makedirs(os.path.dirname(save_res_path)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     model.eval() | 
					
						
							| 
									
										
										
										
											2021-04-25 12:49:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with open(save_res_path, "w") as fout: | 
					
						
							|  |  |  |         for file in get_image_file_list(config['Global']['infer_img']): | 
					
						
							|  |  |  |             logger.info("infer_img: {}".format(file)) | 
					
						
							|  |  |  |             with open(file, 'rb') as f: | 
					
						
							|  |  |  |                 img = f.read() | 
					
						
							|  |  |  |                 data = {'image': img} | 
					
						
							|  |  |  |             batch = transform(data, ops) | 
					
						
							|  |  |  |             if config['Architecture']['algorithm'] == "SRN": | 
					
						
							|  |  |  |                 encoder_word_pos_list = np.expand_dims(batch[1], axis=0) | 
					
						
							|  |  |  |                 gsrm_word_pos_list = np.expand_dims(batch[2], axis=0) | 
					
						
							|  |  |  |                 gsrm_slf_attn_bias1_list = np.expand_dims(batch[3], axis=0) | 
					
						
							|  |  |  |                 gsrm_slf_attn_bias2_list = np.expand_dims(batch[4], axis=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 others = [ | 
					
						
							|  |  |  |                     paddle.to_tensor(encoder_word_pos_list), | 
					
						
							|  |  |  |                     paddle.to_tensor(gsrm_word_pos_list), | 
					
						
							|  |  |  |                     paddle.to_tensor(gsrm_slf_attn_bias1_list), | 
					
						
							|  |  |  |                     paddle.to_tensor(gsrm_slf_attn_bias2_list) | 
					
						
							|  |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2021-08-24 03:49:26 +00:00
										 |  |  |             if config['Architecture']['algorithm'] == "SAR": | 
					
						
							|  |  |  |                 valid_ratio = np.expand_dims(batch[-1], axis=0) | 
					
						
							|  |  |  |                 img_metas = [paddle.to_tensor(valid_ratio)] | 
					
						
							| 
									
										
										
										
											2021-04-25 12:49:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             images = np.expand_dims(batch[0], axis=0) | 
					
						
							|  |  |  |             images = paddle.to_tensor(images) | 
					
						
							|  |  |  |             if config['Architecture']['algorithm'] == "SRN": | 
					
						
							|  |  |  |                 preds = model(images, others) | 
					
						
							| 
									
										
										
										
											2021-08-24 03:49:26 +00:00
										 |  |  |             elif config['Architecture']['algorithm'] == "SAR": | 
					
						
							|  |  |  |                 preds = model(images, img_metas) | 
					
						
							| 
									
										
										
										
											2021-04-25 12:49:45 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 preds = model(images) | 
					
						
							|  |  |  |             post_result = post_process_class(preds) | 
					
						
							| 
									
										
										
										
											2021-06-05 03:58:17 +00:00
										 |  |  |             info = None | 
					
						
							|  |  |  |             if isinstance(post_result, dict): | 
					
						
							|  |  |  |                 rec_info = dict() | 
					
						
							|  |  |  |                 for key in post_result: | 
					
						
							|  |  |  |                     if len(post_result[key][0]) >= 2: | 
					
						
							|  |  |  |                         rec_info[key] = { | 
					
						
							|  |  |  |                             "label": post_result[key][0][0], | 
					
						
							| 
									
										
										
										
											2021-09-01 19:36:16 +08:00
										 |  |  |                             "score": float(post_result[key][0][1]), | 
					
						
							| 
									
										
										
										
											2021-06-05 03:58:17 +00:00
										 |  |  |                         } | 
					
						
							|  |  |  |                 info = json.dumps(rec_info) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 if len(post_result[0]) >= 2: | 
					
						
							|  |  |  |                     info = post_result[0][0] + "\t" + str(post_result[0][1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if info is not None: | 
					
						
							|  |  |  |                 logger.info("\t result: {}".format(info)) | 
					
						
							|  |  |  |                 fout.write(file + "\t" + info) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     logger.info("success!") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2020-11-09 16:40:24 +08:00
										 |  |  |     config, device, logger, vdl_writer = program.preprocess() | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |     main() |