ocr_web_client.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. # -*- coding: utf-8 -*-
  15. import requests
  16. import json
  17. import cv2
  18. import base64
  19. import os, sys
  20. import time
  21. def cv2_to_base64(image):
  22. #data = cv2.imencode('.jpg', image)[1]
  23. return base64.b64encode(image).decode(
  24. 'utf8') #data.tostring()).decode('utf8')
  25. headers = {"Content-type": "application/json"}
  26. url = "http://127.0.0.1:9292/ocr/prediction"
  27. test_img_dir = "../../../doc/imgs/"
  28. for idx, img_file in enumerate(os.listdir(test_img_dir)):
  29. with open(os.path.join(test_img_dir, img_file), 'rb') as file:
  30. image_data1 = file.read()
  31. image = cv2_to_base64(image_data1)
  32. for i in range(1):
  33. data = {"feed": [{"image": image}], "fetch": ["save_infer_model/scale_0.tmp_1"]}
  34. r = requests.post(url=url, headers=headers, data=json.dumps(data))
  35. print(r.json())
  36. test_img_dir = "../../../doc/imgs/"
  37. print("==> total number of test imgs: ", len(os.listdir(test_img_dir)))