main.cc 2.7 KB
Newer Older
S
sjtubinlong 已提交
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
//   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.

#include <iostream>
#include <string>

#include "humanseg.h" // NOLINT
#include "humanseg_postprocess.h" // NOLINT

// Do predicting on a video file
int VideoPredict(const std::string& video_path, HumanSeg& seg)
{
  cv::VideoCapture capture;
  capture.open(video_path.c_str());
  if (!capture.isOpened()) {
    printf("can not open video : %s\n", video_path.c_str());
    return -1;
  }

  int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  int video_height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  cv::VideoWriter video_out;
  std::string video_out_path = "result.avi";
  video_out.open(video_out_path.c_str(),
                 CV_FOURCC('M', 'J', 'P', 'G'),
                 30.0,
                 cv::Size(video_width, video_height),
                 true);
  if (!video_out.isOpened()) {
    printf("create video writer failed!\n");
    return -1;
  }
  cv::Mat frame;
  while (capture.read(frame)) {
S
sjtubinlong 已提交
46 47 48
    if (frame.empty()) {
      break;
    }
S
sjtubinlong 已提交
49 50 51 52
    cv::Mat out_im = seg.Predict(frame);
    video_out.write(out_im);
  }
  capture.release();
S
sjtubinlong 已提交
53
  video_out.release();
S
sjtubinlong 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  return 0;
}

// Do predicting on a image file
int ImagePredict(const std::string& image_path, HumanSeg& seg)
{
  cv::Mat img = imread(image_path, cv::IMREAD_COLOR);
  cv::Mat out_im = seg.Predict(img);
  imwrite("result.jpeg", out_im);
  return 0;
}

int main(int argc, char* argv[]) {
  if (argc < 3 || argc > 4) {
    std::cout << "Usage:"
              << "./humanseg ./models/ ./data/test.avi"
              << std::endl;
    return -1;
  }

  bool use_gpu = (argc == 4 ? std::stoi(argv[3]) : false);
  auto model_dir = std::string(argv[1]);
  auto input_path = std::string(argv[2]);

  // Init Model
  std::vector<float> means = {104.008, 116.669, 122.675};
  std::vector<float> scale = {1.000, 1.000, 1.000};
  HumanSeg seg(model_dir, means, scale, use_gpu);

  // Call ImagePredict while input_path is a image file path
  // The output will be saved as result.jpeg
S
sjtubinlong 已提交
85
  // ImagePredict(input_path, seg);
S
sjtubinlong 已提交
86 87 88
  
  // Call VideoPredict while input_path is a video file path
  // The output will be saved as result.avi
S
sjtubinlong 已提交
89
  VideoPredict(input_path, seg);
S
sjtubinlong 已提交
90 91
  return 0;
}