module.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 ppstructure.table.predict_table import TableSystem as _TableSystem
  30. from ppstructure.predict_system import save_structure_res
  31. from ppstructure.utility import parse_args
  32. from deploy.hubserving.structure_table.params import read_params
  33. @moduleinfo(
  34. name="structure_table",
  35. version="1.0.0",
  36. summary="PP-Structure table service",
  37. author="paddle-dev",
  38. author_email="paddle-dev@baidu.com",
  39. type="cv/structure_table",
  40. )
  41. class TableSystem(hub.Module):
  42. def _initialize(self, use_gpu=False, enable_mkldnn=False):
  43. """
  44. initialize with the necessary elements
  45. """
  46. cfg = self.merge_configs()
  47. cfg.use_gpu = use_gpu
  48. if use_gpu:
  49. try:
  50. _places = os.environ["CUDA_VISIBLE_DEVICES"]
  51. int(_places[0])
  52. print("use gpu: ", use_gpu)
  53. print("CUDA_VISIBLE_DEVICES: ", _places)
  54. cfg.gpu_mem = 8000
  55. except:
  56. raise RuntimeError(
  57. "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."
  58. )
  59. cfg.ir_optim = True
  60. cfg.enable_mkldnn = enable_mkldnn
  61. self.table_sys = _TableSystem(cfg)
  62. def merge_configs(self):
  63. # default cfg
  64. backup_argv = copy.deepcopy(sys.argv)
  65. sys.argv = sys.argv[:1]
  66. cfg = parse_args()
  67. update_cfg_map = vars(read_params())
  68. for key in update_cfg_map:
  69. cfg.__setattr__(key, update_cfg_map[key])
  70. sys.argv = copy.deepcopy(backup_argv)
  71. return cfg
  72. def read_images(self, paths=[]):
  73. images = []
  74. for img_path in paths:
  75. assert os.path.isfile(img_path), "The {} isn't a valid file.".format(
  76. img_path
  77. )
  78. img = cv2.imread(img_path)
  79. if img is None:
  80. logger.info("error in loading image:{}".format(img_path))
  81. continue
  82. images.append(img)
  83. return images
  84. def predict(self, images=[], paths=[]):
  85. """
  86. Get the chinese texts in the predicted images.
  87. Args:
  88. images (list(numpy.ndarray)): images data, shape of each is [H, W, C]. If images not paths
  89. paths (list[str]): The paths of images. If paths not images
  90. Returns:
  91. res (list): The result of chinese texts and save path of images.
  92. """
  93. if images != [] and isinstance(images, list) and paths == []:
  94. predicted_data = images
  95. elif images == [] and isinstance(paths, list) and paths != []:
  96. predicted_data = self.read_images(paths)
  97. else:
  98. raise TypeError("The input data is inconsistent with expectations.")
  99. assert (
  100. predicted_data != []
  101. ), "There is not any image to be predicted. Please check the input data."
  102. all_results = []
  103. for img in predicted_data:
  104. if img is None:
  105. logger.info("error in loading image")
  106. all_results.append([])
  107. continue
  108. starttime = time.time()
  109. res, _ = self.table_sys(img)
  110. elapse = time.time() - starttime
  111. logger.info("Predict time: {}".format(elapse))
  112. all_results.append({"html": res["html"]})
  113. return all_results
  114. @serving
  115. def serving_method(self, images, **kwargs):
  116. """
  117. Run as a service.
  118. """
  119. images_decode = [base64_to_cv2(image) for image in images]
  120. results = self.predict(images_decode, **kwargs)
  121. return results
  122. if __name__ == "__main__":
  123. table_system = TableSystem()
  124. table_system._initialize()
  125. image_path = ["./ppstructure/docs/table/table.jpg"]
  126. res = table_system.predict(paths=image_path)
  127. print(res)