JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
BufferedVideoReader.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18
20
21// ####################################################################################################
22BufferedVideoReader::BufferedVideoReader(std::string const & instance, size_t bufsize) :
23 jevois::Component(instance), itsBuf(bufsize), itsRunning(true)
24{ }
25
26// ####################################################################################################
29
30// ####################################################################################################
32{
33 // Start our reader thread:
34 itsRunFut = jevois::async(std::bind(&BufferedVideoReader::run, this));
35
36 // Wait until we have some data:
37 size_t count = 0;
38 while (itsBuf.filled_size() == 0)
39 {
40 std::this_thread::sleep_for(std::chrono::milliseconds(10));
41 if (count++ == 100)
42 { LINFO("Waiting for " << absolutePath(filename::get()) << " to start loading..."); count = 0; }
43 }
44}
45
46// ####################################################################################################
48{
49 // Tell run() thread to finish up:
50 itsRunning.store(false);
51 while (itsBuf.filled_size()) itsBuf.pop(); // in case run() is blocked trying to push
52
53 JEVOIS_WAIT_GET_FUTURE(itsRunFut);
54}
55
56// ####################################################################################################
58{ return itsBuf.pop(); }
59
60// ####################################################################################################
62{
63 // Open the video file:
64 std::string const path = absolutePath(filename::get());
65 cv::VideoCapture vcap(path);
66 if (vcap.isOpened() == false) { itsBuf.push(cv::Mat()); LERROR("Could not open video file " << path); return; }
67
68 cv::Mat frame;
69 while (itsRunning.load())
70 if (vcap.read(frame)) itsBuf.push(frame); else { itsBuf.push(cv::Mat()); break; }
71}
72
#define JEVOIS_WAIT_GET_FUTURE(f)
void run()
Reader thread.
BufferedVideoReader(std::string const &instance, size_t bufsize=100)
Constructor.
~BufferedVideoReader()
Virtual destructor for safe inheritance.
virtual void postUninit() override
Uninit, wait on our run thread and swallow any exception.
cv::Mat get()
Get the next frame as a BGR cv::Mat, or an empty cv::Mat when the movie is finished.
virtual void postInit() override
Start the thread that loads, decodes and pushes the frames into our buffer.
size_t filled_size() const
void push(T const &val)
std::filesystem::path absolutePath(std::filesystem::path const &path="")
#define LERROR(msg)
#define LINFO(msg)
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)