📄 haarface.cpp
字号:
// HaarFace.cpp - by Robin Hewitt, 2006
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for terms of use.
//
#define USE_OPENCVINTERFACE
//////////////////////////////////////////////////////////////
// Implementation of HaarFace and HaarFaceSighting classes
//
// This class wraps the OpenCV Haar detector so the results
// can be returned within the framework Mavis uses
#include <string>
#include "HaarFace.h"
#include "Mavis.h"
#include <memory.h>
//#include <math.h>
#include "../Params.h"
#include "../Logger.h"
#include "../mavistypes.h"
//////////////////////////////////////////////////////////////
// HaarFaceSighting class
//
HaarFaceSighting::HaarFaceSighting()
{
}
HaarFaceSighting::~HaarFaceSighting()
{
}
//////////////////////////////////////////////////////////////
// HaarFace class
//
//////////////////
// constructor
//
HaarFace::HaarFace(Mavis * pM) : MVObjBase(pM), pCascade(0), pStorage(0),
pFaceRectSeq(0), iSeq(0)
{
Params * pParams = pMavis->getParams();
string haarDir, haarFile, cascadePath;
const char * tmp;
// load the path for the Haar Cascade directory
tmp = pParams->getStringValue("haarFace", "haarCascadeDir");
if( tmp )
haarDir = tmp;
else
haarDir = "./data"; // default value
// load the file name for the Haar cascade
tmp = pParams->getStringValue("haarFace", "haarCascade");
if( tmp )
haarFile = tmp;
else
haarFile = "haarcascade_frontalface_default.xml"; // default value
MVUtils::makeFullPath(haarDir, haarFile, cascadePath);
pCascade = (CvHaarClassifierCascade *)cvLoad(cascadePath.c_str(), 0, 0, 0);
pStorage = cvCreateMemStorage(0);
if( !pCascade )
pMavis->getLogger()->writelnToLog(
"HaarFace Detector:\n"
"\tFailed to load Haar Cascade %s", cascadePath.c_str());
if( !pStorage )
pMavis->getLogger()->writelnToLog(
"HaarFace Detector: failed to allocate Storage");
if(pCascade && pStorage)
pMavis->getLogger()->writelnToLog("HaarFace Detector initialized");
}
//////////////////
// destructor
//
HaarFace::~HaarFace()
{
if(pCascade) cvReleaseHaarClassifierCascade( &pCascade );
}
//////////////////
// lookOnce()
//
int HaarFace::lookOnce(ObjLoc_t * pObjLoc)
{
// initialize object location to "not found" state
memset(pObjLoc, 0, sizeof(ObjLoc_t));
// get next frame
VideoFrame * pFrame = pMavis->getNextFrame();
// validate that detector is initialized
if( !pCascade || !pStorage)
{
pMavis->getLogger()->writelnToLog("HaarFace Detector:\n\t"
"Refusing to detect faces because initialization failed");
return -1;
}
// convert it to an IplImage
IplImage * pImg = OpenCVInterface::VideoFrameToIplImage(*pFrame, true);
// detect faces in image
int maxSize = pFrame->getWidth() / 5;
pFaceRectSeq = cvHaarDetectObjects
(pImg, pCascade, pStorage,
1.1, // scale-increase factor
3, // min_neighbors
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(maxSize,maxSize)); // smallest size face to detect
// take first one only (for the first cut)
// (but add instance variables to track how many have been
// retrieved, and add an interface for retrieving the rest)
iSeq = 0;
if( pFaceRectSeq && pFaceRectSeq->total )
{
CvRect * rect = (CvRect *)cvGetSeqElem(pFaceRectSeq, iSeq++);
// convert CVRect to ROI (add this to OpenCVInterface namespace
ImgROI_t roi = OpenCVInterface::CvRectToRoi(pImg, rect);
// fill in Obj_Loc_t data
pObjLoc->prob = 100;
setImageLocation(pObjLoc, roi, pMavis);
pObjLoc->nFound = pFaceRectSeq->total;
pObjLoc->nRemaining = pFaceRectSeq->total-1;
// draw remaining ROIs
for(int i=1; i<pFaceRectSeq->total; i++)
{
rect = (CvRect *)cvGetSeqElem(pFaceRectSeq, i);
ImgROI_t roi = OpenCVInterface::CvRectToRoi(pImg, rect);
MVImgUtils::Vis::ellipse(*pFrame, roi, 0x999999);
}
// draw initial ROI
MVImgUtils::Vis::ellipse(*pFrame, roi, 0xff0000);
}
cvReleaseImage( &pImg );
return 0;
}
//////////////////////////
// getNext()
//
int HaarFace::getNext(ObjLoc_t * pObjLoc)
{
memset(pObjLoc, 0, sizeof(ObjLoc_t));
// get next frame
VideoFrame * pFrame = pMavis->getNextFrame();
if( pFaceRectSeq && (iSeq < pFaceRectSeq->total-1) )
{
CvRect * rect = (CvRect *)cvGetSeqElem(pFaceRectSeq, iSeq++);
// convert CVRect to ROI (add this to OpenCVInterface namespace
ImgROI_t roi = OpenCVInterface::CvRectToRoi(pFrame->getHeight(), rect);
// fill in Obj_Loc_t data
pObjLoc->prob = 100;
setImageLocation(pObjLoc, roi, pMavis);
pObjLoc->nFound = pFaceRectSeq->total;
// draw ROI
MVImgUtils::Vis::ellipse(*pFrame, roi, 0xff0000);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
// Mavis License Agreement
//
// Copyright (c) 2004-2006, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage.
///////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -