从main.cpp传入数据:
std::string pb_process_string;
dy_algorithm_protobuf::algorithm_in_infor input_info;
dy_algorithm_protobuf::dy_algorithm_in *msg1 = input_info.add_algorithm_in_info();
cv::Mat image3 = cv::imread(input_file_list[i],1);
// cv::imwrite("./test/saveimage3_1.jpg",image3);
std::string img_name = input_file_list[i].substr(input_file_list[i].find_last_of('/')+1);
printf("img_name: %s \n", img_name.c_str());
msg1->set_in_id("123456");
msg1->set_in_format(1);
msg1->set_in_width(image3.cols);
msg1->set_in_height(image3.rows);
// msg1->set_in_stride(seqFrameLen);
// std::cout << "image3.channels() " << image3.channels() << "\t" << image3.isContinuous() << "\n";
msg1->set_in_data_bytes(image3.data,image3.rows * image3.cols * image3.channels());
// 设置当前图片的时间戳
msg1->set_in_data_string(std::to_string(i*2));
msg1->set_in_single_len(handler->live_type);
input_info.SerializeToString(&pb_process_string);
sdk内部对二进制数据进行解析:
// pb 数据反序列化
std::string str;
dy_algorithm_protobuf::algorithm_in_infor in_pb;
str.assign(in_pb_buffer, in_pb_length);
if (!in_pb.ParseFromString(str)) {
print_vlog("DYFaceScore", R"("note6":"{}", "pb_str":"{}", "pb_len":"{}")", "input pb parse failed", in_pb_buffer, in_pb_length);
return DY_E_INVALIDARG;
}
if (in_pb.algorithm_in_info_size() != 1) {
print_vlog("DYFaceScore", R"("note7":"{}", "pb_str":"{}", "pb_len":"{}")", "input pb size illegal", in_pb_buffer, in_pb_length);
return DY_E_INVALIDARG;
}
if (!in_pb.algorithm_in_info(0).has_in_width() ||
!in_pb.algorithm_in_info(0).has_in_height() ||
!in_pb.algorithm_in_info(0).has_in_format() ||
// !in_pb.algorithm_in_info(0).has_in_stride() ||
!in_pb.algorithm_in_info(0).has_in_data_bytes() ||
!in_pb.algorithm_in_info(0).has_in_id()) {
print_vlog("DYFaceScore", R"("note8":"{}", "width_flag":"{}", "height_flag":"{}", "format_flag":"{}", "stride":"{}" "data_flag":"{}", in_id flag: {})", "input pb param illegal",
in_pb.algorithm_in_info(0).has_in_width(), in_pb.algorithm_in_info(0).has_in_height(), in_pb.algorithm_in_info(0).has_in_format(), in_pb.algorithm_in_info(0).has_in_stride(), in_pb.algorithm_in_info(0).has_in_data_bytes(), in_pb.algorithm_in_info(0).has_in_id());
return DY_E_INVALIDARG;
}
// 解析in_id
task_id_ = in_pb.algorithm_in_info(0).in_id();
// 获取输入数据
int width = in_pb.algorithm_in_info(0).in_width();
int height = in_pb.algorithm_in_info(0).in_height();
std::string data_str = in_pb.algorithm_in_info(0).in_data_bytes();
input_type_ = in_pb.algorithm_in_info(0).in_format();
#ifdef DEBUG
print_vlog("DYFaceScore", R"(input_type_1111: {})", input_type_);
#endif
if (input_type_ != 0 && input_type_ != 1 && input_type_ != 2){
print_vlog("DYFaceScore", R"("note9":"{}","your input":"{}")", "input type error!,must be 0, 1 or 2",input_type_);
return DY_E_INVALIDARG;
}
if (width <= 0 || height <= 0 || data_str.empty()) {
print_vlog("DYFaceScore", R"("note10":"{}", "width":{}, "height":{})", "input pb data illegal", width, height);
return DY_E_INVALIDARG;
}
seq_frames_.clear();
if (data_str.c_str() == NULL) {
print_vlog("Process2", data_str.c_str(), R"("note12":"{}")", "data_str error");
return DY_E_INVALIDARG;
}
// 二进制数据转Mat序列
data_str = in_pb.algorithm_in_info(0).in_data_bytes();
cv::Mat mat = cv::Mat(height,width,CV_8UC3,(unsigned char *)data_str.c_str());
m_input_mat = mat.clone();
seq_frames_.push_back(m_input_mat);
从main.cpp传入数据:
void process_image2pb(const char *image_file, std::string &in_pb_str) {
std::ifstream infile;
infile.open(image_file, std::ifstream::binary);
infile.seekg(0, std::ios::end);
int file_len = infile.tellg();
infile.seekg(0, std::ios::beg);
char *buffer = new char[file_len];
infile.read(buffer, file_len);
infile.close();
dy_algorithm_protobuf::algorithm_in_infor in_infor;
dy_algorithm_protobuf::dy_algorithm_in *msg = in_infor.add_algorithm_in_info();
msg->set_in_id(std::string(image_file));
msg->set_in_data_bytes(buffer, file_len);
msg->set_in_single_len(file_len);
in_infor.SerializeToString(&in_pb_str);
delete[] buffer;
}
在sdk内部对二进制数据进行解析数据:
// pb 数据反序列化
std::string str;
dy_algorithm_protobuf::algorithm_in_infor in_pb;
str.assign(in_pb_buffer, in_pb_length);
if (!in_pb.ParseFromString(str)) {
print_vlog("DYFaceScore", R"("note6":"{}", "pb_str":"{}", "pb_len":"{}")", "input pb parse failed", in_pb_buffer, in_pb_length);
return DY_E_INVALIDARG;
}
if (in_pb.algorithm_in_info_size() != 1) {
print_vlog("DYFaceScore", R"("note7":"{}", "pb_str":"{}", "pb_len":"{}")", "input pb size illegal", in_pb_buffer, in_pb_length);
return DY_E_INVALIDARG;
}
if (!in_pb.algorithm_in_info(0).has_in_data_bytes() ||!in_pb.algorithm_in_info(0).has_in_id()) {
print_vlog("DYDanceRecognizer", R"("note8":"{}", "data_flag":"{}", in_id flag: {})", "input pb param illegal", in_pb.algorithm_in_info(0).has_in_data_bytes(), in_pb.algorithm_in_info(0).has_in_id());
return DY_E_INVALIDARG;
}
// 解析in_id
task_id_ = in_pb.algorithm_in_info(0).in_id();
// 获取输入数据
std::string data_str = in_pb.algorithm_in_info(0).in_data_bytes();
if (data_str.c_str() == NULL)
{
print_vlog("Process2", data_str.c_str(), R"("note12":"{}")", "data_str error");
return DY_E_INVALIDARG;
}
#ifdef DEBUG
print_vlog("DYFaceScore", R"(input_type_1111: {})", input_type_);
#endif
cv::Mat input_mat = cv::Mat(cv::Size(1, data_str.size()), CV_8UC1, (char *)data_str.c_str());
cv::Mat decode_image(cv::imdecode(input_mat, cv::IMREAD_COLOR));
if (decode_image.empty()) {
print_vlog("Process",R"("note":"{}")", "loadPbVec2Mat decode_image is empty");
return DY_E_INVALIDARG;
}
seq_frames_.clear();
seq_frames_.push_back(decode_image);