web_service_rec.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (c) 2021 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. from paddle_serving_server.web_service import WebService, Op
  15. import logging
  16. import numpy as np
  17. import cv2
  18. import base64
  19. # from paddle_serving_app.reader import OCRReader
  20. from ocr_reader import OCRReader, DetResizeForTest, ArgsParser
  21. from paddle_serving_app.reader import Sequential, ResizeByFactor
  22. from paddle_serving_app.reader import Div, Normalize, Transpose
  23. _LOGGER = logging.getLogger()
  24. class RecOp(Op):
  25. def init_op(self):
  26. self.ocr_reader = OCRReader(
  27. char_dict_path="../../ppocr/utils/ppocr_keys_v1.txt")
  28. def preprocess(self, input_dicts, data_id, log_id):
  29. (_, input_dict), = input_dicts.items()
  30. raw_im = base64.b64decode(input_dict["image"].encode('utf8'))
  31. data = np.fromstring(raw_im, np.uint8)
  32. im = cv2.imdecode(data, cv2.IMREAD_COLOR)
  33. feed_list = []
  34. max_wh_ratio = 0
  35. ## Many mini-batchs, the type of feed_data is list.
  36. max_batch_size = 6 # len(dt_boxes)
  37. # If max_batch_size is 0, skipping predict stage
  38. if max_batch_size == 0:
  39. return {}, True, None, ""
  40. boxes_size = max_batch_size
  41. rem = boxes_size % max_batch_size
  42. h, w = im.shape[0:2]
  43. wh_ratio = w * 1.0 / h
  44. max_wh_ratio = max(max_wh_ratio, wh_ratio)
  45. _, w, h = self.ocr_reader.resize_norm_img(im, max_wh_ratio).shape
  46. norm_img = self.ocr_reader.resize_norm_img(im, max_batch_size)
  47. norm_img = norm_img[np.newaxis, :]
  48. feed = {"x": norm_img.copy()}
  49. feed_list.append(feed)
  50. return feed_list, False, None, ""
  51. def postprocess(self, input_dicts, fetch_data, data_id, log_id):
  52. res_list = []
  53. if isinstance(fetch_data, dict):
  54. if len(fetch_data) > 0:
  55. rec_batch_res = self.ocr_reader.postprocess(
  56. fetch_data, with_score=True)
  57. for res in rec_batch_res:
  58. res_list.append(res[0])
  59. elif isinstance(fetch_data, list):
  60. for one_batch in fetch_data:
  61. one_batch_res = self.ocr_reader.postprocess(
  62. one_batch, with_score=True)
  63. for res in one_batch_res:
  64. res_list.append(res[0])
  65. res = {"res": str(res_list)}
  66. return res, None, ""
  67. class OcrService(WebService):
  68. def get_pipeline_response(self, read_op):
  69. rec_op = RecOp(name="rec", input_ops=[read_op])
  70. return rec_op
  71. uci_service = OcrService(name="ocr")
  72. FLAGS = ArgsParser().parse_args()
  73. uci_service.prepare_pipeline_config(yml_dict=FLAGS.conf_dict)
  74. uci_service.run_service()