github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/streaming/cap.hpp (about)

     1  // This file is part of OpenCV project.
     2  // It is subject to the license terms in the LICENSE file found in the top-level directory
     3  // of this distribution and at http://opencv.org/license.html.
     4  //
     5  // Copyright (C) 2019 Intel Corporation
     6  
     7  #ifndef OPENCV_GAPI_STREAMING_CAP_HPP
     8  #define OPENCV_GAPI_STREAMING_CAP_HPP
     9  
    10  /**
    11   * YOUR ATTENTION PLEASE!
    12   *
    13   * This is a header-only implementation of cv::VideoCapture-based
    14   * Stream source.  It is not built by default with G-API as G-API
    15   * doesn't depend on videoio module.
    16   *
    17   * If you want to use it in your application, please make sure
    18   * videioio is available in your OpenCV package and is linked to your
    19   * application.
    20   *
    21   * Note for developers: please don't put videoio dependency in G-API
    22   * because of this file.
    23   */
    24  #include <chrono>
    25  
    26  #include <opencv2/videoio.hpp>
    27  #include <opencv2/gapi/garg.hpp>
    28  #include <opencv2/gapi/streaming/meta.hpp>
    29  
    30  namespace cv {
    31  namespace gapi {
    32  namespace wip {
    33  
    34  /**
    35   * @brief OpenCV's VideoCapture-based streaming source.
    36   *
    37   * This class implements IStreamSource interface.
    38   * Its constructor takes the same parameters as cv::VideoCapture does.
    39   *
    40   * Please make sure that videoio OpenCV module is available before using
    41   * this in your application (G-API doesn't depend on it directly).
    42   *
    43   * @note stream sources are passed to G-API via shared pointers, so
    44   *  please gapi::make_src<> to create objects and ptr() to pass a
    45   *  GCaptureSource to cv::gin().
    46   */
    47  class GCaptureSource: public IStreamSource
    48  {
    49  public:
    50      explicit GCaptureSource(int id) : cap(id) { prep(); }
    51      explicit GCaptureSource(const std::string &path) : cap(path) { prep(); }
    52  
    53      // TODO: Add more constructor overloads to make it
    54      // fully compatible with VideoCapture's interface.
    55  
    56  protected:
    57      cv::VideoCapture cap;
    58      cv::Mat first;
    59      bool first_pulled = false;
    60      int64_t counter = 0;
    61  
    62      void prep()
    63      {
    64          // Prepare first frame to report its meta to engine
    65          // when needed
    66          GAPI_Assert(first.empty());
    67          cv::Mat tmp;
    68          if (!cap.read(tmp))
    69          {
    70              GAPI_Assert(false && "Couldn't grab the very first frame");
    71          }
    72          // NOTE: Some decode/media VideoCapture backends continue
    73          // owning the video buffer under cv::Mat so in order to
    74          // process it safely in a highly concurrent pipeline, clone()
    75          // is the only right way.
    76          first = tmp.clone();
    77      }
    78  
    79      virtual bool pull(cv::gapi::wip::Data &data) override
    80      {
    81          if (!first_pulled)
    82          {
    83              GAPI_Assert(!first.empty());
    84              first_pulled = true;
    85              data = first; // no need to clone here since it was cloned already
    86          }
    87          else
    88          {
    89              if (!cap.isOpened()) return false;
    90  
    91              cv::Mat frame;
    92              if (!cap.read(frame))
    93              {
    94                  // end-of-stream happened
    95                  return false;
    96              }
    97              // Same reason to clone as in prep()
    98              data = frame.clone();
    99          }
   100          // Tag data with seq_id/ts
   101          const auto now = std::chrono::system_clock::now();
   102          const auto dur = std::chrono::duration_cast<std::chrono::microseconds>
   103              (now.time_since_epoch());
   104          data.meta[cv::gapi::streaming::meta_tag::timestamp] = int64_t{dur.count()};
   105          data.meta[cv::gapi::streaming::meta_tag::seq_id]    = int64_t{counter++};
   106          return true;
   107      }
   108  
   109      virtual GMetaArg descr_of() const override
   110      {
   111          GAPI_Assert(!first.empty());
   112          return cv::GMetaArg{cv::descr_of(first)};
   113      }
   114  };
   115  
   116  // NB: Overload for using from python
   117  GAPI_EXPORTS_W cv::Ptr<IStreamSource> inline make_capture_src(const std::string& path)
   118  {
   119      return make_src<GCaptureSource>(path);
   120  }
   121  
   122  } // namespace wip
   123  } // namespace gapi
   124  } // namespace cv
   125  
   126  #endif // OPENCV_GAPI_STREAMING_CAP_HPP