JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
MovieInput.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 #include <jevois/Core/MovieInput.H>
19 #include <jevois/Debug/Log.H>
20 #include <jevois/Util/Utils.H>
22 
23 #include <opencv2/videoio/videoio_c.h> // for CV_CAP_PROP_POS_AVI_RATIO
24 #include <opencv2/imgproc/imgproc.hpp>
25 
26 // ##############################################################################################################
27 jevois::MovieInput::MovieInput(std::string const & filename, unsigned int const nbufs) :
28  jevois::VideoInput(filename, nbufs)
29 {
30  // Open the movie file:
31  if (itsCap.open(filename) == false) LFATAL("Failed to open movie or image sequence [" << filename << ']');
32 }
33 
34 // ##############################################################################################################
36 { }
37 
38 // ##############################################################################################################
40 { }
41 
42 // ##############################################################################################################
44 { }
45 
46 // ##############################################################################################################
48 { }
49 
50 // ##############################################################################################################
52 {
53  static size_t frameidx = 0; // only used for conversion info messages
54 
55  // Grab the next frame:
56  cv::Mat frame;
57  if (itsCap.read(frame) == false)
58  {
59  LINFO("End of input - Rewinding...");
60 
61  // Maybe end of file, reset the position:
62  itsCap.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
63 
64  // Try again:
65  if (itsCap.read(frame) == false) LFATAL("Could not read next video frame");
66  }
67 
68  // If dims do not match, resize:
69  if (frame.cols != int(itsMapping.cw) || frame.rows != int(itsMapping.ch))
70  {
71  if (frameidx++ % 100 == 0)
72  LINFO("Resizing frame from " << frame.cols <<'x'<< frame.rows << " to " << itsMapping.cw <<'x'<< itsMapping.ch);
73  cv::resize(frame, frame, cv::Size(itsMapping.cw, itsMapping.ch));
74  }
75 
76  // Reset our VideoBuf:
77  itsBuf.reset(new jevois::VideoBuf(-1, itsMapping.csize(), 0));
78 
79  // Set the fields in our output RawImage:
80  img.width = itsMapping.cw;
81  img.height = itsMapping.ch;
82  img.fmt = itsMapping.cfmt;
83  img.fps = itsMapping.cfps;
84  img.buf = itsBuf;
85  img.bufindex = 0;
86 
87  // Now convert from BGR to desired color format:
89 }
90 
91 // ##############################################################################################################
92 void jevois::MovieInput::done(RawImage & JEVOIS_UNUSED_PARAM(img))
93 {
94  // Just nuke our buffer:
95  itsBuf.reset();
96 }
97 
98 // ##############################################################################################################
99 void jevois::MovieInput::queryControl(struct v4l2_queryctrl & JEVOIS_UNUSED_PARAM(qc)) const
100 { throw std::runtime_error("Operation queryControl() not supported by MovieInput"); }
101 
102 // ##############################################################################################################
103 void jevois::MovieInput::queryMenu(struct v4l2_querymenu & JEVOIS_UNUSED_PARAM(qm)) const
104 { throw std::runtime_error("Operation queryMenu() not supported by MovieInput"); }
105 
106 // ##############################################################################################################
107 void jevois::MovieInput::getControl(struct v4l2_control & JEVOIS_UNUSED_PARAM(ctrl)) const
108 { throw std::runtime_error("Operation getControl() not supported by MovieInput"); }
109 
110 // ##############################################################################################################
111 void jevois::MovieInput::setControl(struct v4l2_control const & JEVOIS_UNUSED_PARAM(ctrl))
112 { throw std::runtime_error("Operation setControl() not supported by MovieInput"); }
113 
114 // ##############################################################################################################
116 {
117  // Store the mapping so we can check frame size and format when grabbing:
118  itsMapping = m;
119 }
120 
121 // ##############################################################################################################
122 void jevois::MovieInput::writeRegister(unsigned char JEVOIS_UNUSED_PARAM(reg), unsigned char JEVOIS_UNUSED_PARAM(val))
123 { LFATAL("Operation not supported by MovieInput"); }
124 
125 // ##############################################################################################################
126 unsigned char jevois::MovieInput::readRegister(unsigned char JEVOIS_UNUSED_PARAM(reg))
127 { LFATAL("Operation not supported by MovieInput"); }
virtual void queryControl(struct v4l2_queryctrl &qc) const override
Get information about a control, throw if unsupported by hardware.
Definition: MovieInput.C:99
virtual void writeRegister(unsigned char reg, unsigned char val) override
Write a value of one of the camera&#39;s registers.
Definition: MovieInput.C:122
virtual void abortStream() override
Abort streaming.
Definition: MovieInput.C:43
virtual void queryMenu(struct v4l2_querymenu &qm) const override
Get the available menu entry names for a menu-type control, throw if unsupported by hardware...
Definition: MovieInput.C:103
unsigned int height
Image height in pixels.
Definition: RawImage.H:146
virtual void streamOff() override
Stop streaming.
Definition: MovieInput.C:47
MovieInput(std::string const &filename, unsigned int const nbufs=3)
Constructor, opens the movie file.
Definition: MovieInput.C:27
A V4L2 video buffer, to be held in a shared_ptr.
Definition: VideoBuf.H:29
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
Definition: RawImage.H:147
virtual void streamOn() override
Start streaming.
Definition: MovieInput.C:39
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
virtual void setFormat(VideoMapping const &m) override
Set the video format and frame rate.
Definition: MovieInput.C:115
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
virtual void get(RawImage &img) override
Get the next frame from the video file, possibly looping back to start if end is reached.
Definition: MovieInput.C:51
unsigned int ch
camera height
Definition: VideoMapping.H:50
unsigned int csize() const
Return the size in bytes of a camera image.
Definition: VideoMapping.C:41
Simple struct to hold video mapping definitions for the processing Engine.
Definition: VideoMapping.H:41
virtual void done(RawImage &img) override
Indicate that user processing is done with an image previously obtained via get() ...
Definition: MovieInput.C:92
virtual void getControl(struct v4l2_control &ctrl) const override
Get a control&#39;s current value, throw if unsupported by hardware.
Definition: MovieInput.C:107
void convertCvBGRtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1180
Base class for video input, which will get derived into Camera and MovieInput.
Definition: VideoInput.H:31
virtual void setControl(struct v4l2_control const &ctrl) override
Set a control, throw if the hardware rejects the value.
Definition: MovieInput.C:111
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:212
virtual unsigned char readRegister(unsigned char reg) override
Read a value from one of the camera&#39;s registers.
Definition: MovieInput.C:126
unsigned int cfmt
camera pixel format
Definition: VideoMapping.H:48
virtual ~MovieInput()
Virtual destructor for save inheritance.
Definition: MovieInput.C:35
unsigned int cw
camera width
Definition: VideoMapping.H:49
float fps
Programmed frames/s as given by current video mapping, may not be actual.
Definition: RawImage.H:148
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:176
size_t bufindex
The index of the data buffer in the kernel driver.
Definition: RawImage.H:150
VideoMapping itsMapping
Our current video mapping, we resize the input to the mapping&#39;s camera dims.
Definition: MovieInput.H:94
std::shared_ptr< VideoBuf > itsBuf
Our single video buffer.
Definition: MovieInput.H:93
unsigned int width
Image width in pixels.
Definition: RawImage.H:145
cv::VideoCapture itsCap
Our OpenCV video capture, works on movie and image files too.
Definition: MovieInput.H:92
float cfps
camera frame rate in frames/sec
Definition: VideoMapping.H:51