bool ReadClipFromFrames()

in caffe2_customized_ops/video/customized_video_io.cc [57:114]


bool ReadClipFromFrames(
    std::string img_dir,
    const int start_frm,
    std::string im_extension,
    const int length,
    const int height,
    const int width,
    const int sampling_rate,
    float*& buffer) {
  char fn_im[512];
  cv::Mat img, img_origin;
  buffer = nullptr;
  int offset = 0;
  int channel_size = 0;
  int image_size = 0;
  int data_size = 0;

  int end_frm = start_frm + length * sampling_rate;
  for (int i = start_frm; i < end_frm; i += sampling_rate) {
    snprintf(fn_im, 512, "%s/%06d%s", img_dir.c_str(), i, im_extension.c_str());
    if (height > 0 && width > 0) {
      img_origin = cv::imread(fn_im, CV_LOAD_IMAGE_COLOR);
      if (!img_origin.data) {
        LOG(ERROR) << "Could not open or find file " << fn_im;
        if (buffer != nullptr) {
          delete[] buffer;
        }
        return false;
      }
      cv::resize(img_origin, img, cv::Size(width, height));
      img_origin.release();
    } else {
      img = cv::imread(fn_im, CV_LOAD_IMAGE_COLOR);
      if (!img.data) {
        LOG(ERROR) << "Could not open or find file " << fn_im;
        if (buffer != nullptr) {
          delete[] buffer;
        }
        return false;
      }
    }

    // If this is the first frame, allocate memory for the buffer
    if (i == start_frm) {
      image_size = img.rows * img.cols;
      channel_size = image_size * length;
      data_size = channel_size * 3;
      buffer = new float[data_size];
    }

    for (int c = 0; c < 3; c++) {
      ImageChannelToBuffer(&img, buffer + c * channel_size + offset, c);
    }
    offset += image_size;
  }
  CAFFE_ENFORCE(offset == channel_size, "Wrong offset size");
  return true;
}