JeVoisBase  1.17
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
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 // ####################################################################################################
22 BufferedVideoReader::BufferedVideoReader(std::string const & instance, size_t bufsize) :
23  jevois::Component(instance), itsBuf(bufsize), itsRunning(true)
24 { }
25 
26 // ####################################################################################################
28 { }
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 
JEVOIS_WAIT_GET_FUTURE
#define JEVOIS_WAIT_GET_FUTURE(f)
BufferedVideoReader::~BufferedVideoReader
~BufferedVideoReader()
Virtual destructor for safe inheritance.
Definition: BufferedVideoReader.C:27
LERROR
#define LERROR(msg)
jevois
jevois::BoundedBuffer::pop
T pop()
jevois::BoundedBuffer::push
void push(T const &val)
jevois::BoundedBuffer::filled_size
size_t filled_size() const
BufferedVideoReader::BufferedVideoReader
BufferedVideoReader(std::string const &instance, size_t bufsize=100)
Constructor.
Definition: BufferedVideoReader.C:22
jevois::async
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
test-model.frame
frame
Definition: test-model.py:22
BufferedVideoReader::postUninit
virtual void postUninit() override
Uninit, wait on our run thread and swallow any exception.
Definition: BufferedVideoReader.C:47
BufferedVideoReader::get
cv::Mat get()
Get the next frame as a BGR cv::Mat, or an empty cv::Mat when the movie is finished.
Definition: BufferedVideoReader.C:57
BufferedVideoReader.H
BufferedVideoReader::run
void run()
Reader thread.
Definition: BufferedVideoReader.C:61
BufferedVideoReader::postInit
virtual void postInit() override
Start the thread that loads, decodes and pushes the frames into our buffer.
Definition: BufferedVideoReader.C:31
LINFO
#define LINFO(msg)
jevois::Component::absolutePath
std::string absolutePath(std::string const &path="")