web_service_det.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. from paddle_serving_app.reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes
  24. _LOGGER = logging.getLogger()
  25. class DetOp(Op):
  26. def init_op(self):
  27. self.det_preprocess = Sequential([
  28. DetResizeForTest(), Div(255),
  29. Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), Transpose(
  30. (2, 0, 1))
  31. ])
  32. self.filter_func = FilterBoxes(10, 10)
  33. self.post_func = DBPostProcess({
  34. "thresh": 0.3,
  35. "box_thresh": 0.5,
  36. "max_candidates": 1000,
  37. "unclip_ratio": 1.5,
  38. "min_size": 3
  39. })
  40. def preprocess(self, input_dicts, data_id, log_id):
  41. (_, input_dict), = input_dicts.items()
  42. data = base64.b64decode(input_dict["image"].encode('utf8'))
  43. self.raw_im = data
  44. data = np.fromstring(data, np.uint8)
  45. # Note: class variables(self.var) can only be used in process op mode
  46. im = cv2.imdecode(data, cv2.IMREAD_COLOR)
  47. self.ori_h, self.ori_w, _ = im.shape
  48. det_img = self.det_preprocess(im)
  49. _, self.new_h, self.new_w = det_img.shape
  50. return {"x": det_img[np.newaxis, :].copy()}, False, None, ""
  51. def postprocess(self, input_dicts, fetch_dict, data_id, log_id):
  52. det_out = list(fetch_dict.values())[0]
  53. ratio_list = [
  54. float(self.new_h) / self.ori_h, float(self.new_w) / self.ori_w
  55. ]
  56. dt_boxes_list = self.post_func(det_out, [ratio_list])
  57. dt_boxes = self.filter_func(dt_boxes_list[0], [self.ori_h, self.ori_w])
  58. out_dict = {"dt_boxes": str(dt_boxes)}
  59. return out_dict, None, ""
  60. class OcrService(WebService):
  61. def get_pipeline_response(self, read_op):
  62. det_op = DetOp(name="det", input_ops=[read_op])
  63. return det_op
  64. uci_service = OcrService(name="ocr")
  65. FLAGS = ArgsParser().parse_args()
  66. uci_service.prepare_pipeline_config(yml_dict=FLAGS.conf_dict)
  67. uci_service.run_service()