File size: 4,529 Bytes
73bf052 |
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 46 47 48 49 50 51 52 53 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019 Intel Corporation
#ifndef OPENCV_GAPI_STREAMING_CAP_HPP
#define OPENCV_GAPI_STREAMING_CAP_HPP
/**
* YOUR ATTENTION PLEASE!
*
* This is a header-only implementation of cv::VideoCapture-based
* Stream source. It is not built by default with G-API as G-API
* doesn't depend on videoio module.
*
* If you want to use it in your application, please make sure
* videioio is available in your OpenCV package and is linked to your
* application.
*
* Note for developers: please don't put videoio dependency in G-API
* because of this file.
*/
#include <chrono>
#include <map>
#include <opencv2/videoio.hpp>
#include <opencv2/gapi/garg.hpp>
#include <opencv2/gapi/streaming/meta.hpp>
namespace cv {
namespace gapi {
namespace wip {
/**
* @brief OpenCV's VideoCapture-based streaming source.
*
* This class implements IStreamSource interface.
* Its constructor takes the same parameters as cv::VideoCapture does.
*
* Please make sure that videoio OpenCV module is available before using
* this in your application (G-API doesn't depend on it directly).
*
* @note stream sources are passed to G-API via shared pointers, so
* please gapi::make_src<> to create objects and ptr() to pass a
* GCaptureSource to cv::gin().
*/
class GCaptureSource: public IStreamSource
{
public:
explicit GCaptureSource(int id, const std::map<int, double> &properties = {})
: cap(id) { prep(properties); }
explicit GCaptureSource(const std::string &path,
const std::map<int, double> &properties = {})
: cap(path) { prep(properties); }
void set(int propid, double value) {
cap.set(propid, value);
}
// TODO: Add more constructor overloads to make it
// fully compatible with VideoCapture's interface.
protected:
cv::VideoCapture cap;
cv::Mat first;
bool first_pulled = false;
int64_t counter = 0;
void prep(const std::map<int, double> &properties)
{
for (const auto &it : properties) {
cap.set(it.first, it.second);
}
// Prepare first frame to report its meta to engine
// when needed
GAPI_Assert(first.empty());
cv::Mat tmp;
if (!cap.read(tmp))
{
GAPI_Error("Couldn't grab the very first frame");
}
// NOTE: Some decode/media VideoCapture backends continue
// owning the video buffer under cv::Mat so in order to
// process it safely in a highly concurrent pipeline, clone()
// is the only right way.
first = tmp.clone();
}
virtual bool pull(cv::gapi::wip::Data &data) override
{
if (!first_pulled)
{
GAPI_Assert(!first.empty());
first_pulled = true;
data = first; // no need to clone here since it was cloned already
}
else
{
if (!cap.isOpened()) return false;
cv::Mat frame;
if (!cap.read(frame))
{
// end-of-stream happened
return false;
}
// Same reason to clone as in prep()
data = frame.clone();
}
// Tag data with seq_id/ts
const auto now = std::chrono::system_clock::now();
const auto dur = std::chrono::duration_cast<std::chrono::microseconds>
(now.time_since_epoch());
data.meta[cv::gapi::streaming::meta_tag::timestamp] = int64_t{dur.count()};
data.meta[cv::gapi::streaming::meta_tag::seq_id] = int64_t{counter++};
return true;
}
virtual GMetaArg descr_of() const override
{
GAPI_Assert(!first.empty());
return cv::GMetaArg{cv::descr_of(first)};
}
};
// NB: Overload for using from python
GAPI_EXPORTS_W cv::Ptr<IStreamSource>
inline make_capture_src(const std::string& path,
const std::map<int, double>& properties = {})
{
return make_src<GCaptureSource>(path, properties);
}
// NB: Overload for using from python
GAPI_EXPORTS_W cv::Ptr<IStreamSource>
inline make_capture_src(const int id,
const std::map<int, double>& properties = {})
{
return make_src<GCaptureSource>(id, properties);
}
} // namespace wip
} // namespace gapi
} // namespace cv
#endif // OPENCV_GAPI_STREAMING_CAP_HPP
|