write_json_op.cpp 1.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "pb_to_json.h"
#include <google/protobuf/text_format.h>

#include "op/write_json_op.h"
#include "framework/memory.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

using baidu::paddle_serving::predictor::format::XImageResInstance;
using baidu::paddle_serving::predictor::image_classification::ClassifyResponse;
using baidu::paddle_serving::predictor::image_classification::Response;

int WriteJsonOp::inference() {
    const ClassifyResponse* classify_out =
            get_depend_argument<ClassifyResponse>("image_classify_op");
    if (!classify_out) {
        LOG(ERROR) << "Failed mutable depended argument, op:"
            << "image_classify_op";
        return -1;
    }

    Response* res = mutable_data<Response>();
    if (!res) {
        LOG(ERROR) << "Failed mutable output response in op:" 
            << "WriteJsonOp";
        return -1;
    }

    // transfer classify output message into json format
    std::string err_string;
    uint32_t sample_size = classify_out->predictions_size();
    for (uint32_t si = 0; si < sample_size; si++) {
        XImageResInstance* ins = res->add_predictions();
        if (!ins) {
            LOG(ERROR) << "Failed add one prediction ins"; 
            return -1;
        }
        std::string* text = ins->mutable_response_json();
        if (!ProtoMessageToJson(classify_out->predictions(si), 
                    text, &err_string)) {
            LOG(ERROR) << "Failed convert message[" 
                << classify_out->predictions(si).ShortDebugString()
                << "], err: " << err_string;
            return -1;
        }
    }

    LOG(TRACE) << "Succ write json:"
        << classify_out->ShortDebugString();

    return 0; 
}

DEFINE_OP(WriteJsonOp);

} // predictor
} // paddle_serving
} // baidu