rec_web_server.py 3.0 KB
Newer Older
D
dyning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle_serving_client import Client
from paddle_serving_app.reader import OCRReader
import cv2
import sys
import numpy as np
import os
from paddle_serving_client import Client
from paddle_serving_app.reader import Sequential, URL2Image, ResizeByFactor
from paddle_serving_app.reader import Div, Normalize, Transpose
from paddle_serving_app.reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes
25
if sys.argv[1] == 'gpu':
26
    from paddle_serving_server.web_service import WebService
27 28
elif sys.argv[1] == 'cpu':
    from paddle_serving_server.web_service import WebService
D
dyning 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import time
import re
import base64


class OCRService(WebService):
    def init_rec(self):
        self.ocr_reader = OCRReader()

    def preprocess(self, feed=[], fetch=[]):
        # TODO: to handle batch rec images
        img_list = []
        for feed_data in feed:
            data = base64.b64decode(feed_data["image"].encode('utf8'))
            data = np.fromstring(data, np.uint8)
            im = cv2.imdecode(data, cv2.IMREAD_COLOR)
            img_list.append(im)
        max_wh_ratio = 0
        for i, boximg in enumerate(img_list):
            h, w = boximg.shape[0:2]
            wh_ratio = w * 1.0 / h
            max_wh_ratio = max(max_wh_ratio, wh_ratio)
51 52 53 54
        _, w, h = self.ocr_reader.resize_norm_img(img_list[0],
                                                  max_wh_ratio).shape
        imgs = np.zeros((len(img_list), 3, w, h)).astype('float32')
        for i, img in enumerate(img_list):
D
dyning 已提交
55
            norm_img = self.ocr_reader.resize_norm_img(img, max_wh_ratio)
56 57 58
            imgs[i] = norm_img

        feed = {"image": imgs.copy()}
D
dyning 已提交
59
        fetch = ["ctc_greedy_decoder_0.tmp_0", "softmax_0.tmp_0"]
60
        return feed, fetch, True
D
dyning 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73

    def postprocess(self, feed={}, fetch=[], fetch_map=None):
        rec_res = self.ocr_reader.postprocess(fetch_map, with_score=True)
        res_lst = []
        for res in rec_res:
            res_lst.append(res[0])
        res = {"res": res_lst}
        return res


ocr_service = OCRService(name="ocr")
ocr_service.load_model_config("ocr_rec_model")
ocr_service.init_rec()
74 75
if sys.argv[1] == 'gpu':
    ocr_service.set_gpus("0")
H
HexToString 已提交
76
    ocr_service.prepare_server(workdir="workdir", port=9292, device="gpu")
77 78
elif sys.argv[1] == 'cpu':
    ocr_service.prepare_server(workdir="workdir", port=9292, device="cpu")
D
dyning 已提交
79 80
ocr_service.run_rpc_service()
ocr_service.run_web_service()