JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
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
19#include <jevois/Debug/Log.H>
20#include <jevois/Util/Utils.H>
22
23#include <opencv2/videoio.hpp> // for CV_CAP_PROP_POS_AVI_RATIO
24#include <opencv2/imgproc/imgproc.hpp>
25
26// ##############################################################################################################
27jevois::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// ##############################################################################################################
37
38// ##############################################################################################################
41
42// ##############################################################################################################
45
46// ##############################################################################################################
49
50// ##############################################################################################################
52{
53 return (itsMapping.c2fmt != 0);
54}
55
56// ##############################################################################################################
58{
59 static size_t frameidx = 0; // only used for conversion info messages
60
61 // Users may call get() several times on a given frame. The switch to the next frame is when done() is called, which
62 // invalidates itsBuf:
63 if (itsBuf)
64 {
65 // Just pass the buffer to the rawimage:
66 img.width = itsMapping.cw;
67 img.height = itsMapping.ch;
68 img.fmt = itsMapping.cfmt;
69 img.fps = itsMapping.cfps;
70 img.buf = itsBuf;
71 img.bufindex = 0;
72
73 return;
74 }
75
76 // Grab the next frame:
77 if (itsCap.read(itsRawFrame) == false)
78 {
79 LINFO("End of input - Rewinding...");
80
81 // Maybe end of file, reset the position:
82 itsCap.set(cv::CAP_PROP_POS_AVI_RATIO, 0);
83 itsCap.set(cv::CAP_PROP_POS_FRAMES, 0);
84
85 // Try again:
86 if (itsCap.read(itsRawFrame) == false) LFATAL("Could not read next video frame");
87 }
88
89 // If dims do not match, resize:
90 cv::Mat frame;
91 if (itsRawFrame.cols != int(itsMapping.cw) || itsRawFrame.rows != int(itsMapping.ch))
92 {
93 if ((frameidx++ % 100) == 0)
94 LINFO("Note: Resizing get() frame from " << itsRawFrame.cols <<'x'<< itsRawFrame.rows << " to " <<
95 itsMapping.cw <<'x'<< itsMapping.ch);
96 cv::resize(itsRawFrame, frame, cv::Size(itsMapping.cw, itsMapping.ch));
97 }
98 else frame = itsRawFrame;
99
100 // Reset our VideoBuf:
101 itsBuf.reset(new jevois::VideoBuf(-1, itsMapping.csize(), 0, -1));
102
103 // Set the fields in our output RawImage:
104 img.width = itsMapping.cw;
105 img.height = itsMapping.ch;
106 img.fmt = itsMapping.cfmt;
107 img.fps = itsMapping.cfps;
108 img.buf = itsBuf;
109 img.bufindex = 0;
110
111 // Now convert from BGR to desired color format:
113}
114
115// ##############################################################################################################
117{
118 static size_t frameidx2 = 0; // only used for conversion info messages
119
120 // Users may call get2() several times on a given frame. The switch to the next frame is when done2() is called, which
121 // invalidates itsBuf2:
122 if (itsBuf2)
123 {
124 // Just pass the buffer to the rawimage:
125 img.width = itsMapping.c2w;
126 img.height = itsMapping.c2h;
127 img.fmt = itsMapping.c2fmt;
128 img.fps = itsMapping.cfps;
129 img.buf = itsBuf2;
130 img.bufindex = 0;
131
132 return;
133 }
134
135 // If get2() is called before get() let's call get() now:
136 if (! itsBuf)
137 {
139 get(tmp);
140 }
141
142 // Now both itsBuf and itsRawFrame are valid, let's just convert/resize itsRawFrame into our second frame format:
143 cv::Mat frame;
144 if (itsRawFrame.cols != int(itsMapping.c2w) || itsRawFrame.rows != int(itsMapping.c2h))
145 {
146 if ((frameidx2++ % 100) == 0)
147 LINFO("Note: Resizing get2() frame from " << itsRawFrame.cols <<'x'<< itsRawFrame.rows << " to " <<
148 itsMapping.c2w <<'x'<< itsMapping.c2h);
149 cv::resize(itsRawFrame, frame, cv::Size(itsMapping.c2w, itsMapping.c2h));
150 }
151 else frame = itsRawFrame;
152
153 // Reset our VideoBuf:
154 itsBuf2.reset(new jevois::VideoBuf(-1, itsMapping.c2size(), 0, -1));
155
156 // Set the fields in our output RawImage:
157 img.width = itsMapping.c2w;
158 img.height = itsMapping.c2h;
159 img.fmt = itsMapping.c2fmt;
160 img.fps = itsMapping.cfps;
161 img.buf = itsBuf2;
162 img.bufindex = 0;
163
164 // Now convert from BGR to desired color format:
166}
167
168// ##############################################################################################################
170{
171 // Just nuke our buffer:
172 itsBuf.reset();
173 itsRawFrame = cv::Mat();
174}
175
176// ##############################################################################################################
178{
179 // Just nuke our buffer:
180 itsBuf2.reset();
181}
182
183// ##############################################################################################################
184void jevois::MovieInput::queryControl(struct v4l2_queryctrl &) const
185{ throw std::runtime_error("Operation queryControl() not supported by MovieInput"); }
186
187// ##############################################################################################################
188void jevois::MovieInput::queryMenu(struct v4l2_querymenu &) const
189{ throw std::runtime_error("Operation queryMenu() not supported by MovieInput"); }
190
191// ##############################################################################################################
192void jevois::MovieInput::getControl(struct v4l2_control &) const
193{ throw std::runtime_error("Operation getControl() not supported by MovieInput"); }
194
195// ##############################################################################################################
196void jevois::MovieInput::setControl(struct v4l2_control const &)
197{ throw std::runtime_error("Operation setControl() not supported by MovieInput"); }
198
199// ##############################################################################################################
201{
202 // Store the mapping so we can check frame size and format when grabbing:
203 itsMapping = m;
204}
MovieInput(std::string const &filename, unsigned int const nbufs=3)
Constructor, opens the movie file.
Definition MovieInput.C:27
virtual bool hasScaledImage() const override
Check whether a second input image scaled by the JeVoisPro Platform ISP is available.
Definition MovieInput.C:51
void setFormat(VideoMapping const &m) override
Set the video format and frame rate.
Definition MovieInput.C:200
virtual ~MovieInput()
Virtual destructor for safe inheritance.
Definition MovieInput.C:35
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:57
void streamOn() override
Start streaming.
Definition MovieInput.C:39
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:188
void getControl(struct v4l2_control &ctrl) const override
Get a control's current value, throw if unsupported by hardware.
Definition MovieInput.C:192
void done(RawImage &img) override
Indicate that user processing is done with an image previously obtained via get()
Definition MovieInput.C:169
cv::VideoCapture itsCap
Our OpenCV video capture, works on movie and image files too.
Definition MovieInput.H:102
void queryControl(struct v4l2_queryctrl &qc) const override
Get information about a control, throw if unsupported by hardware.
Definition MovieInput.C:184
void done2(RawImage &img) override
Indicate that user processing is done with an image previously obtained via get()
Definition MovieInput.C:177
void streamOff() override
Stop streaming.
Definition MovieInput.C:47
virtual void get2(RawImage &img) override
Get the next captured ISP-scaled secondary buffer.
Definition MovieInput.C:116
void setControl(struct v4l2_control const &ctrl) override
Set a control, throw if the hardware rejects the value.
Definition MovieInput.C:196
void abortStream() override
Abort streaming.
Definition MovieInput.C:43
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
float fps
Programmed frames/s as given by current video mapping, may not be actual.
Definition RawImage.H:148
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
Definition RawImage.H:147
size_t bufindex
The index of the data buffer in the kernel driver.
Definition RawImage.H:150
unsigned int width
Image width in pixels.
Definition RawImage.H:145
unsigned int height
Image height in pixels.
Definition RawImage.H:146
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition RawImage.H:149
A V4L2 video buffer, to be held in a shared_ptr.
Definition VideoBuf.H:30
Base class for video input, which will get derived into Camera and MovieInput.
Definition VideoInput.H:32
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
void convertCvBGRtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2
Simple struct to hold video mapping definitions for the processing Engine.