module.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 paddlehub
  22. from paddlehub.common.logger import logger
  23. from paddlehub.module.module import moduleinfo, runnable, serving
  24. import cv2
  25. import paddlehub as hub
  26. from tools.infer.utility import base64_to_cv2
  27. from tools.infer.predict_rec import TextRecognizer
  28. from tools.infer.utility import parse_args
  29. from deploy.hubserving.ocr_rec.params import read_params
  30. @moduleinfo(
  31. name="ocr_rec",
  32. version="1.0.0",
  33. summary="ocr recognition service",
  34. author="paddle-dev",
  35. author_email="paddle-dev@baidu.com",
  36. type="cv/text_recognition",
  37. )
  38. class OCRRec(hub.Module):
  39. def _initialize(self, use_gpu=False, enable_mkldnn=False):
  40. """
  41. initialize with the necessary elements
  42. """
  43. cfg = self.merge_configs()
  44. cfg.use_gpu = use_gpu
  45. if use_gpu:
  46. try:
  47. _places = os.environ["CUDA_VISIBLE_DEVICES"]
  48. int(_places[0])
  49. print("use gpu: ", use_gpu)
  50. print("CUDA_VISIBLE_DEVICES: ", _places)
  51. cfg.gpu_mem = 8000
  52. except:
  53. raise RuntimeError(
  54. "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."
  55. )
  56. cfg.ir_optim = True
  57. cfg.enable_mkldnn = enable_mkldnn
  58. self.text_recognizer = TextRecognizer(cfg)
  59. def merge_configs(
  60. self,
  61. ):
  62. # default cfg
  63. backup_argv = copy.deepcopy(sys.argv)
  64. sys.argv = sys.argv[:1]
  65. cfg = parse_args()
  66. update_cfg_map = vars(read_params())
  67. for key in update_cfg_map:
  68. cfg.__setattr__(key, update_cfg_map[key])
  69. sys.argv = copy.deepcopy(backup_argv)
  70. return cfg
  71. def read_images(self, paths=[]):
  72. images = []
  73. for img_path in paths:
  74. assert os.path.isfile(img_path), "The {} isn't a valid file.".format(
  75. img_path
  76. )
  77. img = cv2.imread(img_path)
  78. if img is None:
  79. logger.info("error in loading image:{}".format(img_path))
  80. continue
  81. images.append(img)
  82. return images
  83. def predict(self, images=[], paths=[]):
  84. """
  85. Get the text box in the predicted images.
  86. Args:
  87. images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths
  88. paths (list[str]): The paths of images. If paths not images
  89. Returns:
  90. res (list): The result of text detection box and save path of images.
  91. """
  92. if images != [] and isinstance(images, list) and paths == []:
  93. predicted_data = images
  94. elif images == [] and isinstance(paths, list) and paths != []:
  95. predicted_data = self.read_images(paths)
  96. else:
  97. raise TypeError("The input data is inconsistent with expectations.")
  98. assert (
  99. predicted_data != []
  100. ), "There is not any image to be predicted. Please check the input data."
  101. img_list = []
  102. for img in predicted_data:
  103. if img is None:
  104. continue
  105. img_list.append(img)
  106. rec_res_final = []
  107. try:
  108. rec_res, predict_time = self.text_recognizer(img_list)
  109. for dno in range(len(rec_res)):
  110. text, score = rec_res[dno]
  111. rec_res_final.append(
  112. {
  113. "text": text,
  114. "confidence": float(score),
  115. }
  116. )
  117. except Exception as e:
  118. print(e)
  119. return [[]]
  120. return [rec_res_final]
  121. @serving
  122. def serving_method(self, images, **kwargs):
  123. """
  124. Run as a service.
  125. """
  126. images_decode = [base64_to_cv2(image) for image in images]
  127. results = self.predict(images_decode, **kwargs)
  128. return results
  129. if __name__ == "__main__":
  130. ocr = OCRRec()
  131. ocr._initialize()
  132. image_path = [
  133. "./doc/imgs_words/ch/word_1.jpg",
  134. "./doc/imgs_words/ch/word_2.jpg",
  135. "./doc/imgs_words/ch/word_3.jpg",
  136. ]
  137. res = ocr.predict(paths=image_path)
  138. print(res)