bool CustomizedVideoInputOp::GetClipAndLabelFromDBValue()

in caffe2_customized_ops/video/customized_video_input_op.h [253:355]


bool CustomizedVideoInputOp<Context>::GetClipAndLabelFromDBValue(
    const string& value,
    float*& buffer,
    int* label_data,
    std::mt19937* randgen,
    int & height,
    int & width
  ) {
  TensorProtos protos;
  CAFFE_ENFORCE(protos.ParseFromString(value));
  const TensorProto& video_proto = protos.protos(0);
  const TensorProto& label_proto = protos.protos(1);

  int start_frm = -1;
  if (!temporal_jitter_) {
    const TensorProto& start_frm_proto = protos.protos(2);
    start_frm = start_frm_proto.int32_data(0);
  }
  // int start_frm = temporal_jitter_ ? -1 : 0;

  // assign labels
  if (!multiple_label_) {
      label_data[0] = label_proto.int32_data(0);
  } else {
    // For multiple label case, output label is a binary vector
    // where presented concepts are makred 1
    memset(label_data, 0, sizeof(int) * num_of_labels_);
    for (int i = 0; i < label_proto.int32_data_size(); i++) {
      label_data[label_proto.int32_data(i)] = 1;
    }
  }

  if (use_local_file_) {
    CAFFE_ENFORCE_EQ(
        video_proto.data_type(),
        TensorProto::STRING,
        "Database with a file_list is expected to be string data");
  }

  if (video_proto.data_type() == TensorProto::STRING) {
    const string& encoded_video_str = video_proto.string_data(0);
    int encoded_size = encoded_video_str.size();
    if (!use_local_file_) {
      DecodeClipFromMemoryBufferFlex(
          const_cast<char*>(encoded_video_str.data()),
          encoded_size,
          start_frm,
          length_,
          height,
          width,
          sampling_rate_,
          buffer,
          randgen);
    } else { // use local file
      // encoded string contains an absolute path to a local file or folder
      std::string filename = encoded_video_str;
      if (use_image_) {
        LOG(FATAL) << "Branch not implemented.";
        /* CAFFE_ENFORCE(
          !temporal_jitter_,
          "Temporal jittering is not suported for image sequence input"
        );
        CHECK(ReadClipFromFrames(
            filename,
            start_frm,
            im_extension_,
            length_,
            scale_h_,
            scale_w_,
            sampling_rate_,
            buffer)); */
      } else {
        // printf("filename: %s\n", filename.c_str());
        CHECK(DecodeClipFromVideoFileFlex(
            filename,
            start_frm,
            length_,
            height,
            width,
            sampling_rate_,
            buffer,
            randgen,
            sample_times_
          ));
      } // end of else (i.e., use_image_ == False)
    } // end of else (i.e., use_local_file_ == True)
  } else if (video_proto.data_type() == TensorProto::BYTE) {
    LOG(FATAL) << "Branch not implemented.";
    /* DecodeClipFromMemoryBufferFlex(
        video_proto.byte_data().data(),
        video_proto.byte_data().size(),
        start_frm,
        length_,
        height,
        width,
        sampling_rate_,
        buffer,
        randgen); */
  } else {
    LOG(FATAL) << "Unknown video data type.";
  }
  return true;
}