2011-10-25 15:48:19 +00:00
/*
2011-12-02 18:34:08 +00:00
* Copyright ( C ) 2011 , Mathieu Labbe - IntRoLab - Universite de Sherbrooke
2011-10-25 15:48:19 +00:00
*/
# include "Camera.h"
# include <stdio.h>
2012-05-16 01:42:10 +00:00
# include <opencv2/imgproc/imgproc.hpp>
2011-11-23 16:44:14 +00:00
# include "Settings.h"
2012-01-30 03:16:30 +00:00
# include <QtCore/QFile>
2012-08-28 20:06:43 +00:00
# include "utilite/UDirectory.h"
2011-10-25 15:48:19 +00:00
2011-11-23 16:44:14 +00:00
Camera : : Camera ( QObject * parent ) :
2012-08-28 20:06:43 +00:00
QObject ( parent ) ,
currentImageIndex_ ( 0 )
2011-10-25 15:48:19 +00:00
{
2011-11-23 18:19:55 +00:00
qRegisterMetaType < cv : : Mat > ( " cv::Mat " ) ;
2011-11-23 16:44:14 +00:00
connect ( & cameraTimer_ , SIGNAL ( timeout ( ) ) , this , SLOT ( takeImage ( ) ) ) ;
2011-10-25 15:48:19 +00:00
}
Camera : : ~ Camera ( )
{
2011-11-23 16:44:14 +00:00
this - > stop ( ) ;
2011-10-25 15:48:19 +00:00
}
2011-11-23 16:44:14 +00:00
void Camera : : stop ( )
2011-10-25 15:48:19 +00:00
{
2011-11-24 00:00:37 +00:00
stopTimer ( ) ;
2012-05-16 01:42:10 +00:00
capture_ . release ( ) ;
2012-08-28 20:06:43 +00:00
images_ . clear ( ) ;
currentImageIndex_ = 0 ;
2011-10-25 15:48:19 +00:00
}
2012-01-30 03:16:30 +00:00
void Camera : : pause ( )
{
stopTimer ( ) ;
}
2012-08-28 20:06:43 +00:00
int Camera : : getTotalFrames ( )
{
if ( images_ . size ( ) )
{
return images_ . size ( ) ;
}
else if ( capture_ . isOpened ( ) )
{
return ( int ) capture_ . get ( CV_CAP_PROP_FRAME_COUNT ) ;
}
return 0 ;
}
int Camera : : getCurrentFrameIndex ( )
{
if ( images_ . size ( ) )
{
return currentImageIndex_ ;
}
else if ( capture_ . isOpened ( ) )
{
return ( int ) capture_ . get ( CV_CAP_PROP_POS_FRAMES ) ;
}
return 0 ;
}
void Camera : : moveToFrame ( int frame )
{
if ( frame < images_ . size ( ) )
{
currentImageIndex_ = frame ;
}
else if ( capture_ . isOpened ( ) & & frame < ( int ) capture_ . get ( CV_CAP_PROP_FRAME_COUNT ) )
{
capture_ . set ( CV_CAP_PROP_POS_FRAMES , frame ) ;
}
}
2011-11-23 16:44:14 +00:00
void Camera : : takeImage ( )
2011-10-25 15:48:19 +00:00
{
2012-08-28 20:06:43 +00:00
cv : : Mat img ;
2012-05-16 01:42:10 +00:00
if ( capture_ . isOpened ( ) )
2011-10-25 15:48:19 +00:00
{
2012-05-16 01:42:10 +00:00
capture_ . read ( img ) ; // capture a frame
2012-08-28 20:06:43 +00:00
}
else if ( ! images_ . empty ( ) )
{
2012-08-31 14:09:25 +00:00
if ( currentImageIndex_ < ( unsigned int ) images_ . size ( ) )
2012-08-28 20:06:43 +00:00
{
img = cv : : imread ( images_ [ currentImageIndex_ + + ] ) ;
}
}
if ( img . empty ( ) )
{
printf ( " Camera: Could not grab a frame, the end of the feed may be reached... \n " ) ;
}
else
{
//resize
if ( Settings : : getCamera_2imageWidth ( ) & &
Settings : : getCamera_3imageHeight ( ) & &
Settings : : getCamera_2imageWidth ( ) ! = img . cols & &
Settings : : getCamera_3imageHeight ( ) ! = img . rows )
{
cv : : Mat resampled ;
cv : : resize ( img , resampled , cv : : Size ( Settings : : getCamera_2imageWidth ( ) , Settings : : getCamera_3imageHeight ( ) ) ) ;
emit imageReceived ( resampled ) ;
}
else if ( capture_ . isOpened ( ) )
2011-10-25 15:48:19 +00:00
{
2012-08-28 20:06:43 +00:00
emit imageReceived ( img . clone ( ) ) ; // clone required with VideoCapture::read()
2011-10-25 15:48:19 +00:00
}
else
{
2012-08-28 20:06:43 +00:00
emit imageReceived ( img ) ; // clone not required with cv::imread()
2011-11-23 16:44:14 +00:00
}
2011-10-25 15:48:19 +00:00
}
2011-11-23 16:44:14 +00:00
}
bool Camera : : start ( )
{
2012-08-28 20:06:43 +00:00
if ( ! capture_ . isOpened ( ) & & images_ . empty ( ) )
2011-10-25 15:48:19 +00:00
{
2012-08-28 20:06:43 +00:00
QString path = Settings : : getCamera_5mediaPath ( ) ;
if ( UDirectory : : exists ( path . toStdString ( ) ) )
{
//Images directory
QString ext = Settings : : getGeneral_imageFormats ( ) ;
ext . remove ( ' * ' ) ;
ext . remove ( ' . ' ) ;
UDirectory dir ( path . toStdString ( ) , ext . toStdString ( ) ) ; // this will load fileNames matching the extensions (in natural order)
const std : : list < std : : string > & fileNames = dir . getFileNames ( ) ;
currentImageIndex_ = 0 ;
images_ . clear ( ) ;
// Modify to have full path
for ( std : : list < std : : string > : : const_iterator iter = fileNames . begin ( ) ; iter ! = fileNames . end ( ) ; + + iter )
{
images_ . append ( path . toStdString ( ) + UDirectory : : separator ( ) + * iter ) ;
}
2014-05-21 15:24:24 +00:00
printf ( " Camera: Reading %d images from directory \" %s \" ... \n " , ( int ) images_ . size ( ) , path . toStdString ( ) . c_str ( ) ) ;
2012-08-28 20:06:43 +00:00
if ( images_ . isEmpty ( ) )
{
2014-05-21 15:24:24 +00:00
printf ( " [WARNING] Directory \" %s \" is empty (no images matching the \" %s \" extensions). "
2012-08-28 20:06:43 +00:00
" If you want to disable loading automatically this directory, "
" clear the Camera/mediaPath parameter. By default, webcam will be used instead of the directory. \n " ,
path . toStdString ( ) . c_str ( ) ,
ext . toStdString ( ) . c_str ( ) ) ;
}
}
else if ( ! path . isEmpty ( ) )
2012-01-30 03:16:30 +00:00
{
2012-08-28 20:06:43 +00:00
//Video file
capture_ . open ( path . toStdString ( ) . c_str ( ) ) ;
2012-05-16 01:42:10 +00:00
if ( ! capture_ . isOpened ( ) )
2012-01-30 03:16:30 +00:00
{
2014-05-21 15:24:24 +00:00
printf ( " [WARNING] Cannot open file \" %s \" . If you want to disable loading automatically this video file, clear the Camera/mediaPath parameter. By default, webcam will be used instead of the file. \n " , path . toStdString ( ) . c_str ( ) ) ;
}
else
{
printf ( " Camera: Reading from video file \" %s \" ... \n " , path . toStdString ( ) . c_str ( ) ) ;
2012-01-30 03:16:30 +00:00
}
}
2012-08-28 20:06:43 +00:00
if ( ! capture_ . isOpened ( ) & & images_ . empty ( ) )
2011-11-23 16:44:14 +00:00
{
2012-05-16 01:42:10 +00:00
//set camera device
2012-08-28 13:44:57 +00:00
capture_ . open ( Settings : : getCamera_1deviceId ( ) ) ;
if ( Settings : : getCamera_2imageWidth ( ) & & Settings : : getCamera_3imageHeight ( ) )
2012-01-30 03:16:30 +00:00
{
2012-08-28 13:44:57 +00:00
capture_ . set ( CV_CAP_PROP_FRAME_WIDTH , double ( Settings : : getCamera_2imageWidth ( ) ) ) ;
capture_ . set ( CV_CAP_PROP_FRAME_HEIGHT , double ( Settings : : getCamera_3imageHeight ( ) ) ) ;
2012-01-30 03:16:30 +00:00
}
2014-05-21 15:24:24 +00:00
printf ( " Camera: Reading from camera device %d... \n " , Settings : : getCamera_1deviceId ( ) ) ;
2011-11-23 16:44:14 +00:00
}
2011-10-25 15:48:19 +00:00
}
2012-08-28 20:06:43 +00:00
if ( ! capture_ . isOpened ( ) & & images_ . empty ( ) )
2011-11-24 00:00:37 +00:00
{
2014-05-21 15:24:24 +00:00
printf ( " [ERROR] Failed to open a capture object! \n " ) ;
2011-11-24 00:00:37 +00:00
return false ;
}
2011-10-25 15:48:19 +00:00
2011-11-24 00:00:37 +00:00
startTimer ( ) ;
2011-11-23 16:44:14 +00:00
return true ;
}
2011-11-24 00:00:37 +00:00
void Camera : : startTimer ( )
{
updateImageRate ( ) ;
cameraTimer_ . start ( ) ;
}
void Camera : : stopTimer ( )
{
cameraTimer_ . stop ( ) ;
}
2011-11-23 16:44:14 +00:00
void Camera : : updateImageRate ( )
{
2012-08-28 13:44:57 +00:00
if ( Settings : : getCamera_4imageRate ( ) )
2011-11-24 00:00:37 +00:00
{
2012-08-28 20:06:43 +00:00
cameraTimer_ . setInterval ( ( int ) ( 1000.0 / Settings : : getCamera_4imageRate ( ) ) ) ;
2011-11-24 00:00:37 +00:00
}
else
{
cameraTimer_ . setInterval ( 0 ) ;
}
2011-10-25 15:48:19 +00:00
}