Esempio n. 1
0
// in a loop, capture frame from camera and detect faces
// send eyes coordinates to change view
void QOpenCV::FaceRecognitionThread::run()
{
	mCancel = false;
	cv::Mat image;
	OpenCV::FaceRecognizer*	mFaceRecognizer = new OpenCV::FaceRecognizer();

	if ( mCapVideo == NULL ) {
		qDebug() << "[FaceRecognitionThread::run()]  Camera is not set";
		return;
	}

	while ( !mCancel ) {
		// get image from camera
		image = mCapVideo->queryFrame();

		cv::cvtColor( image, image, CV_BGR2RGB );

		// we detect faces on grayscale image
		mFaceRecognizer->detectFaces( mCapVideo->getGrayframe() );
		mFaceRecognizer->annotateFaces( image );

		cv::flip( image, image, 1 );

		// show image
		if ( mSendImgEnabled && !image.empty() ) {
			if ( image.data ) {
				emit pushImage( image.clone() );    // ???
			}
		}
		if ( mSendBackgrImgEnabled && !image.empty() ) {
			emit pushBackgrImage( image.clone() );
		}

		// when face was detected along with movement (implemented with threshold)
		// send eyes coordinate to change view
		if ( mFaceRecognizer->detected ) { //&& mFaceRecognizer->isMovement
			emit sendEyesCoords( static_cast<float>( -mFaceRecognizer->getEyesCoords().x ),
								 static_cast<float>( -mFaceRecognizer->getEyesCoords().y ),
								 -mFaceRecognizer->getHeadDistance( mCapVideo->getWidth() ) );
			// Kostan:
			//  focalLength - used my webcam focal length
			//  0.1481m - face width avg (15yr - 40yr)
			float focalLength = 749.3f;
			float distance = mFaceRecognizer->getHeadDistanceFocal( 0.1481 );
			float x = ( float ) -mFaceRecognizer->getEyesCoords().x * ( mCapVideo->getWidth()/200 ) * ( distance / focalLength );
			float y = ( float ) -mFaceRecognizer->getEyesCoords().y * ( mCapVideo->getHeight()/200 ) * ( distance / focalLength );
			emit sendEyesRealCoords( x, y, distance );
		}
		msleep( 80 );
	}
	mCapVideo->release();
	mCapVideo = NULL;
	delete mFaceRecognizer;
}
// in a loop, capture frame from camera and detect faces
// send eyes coordinates to change view
void QOpenCV::FaceRecognitionThread::run()
{
	mCancel = false;
	cv::Mat image;
	OpenCV::FaceRecognizer*	mFaceRecognizer = new OpenCV::FaceRecognizer();

	if ( mCapVideo == NULL ) {
		qDebug() << "[FaceRecognitionThread::run()]  Camera is not set";
		return;
	}

	while ( !mCancel ) {
		// get image from camera
		image = mCapVideo->queryFrame();

		cv::cvtColor( image, image, CV_BGR2RGB );

		// we detect faces on grayscale image
		mFaceRecognizer->detectFaces( mCapVideo->getGrayframe() );
		mFaceRecognizer->annotateFaces( image );

		cv::flip( image, image, 1 );

		// show image
		if ( mSendImgEnabled && !image.empty() ) {
			if ( image.data ) {
				emit pushImage( image.clone() );    // ???
			}
		}
		if ( mSendBackgrImgEnabled && !image.empty() ) {
			emit pushBackgrImage( image.clone() );
		}

		// when face was detected along with movement (implemented with threshold)
		// send eyes coordinate to change view
		if ( mFaceRecognizer->detected ) { //&& mFaceRecognizer->isMovement
			emit sendEyesCoords( static_cast<float>( -mFaceRecognizer->getEyesCoords().x ),
								 static_cast<float>( -mFaceRecognizer->getEyesCoords().y ),
								 -mFaceRecognizer->getHeadDistance( mCapVideo->getWidth() ) );
		}
		msleep( 80 );
	}
	mCapVideo->release();
	mCapVideo = NULL;
	delete mFaceRecognizer;
}