| 
									
										
										
										
											2020-11-21 08:10:45 -06: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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __dir__ = os.path.dirname(os.path.abspath(__file__)) | 
					
						
							|  |  |  | sys.path.append(__dir__) | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  | sys.path.insert(0, os.path.abspath(os.path.join(__dir__, ".."))) | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  | os.environ["FLAGS_allocator_strategy"] = "auto_growth" | 
					
						
							| 
									
										
										
										
											2020-12-22 15:57:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | import paddle | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from ppocr.data import create_operators, transform | 
					
						
							|  |  |  | from ppocr.modeling.architectures import build_model | 
					
						
							|  |  |  | from ppocr.postprocess import build_post_process | 
					
						
							| 
									
										
										
										
											2021-11-12 11:06:36 +08:00
										 |  |  | from ppocr.utils.save_load import load_model | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | from ppocr.utils.utility import get_image_file_list | 
					
						
							|  |  |  | import tools.program as program | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     global_config = config["Global"] | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # build post process | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     post_process_class = build_post_process(config["PostProcess"], global_config) | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # build model | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     model = build_model(config["Architecture"]) | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 11:06:36 +08:00
										 |  |  |     load_model(config, model) | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # create data ops | 
					
						
							|  |  |  |     transforms = [] | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     for op in config["Eval"]["dataset"]["transforms"]: | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |         op_name = list(op)[0] | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |         if "Label" in op_name: | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |         elif op_name == "KeepKeys": | 
					
						
							|  |  |  |             op[op_name]["keep_keys"] = ["image"] | 
					
						
							| 
									
										
										
										
											2022-04-27 18:46:48 +08:00
										 |  |  |         elif op_name == "SSLRotateResize": | 
					
						
							|  |  |  |             op[op_name]["mode"] = "test" | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |         transforms.append(op) | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     global_config["infer_mode"] = True | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |     ops = create_operators(transforms, global_config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     model.eval() | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |     for file in get_image_file_list(config["Global"]["infer_img"]): | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |         logger.info("infer_img: {}".format(file)) | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |         with open(file, "rb") as f: | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |             img = f.read() | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |             data = {"image": img} | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |         batch = transform(data, ops) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         images = np.expand_dims(batch[0], axis=0) | 
					
						
							|  |  |  |         images = paddle.to_tensor(images) | 
					
						
							|  |  |  |         preds = model(images) | 
					
						
							|  |  |  |         post_result = post_process_class(preds) | 
					
						
							| 
									
										
										
										
											2022-01-25 14:35:43 +08:00
										 |  |  |         for rec_result in post_result: | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  |             logger.info("\t result: {}".format(rec_result)) | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |     logger.info("success!") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-21 21:46:20 +08:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2020-11-21 08:10:45 -06:00
										 |  |  |     config, device, logger, vdl_writer = program.preprocess() | 
					
						
							|  |  |  |     main() |