client.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import sys
  17. from paddlex_hps_client import triton_request, utils
  18. from tritonclient import grpc as triton_grpc
  19. OCR_IMAGE_PATH = "./ocr.jpg"
  20. LAYOUT_IMAGE_PATH = "./layout.jpg"
  21. def main():
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument("--file", type=str, required=True)
  24. parser.add_argument("--file-type", type=int, choices=[0, 1])
  25. parser.add_argument("--no-visualization", action="store_true")
  26. parser.add_argument("--url", type=str, default="localhost:8001")
  27. args = parser.parse_args()
  28. client = triton_grpc.InferenceServerClient(args.url)
  29. input_ = {"file": utils.prepare_input_file(args.file)}
  30. if args.file_type is not None:
  31. input_["fileType"] = args.file_type
  32. if args.no_visualization:
  33. input_["visualize"] = False
  34. output = triton_request(client, "table-recognition", input_)
  35. if output["errorCode"] != 0:
  36. print(f"Error code: {output['errorCode']}", file=sys.stderr)
  37. print(f"Error message: {output['errorMsg']}", file=sys.stderr)
  38. sys.exit(1)
  39. result = output["result"]
  40. for i, res in enumerate(result["tableRecResults"]):
  41. print(res["prunedResult"])
  42. for img_name, img in res["outputImages"].items():
  43. img_path = f"{img_name}_{i}.jpg"
  44. utils.save_output_file(img, img_path)
  45. print(f"Output image saved at {img_path}")
  46. if __name__ == "__main__":
  47. main()