pipeline_http_client.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import glob
  15. import requests
  16. import json
  17. import base64
  18. import os
  19. import argparse
  20. parser = argparse.ArgumentParser(description="args for paddleserving")
  21. parser.add_argument("--image_dir", type=str)
  22. parser.add_argument("--image_file", type=str)
  23. parser.add_argument("--http_port", type=int, default=18093)
  24. parser.add_argument("--service_name", type=str, default="ppdet")
  25. args = parser.parse_args()
  26. def get_test_images(infer_dir, infer_img):
  27. """
  28. Get image path list in TEST mode
  29. """
  30. assert infer_img is not None or infer_dir is not None, \
  31. "--image_file or --image_dir should be set"
  32. assert infer_img is None or os.path.isfile(infer_img), \
  33. "{} is not a file".format(infer_img)
  34. assert infer_dir is None or os.path.isdir(infer_dir), \
  35. "{} is not a directory".format(infer_dir)
  36. # infer_img has a higher priority
  37. if infer_img and os.path.isfile(infer_img):
  38. return [infer_img]
  39. images = set()
  40. infer_dir = os.path.abspath(infer_dir)
  41. assert os.path.isdir(infer_dir), \
  42. "infer_dir {} is not a directory".format(infer_dir)
  43. exts = ['jpg', 'jpeg', 'png', 'bmp']
  44. exts += [ext.upper() for ext in exts]
  45. for ext in exts:
  46. images.update(glob.glob('{}/*.{}'.format(infer_dir, ext)))
  47. images = list(images)
  48. assert len(images) > 0, "no image found in {}".format(infer_dir)
  49. print("Found {} inference images in total.".format(len(images)))
  50. return images
  51. if __name__ == "__main__":
  52. url = f"http://127.0.0.1:{args.http_port}/{args.service_name}/prediction"
  53. logid = 10000
  54. img_list = get_test_images(args.image_dir, args.image_file)
  55. for img_file in img_list:
  56. with open(img_file, 'rb') as file:
  57. image_data = file.read()
  58. # base64 encode
  59. image = base64.b64encode(image_data).decode('utf8')
  60. data = {"key": ["image_0"], "value": [image], "logid": logid}
  61. # send requests
  62. r = requests.post(url=url, data=json.dumps(data))
  63. print(r.json())