pipeline_rpc_client.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2020 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. try:
  15. from paddle_serving_server_gpu.pipeline import PipelineClient
  16. except ImportError:
  17. from paddle_serving_server.pipeline import PipelineClient
  18. import numpy as np
  19. import requests
  20. import json
  21. import cv2
  22. import base64
  23. import os
  24. client = PipelineClient()
  25. client.connect(['127.0.0.1:18091'])
  26. def cv2_to_base64(image):
  27. return base64.b64encode(image).decode('utf8')
  28. import argparse
  29. parser = argparse.ArgumentParser(description="args for paddleserving")
  30. parser.add_argument("--image_dir", type=str, default="../../doc/imgs/")
  31. args = parser.parse_args()
  32. test_img_dir = args.image_dir
  33. for img_file in os.listdir(test_img_dir):
  34. with open(os.path.join(test_img_dir, img_file), 'rb') as file:
  35. image_data = file.read()
  36. image = cv2_to_base64(image_data)
  37. for i in range(1):
  38. ret = client.predict(feed_dict={"image": image}, fetch=["res"])
  39. print(ret)