module.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. sys.path.insert(0, ".")
  20. import copy
  21. import time
  22. import paddlehub
  23. from paddlehub.common.logger import logger
  24. from paddlehub.module.module import moduleinfo, runnable, serving
  25. import cv2
  26. import numpy as np
  27. import paddlehub as hub
  28. from tools.infer.utility import base64_to_cv2
  29. from tools.infer.predict_system import TextSystem
  30. from tools.infer.utility import parse_args
  31. from deploy.hubserving.ocr_system.params import read_params
  32. @moduleinfo(
  33. name="ocr_system",
  34. version="1.0.0",
  35. summary="ocr system service",
  36. author="paddle-dev",
  37. author_email="paddle-dev@baidu.com",
  38. type="cv/PP-OCR_system",
  39. )
  40. class OCRSystem(hub.Module):
  41. def _initialize(self, use_gpu=False, enable_mkldnn=False):
  42. """
  43. initialize with the necessary elements
  44. """
  45. cfg = self.merge_configs()
  46. cfg.use_gpu = use_gpu
  47. if use_gpu:
  48. try:
  49. _places = os.environ["CUDA_VISIBLE_DEVICES"]
  50. int(_places[0])
  51. print("use gpu: ", use_gpu)
  52. print("CUDA_VISIBLE_DEVICES: ", _places)
  53. cfg.gpu_mem = 8000
  54. except:
  55. raise RuntimeError(
  56. "Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
  57. )
  58. cfg.ir_optim = True
  59. cfg.enable_mkldnn = enable_mkldnn
  60. self.text_sys = TextSystem(cfg)
  61. def merge_configs(
  62. self,
  63. ):
  64. # default cfg
  65. backup_argv = copy.deepcopy(sys.argv)
  66. sys.argv = sys.argv[:1]
  67. cfg = parse_args()
  68. update_cfg_map = vars(read_params())
  69. for key in update_cfg_map:
  70. cfg.__setattr__(key, update_cfg_map[key])
  71. sys.argv = copy.deepcopy(backup_argv)
  72. return cfg
  73. def read_images(self, paths=[]):
  74. images = []
  75. for img_path in paths:
  76. assert os.path.isfile(img_path), "The {} isn't a valid file.".format(
  77. img_path
  78. )
  79. img = cv2.imread(img_path)
  80. if img is None:
  81. logger.info("error in loading image:{}".format(img_path))
  82. continue
  83. images.append(img)
  84. return images
  85. def predict(self, images=[], paths=[]):
  86. """
  87. Get the chinese texts in the predicted images.
  88. Args:
  89. images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths
  90. paths (list[str]): The paths of images. If paths not images
  91. Returns:
  92. res (list): The result of chinese texts and save path of images.
  93. """
  94. if images != [] and isinstance(images, list) and paths == []:
  95. predicted_data = images
  96. elif images == [] and isinstance(paths, list) and paths != []:
  97. predicted_data = self.read_images(paths)
  98. else:
  99. raise TypeError("The input data is inconsistent with expectations.")
  100. assert (
  101. predicted_data != []
  102. ), "There is not any image to be predicted. Please check the input data."
  103. all_results = []
  104. for img in predicted_data:
  105. if img is None:
  106. logger.info("error in loading image")
  107. all_results.append([])
  108. continue
  109. starttime = time.time()
  110. dt_boxes, rec_res, _ = self.text_sys(img)
  111. elapse = time.time() - starttime
  112. logger.info("Predict time: {}".format(elapse))
  113. dt_num = len(dt_boxes)
  114. rec_res_final = []
  115. for dno in range(dt_num):
  116. text, score = rec_res[dno]
  117. rec_res_final.append(
  118. {
  119. "text": text,
  120. "confidence": float(score),
  121. "text_region": dt_boxes[dno].astype(np.int32).tolist(),
  122. }
  123. )
  124. all_results.append(rec_res_final)
  125. return all_results
  126. @serving
  127. def serving_method(self, images, **kwargs):
  128. """
  129. Run as a service.
  130. """
  131. images_decode = [base64_to_cv2(image) for image in images]
  132. results = self.predict(images_decode, **kwargs)
  133. return results
  134. if __name__ == "__main__":
  135. ocr = OCRSystem()
  136. ocr._initialize()
  137. image_path = [
  138. "./doc/imgs/11.jpg",
  139. "./doc/imgs/12.jpg",
  140. ]
  141. res = ocr.predict(paths=image_path)
  142. print(res)