|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_STREAMING_CAP_HPP
|
|
|
#define OPENCV_GAPI_STREAMING_CAP_HPP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GAPI_Assert(first.empty());
|
|
|
cv::Mat tmp;
|
|
|
if (!cap.read(tmp))
|
|
|
{
|
|
|
GAPI_Error("Couldn't grab the very first frame");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
first = tmp.clone();
|
|
|
}
|
|
|
|
|
|
virtual bool pull(cv::gapi::wip::Data &data) override
|
|
|
{
|
|
|
if (!first_pulled)
|
|
|
{
|
|
|
GAPI_Assert(!first.empty());
|
|
|
first_pulled = true;
|
|
|
data = first;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (!cap.isOpened()) return false;
|
|
|
|
|
|
cv::Mat frame;
|
|
|
if (!cap.read(frame))
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
data = frame.clone();
|
|
|
}
|
|
|
|
|
|
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)};
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|