pipeline_http_client.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import numpy as np
  15. import requests
  16. import json
  17. import base64
  18. import os
  19. import argparse
  20. def str2bool(v):
  21. return v.lower() in ("true", "t", "1")
  22. parser = argparse.ArgumentParser(description="args for paddleserving")
  23. parser.add_argument("--image_dir", type=str, default="../../doc/imgs/")
  24. parser.add_argument("--det", type=str2bool, default=True)
  25. parser.add_argument("--rec", type=str2bool, default=True)
  26. args = parser.parse_args()
  27. def cv2_to_base64(image):
  28. return base64.b64encode(image).decode('utf8')
  29. def _check_image_file(path):
  30. img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif'}
  31. return any([path.lower().endswith(e) for e in img_end])
  32. url = "http://127.0.0.1:9998/ocr/prediction"
  33. test_img_dir = args.image_dir
  34. test_img_list = []
  35. if os.path.isfile(test_img_dir) and _check_image_file(test_img_dir):
  36. test_img_list.append(test_img_dir)
  37. elif os.path.isdir(test_img_dir):
  38. for single_file in os.listdir(test_img_dir):
  39. file_path = os.path.join(test_img_dir, single_file)
  40. if os.path.isfile(file_path) and _check_image_file(file_path):
  41. test_img_list.append(file_path)
  42. if len(test_img_list) == 0:
  43. raise Exception("not found any img file in {}".format(test_img_dir))
  44. for idx, img_file in enumerate(test_img_list):
  45. with open(img_file, 'rb') as file:
  46. image_data1 = file.read()
  47. # print file name
  48. print('{}{}{}'.format('*' * 10, img_file, '*' * 10))
  49. image = cv2_to_base64(image_data1)
  50. data = {"key": ["image"], "value": [image]}
  51. r = requests.post(url=url, data=json.dumps(data))
  52. result = r.json()
  53. print("erro_no:{}, err_msg:{}".format(result["err_no"], result["err_msg"]))
  54. # check success
  55. if result["err_no"] == 0:
  56. ocr_result = result["value"][0]
  57. if not args.det:
  58. print(ocr_result)
  59. else:
  60. try:
  61. for item in eval(ocr_result):
  62. # return transcription and points
  63. print("{}, {}".format(item[0], item[1]))
  64. except Exception as e:
  65. print(ocr_result)
  66. print("No results")
  67. continue
  68. else:
  69. print(
  70. "For details about error message, see PipelineServingLogs/pipeline.log"
  71. )
  72. print("==> total number of test imgs: ", len(test_img_list))