test_server.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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 requests
  15. import json
  16. import cv2
  17. import base64
  18. import time
  19. import numpy as np
  20. def cv2_to_base64(image):
  21. data = cv2.imencode('.jpg', image)[1]
  22. return base64.b64encode(data.tostring()).decode('utf8')
  23. def base64_to_cv2(b64str):
  24. data = base64.b64decode(b64str.encode('utf8'))
  25. data = np.fromstring(data, np.uint8)
  26. data = cv2.imdecode(data, cv2.IMREAD_COLOR)
  27. return data
  28. # Send HTTP request
  29. org_im = cv2.cvtColor(cv2.imread('demo_images/test.jpg'), cv2.COLOR_BGR2RGB)
  30. h, w, c = org_im.shape
  31. hat_ids = 1
  32. data = {
  33. 'images': [cv2_to_base64(org_im)],
  34. 'background': 3,
  35. "beard": 2,
  36. "glasses": 3,
  37. "hat": 3
  38. }
  39. headers = {"Content-type": "application/json"}
  40. url = "http://127.0.0.1:8880/predict/solov2_blazeface"
  41. start = time.time()
  42. r = requests.post(url=url, headers=headers, data=json.dumps(data))
  43. end = time.time()
  44. print('cost:', end - start)
  45. result = base64_to_cv2(r.json()["results"]['image'])
  46. cv2.imwrite("chrismas_final.png", result)