Camera.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "find_object/Camera.h"
29 #include "find_object/Settings.h"
31 #include "find_object/QtOpenCV.h"
32 
33 #include <stdio.h>
34 #include <opencv2/imgproc/imgproc.hpp>
35 #include <QtCore/QFile>
36 #include "utilite/UDirectory.h"
37 #include "CameraTcpServer.h"
38 
39 namespace find_object {
40 
41 Camera::Camera(QObject * parent) :
42  QObject(parent),
43  currentImageIndex_(0),
44  cameraTcpServer_(0)
45 {
46  qRegisterMetaType<cv::Mat>("cv::Mat");
47  connect(&cameraTimer_, SIGNAL(timeout()), this, SLOT(takeImage()));
48 }
49 
51 {
52  this->stop();
53 }
54 
56 {
57  stopTimer();
58  capture_.release();
59  images_.clear();
62  {
63  cameraTcpServer_->close();
64  delete cameraTcpServer_;
65  cameraTcpServer_ = 0;
66  }
67 }
68 
70 {
71  stopTimer();
72 }
73 
75 {
76  if(images_.size())
77  {
78  return images_.size();
79  }
80  else if(capture_.isOpened())
81  {
82  return (int)capture_.get(CV_CAP_PROP_FRAME_COUNT);
83  }
84  return 0;
85 }
86 
88 {
89  if(images_.size())
90  {
91  return currentImageIndex_;
92  }
93  else if(capture_.isOpened())
94  {
95  return (int)capture_.get(CV_CAP_PROP_POS_FRAMES);
96  }
97  return 0;
98 }
99 
100 void Camera::moveToFrame(int frame)
101 {
102  if(frame < images_.size())
103  {
104  currentImageIndex_ = frame;
105  }
106  else if(capture_.isOpened() && frame < (int)capture_.get(CV_CAP_PROP_FRAME_COUNT))
107  {
108  capture_.set(CV_CAP_PROP_POS_FRAMES, frame);
109  }
110 }
111 
113 {
114  if(cameraTcpServer_)
115  {
116  return cameraTcpServer_->getPort();
117  }
118  return 0;
119 }
120 
122 {
123  cv::Mat img;
124  if(capture_.isOpened())
125  {
126  capture_.read(img);// capture a frame
127  }
128  else if(!images_.empty())
129  {
130  if(currentImageIndex_ < (unsigned int)images_.size())
131  {
132  img = cv::imread(images_[currentImageIndex_++]);
133  }
134  }
135  else if(cameraTcpServer_)
136  {
137  img = cameraTcpServer_->getImage();
138  if(cameraTcpServer_->imagesBuffered() > 0 && Settings::getCamera_9queueSize() == 0)
139  {
140  UWARN("%d images buffered so far...", cameraTcpServer_->imagesBuffered());
141  }
142  }
143 
144  if(img.empty())
145  {
146  if(cameraTcpServer_)
147  {
149  {
150  cameraTcpServer_->waitForNewConnection(100);
151  }
152  }
153  else
154  {
155  // In case of a directory of images or a video
156  this->stop();
157  Q_EMIT finished(); // notify that there are no more images
158  }
159  }
160  else
161  {
162  //resize
163  if( Settings::getCamera_2imageWidth() &&
164  Settings::getCamera_3imageHeight() &&
165  Settings::getCamera_2imageWidth() != img.cols &&
166  Settings::getCamera_3imageHeight() != img.rows)
167  {
168  cv::Mat resampled;
169  cv::resize(img, resampled, cv::Size(Settings::getCamera_2imageWidth(), Settings::getCamera_3imageHeight()));
170  Q_EMIT imageReceived(resampled);
171  }
172  else if(capture_.isOpened())
173  {
174  Q_EMIT imageReceived(img.clone()); // clone required with VideoCapture::read()
175  }
176  else
177  {
178  Q_EMIT imageReceived(img); // clone not required with cv::imread()
179  }
180  }
181 }
182 
184 {
185  if(!capture_.isOpened() && images_.empty() && cameraTcpServer_ == 0)
186  {
187  if(Settings::getCamera_6useTcpCamera())
188  {
189  cameraTcpServer_ = new CameraTcpServer(Settings::getCamera_8port(), this);
190  if(!cameraTcpServer_->isListening())
191  {
192  UWARN("CameraTCP: Cannot listen to port %d", cameraTcpServer_->getPort());
193  delete cameraTcpServer_;
194  cameraTcpServer_ = 0;
195  }
196  else
197  {
198  UINFO("CameraTCP: listening to port %d (IP=%s)",
200  cameraTcpServer_->getHostAddress().toString().toStdString().c_str());
201  }
202  }
203  else
204  {
205  QString path = Settings::getCamera_5mediaPath();
206  if(UDirectory::exists(path.toStdString()))
207  {
208  //Images directory
209  QString ext = Settings::getGeneral_imageFormats();
210  ext.remove('*');
211  ext.remove('.');
212  UDirectory dir(path.toStdString(), ext.toStdString()); // this will load fileNames matching the extensions (in natural order)
213  const std::list<std::string> & fileNames = dir.getFileNames();
214  currentImageIndex_ = 0;
215  images_.clear();
216  // Modify to have full path
217  for(std::list<std::string>::const_iterator iter = fileNames.begin(); iter!=fileNames.end(); ++iter)
218  {
219  images_.append(path.toStdString() + UDirectory::separator() + *iter);
220  }
221  UINFO("Camera: Reading %d images from directory \"%s\"...", (int)images_.size(), path.toStdString().c_str());
222  if(images_.isEmpty())
223  {
224  UWARN("Camera: Directory \"%s\" is empty (no images matching the \"%s\" extensions). "
225  "If you want to disable loading automatically this directory, "
226  "clear the Camera/mediaPath parameter. By default, webcam will be used instead of the directory.",
227  path.toStdString().c_str(),
228  ext.toStdString().c_str());
229  }
230  }
231  else if(!path.isEmpty())
232  {
233  //Video file
234  capture_.open(path.toStdString().c_str());
235  if(!capture_.isOpened())
236  {
237  UWARN("Camera: Cannot open file \"%s\". If you want to disable loading "
238  "automatically this video file, clear the Camera/mediaPath parameter. "
239  "By default, webcam will be used instead of the file.", path.toStdString().c_str());
240  }
241  else
242  {
243  UINFO("Camera: Reading from video file \"%s\"...", path.toStdString().c_str());
244  }
245  }
246  if(!capture_.isOpened() && images_.empty())
247  {
248  //set camera device
249  capture_.open(Settings::getCamera_1deviceId());
250  if(Settings::getCamera_2imageWidth() && Settings::getCamera_3imageHeight())
251  {
252  capture_.set(CV_CAP_PROP_FRAME_WIDTH, double(Settings::getCamera_2imageWidth()));
253  capture_.set(CV_CAP_PROP_FRAME_HEIGHT, double(Settings::getCamera_3imageHeight()));
254  }
255  UINFO("Camera: Reading from camera device %d...", Settings::getCamera_1deviceId());
256  }
257  }
258  }
259  if(!capture_.isOpened() && images_.empty() && cameraTcpServer_ == 0)
260  {
261  UERROR("Camera: Failed to open a capture object!");
262  return false;
263  }
264 
265  startTimer();
266  return true;
267 }
268 
270 {
271  updateImageRate();
272  cameraTimer_.start();
273 }
274 
276 {
277  cameraTimer_.stop();
278 }
279 
281 {
282  if(Settings::getCamera_4imageRate())
283  {
284  cameraTimer_.setInterval((int)(1000.0/Settings::getCamera_4imageRate()));
285  }
286  else
287  {
288  cameraTimer_.setInterval(0);
289  }
290 }
291 
292 } // namespace find_object
293 
virtual void takeImage()
Definition: Camera.cpp:121
const std::list< std::string > & getFileNames() const
Definition: UDirectory.h:125
virtual bool start()
Definition: Camera.cpp:183
QTimer cameraTimer_
Definition: Camera.h:72
static std::string separator()
Definition: UDirectory.cpp:365
void imageReceived(const cv::Mat &image)
void moveToFrame(int frame)
Definition: Camera.cpp:100
QHostAddress getHostAddress() const
virtual void updateImageRate()
Definition: Camera.cpp:280
int getTotalFrames()
Definition: Camera.cpp:74
unsigned int currentImageIndex_
Definition: Camera.h:74
virtual void stop()
Definition: Camera.cpp:55
Camera(QObject *parent=0)
Definition: Camera.cpp:41
CameraTcpServer * cameraTcpServer_
Definition: Camera.h:75
#define UERROR(...)
ULogger class and convenient macros.
#define UWARN(...)
cv::VideoCapture capture_
Definition: Camera.h:71
static bool exists(const std::string &dirPath)
Definition: UDirectory.cpp:238
int getCurrentFrameIndex()
Definition: Camera.cpp:87
QList< std::string > images_
Definition: Camera.h:73
virtual ~Camera()
Definition: Camera.cpp:50
#define UINFO(...)


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31