// viola-jonescpu-gpu.cpp : Defines the entry point for the console application. //
include "stdafx.h"
include <stdio.h>
include <stdlib.h>
include < opencv2\opencv.hpp>
include < opencv2\gpu\gpu.hpp>
include <opencv2\core\core.hpp>
include <opencv2\highgui\highgui.hpp>
include <opencv2\imgproc\imgproc.hpp>
include <iostream>
include < vector>
using namespace cv; using namespace std;
ifdef _DEBUG
pragma comment(lib, "opencv_core2411d.lib") //Compiler Directive
pragma comment(lib, "opencv_imgproc2411d.lib") //MAT processing
pragma comment(lib, "opencv_objdetect2411d.lib") //HOGDescriptor
pragma comment(lib, "opencv_gpu2411d.lib")
pragma comment(lib, "opencv_highgui2411d.lib")
else
pragma comment(lib, "opencv_core2411.lib")
pragma comment(lib, "opencv_imgproc2411.lib")
pragma comment(lib, "opencv_objdetect2411.lib")
pragma comment(lib, "opencv_gpu2411.lib")
pragma comment(lib, "opencv_highgui2411.lib")
endif
CvMemStorage *storage;
void main()
{
// For Time Measure
float TakeTime;
unsigned long Atime, Btime;
// Window
namedWindow("Face Detection");
// Load Image
cv::Mat img = imread("GT.jpg");
Mat grayImg; // adaboost detection is gray input only.
cv::cvtColor(img, grayImg, CV_BGR2GRAY); // To convert color image to gray
// Load xml file
string trainface = "MyCascade/haarcascade_frontalface_alt2.xml";
string trainface1 = "MyCascade/lbpcascade_profileface.xml";
// Defining a cascade
CascadeClassifier ada_cpu;
gpu::CascadeClassifier_GPU ada_gpu;
// Checking for proper initialization
if (!(ada_cpu.load(trainface)) && !(ada_cpu.load(trainface1)))
{
printf(" cpu ada xml load fail! \n");
return;
}
if (!(ada_gpu.load(trainface)) && !(ada_gpu.load(trainface1)))
{
printf(" gpu ada xml load fail! \n");
return;
}
// Creating Buffer
storage = cvCreateMemStorage(0);// dynamic memory storage; 0 is the default initial memory block size
// (1) CPU case face detection code
vector< Rect > faces;
Atime = getTickCount();
ada_cpu.detectMultiScale(grayImg, faces);
Btime = getTickCount();
TakeTime = (Btime - Atime) / getTickFrequency();
printf("detected face(cpu version) = %d / %lf sec take.\n", faces.size(), TakeTime);
if (faces.size() >= 1)
{
for (int ji = 0; ji < faces.size(); ++ji)
{
rectangle(img, faces[ji], CV_RGB(0, 0, 255), 4);
}
}
// (2) GPU case face detection code
gpu::GpuMat faceBuf_gpu;
gpu::GpuMat GpuImg;
GpuImg.upload(grayImg);
Atime = getTickCount();
int detectionNumber = ada_gpu.detectMultiScale(GpuImg, faceBuf_gpu);
Btime = getTickCount();
TakeTime = (Btime - Atime) / getTickFrequency();
printf("detected face(gpu version) =%d / %lf sec take.\n", detectionNumber, TakeTime);
Mat faces_downloaded;
if (detectionNumber >= 1)
{
faceBuf_gpu.colRange(0, detectionNumber).download(faces_downloaded);
Rect* faces = faces_downloaded.ptr< Rect>();
for (int ji = 0; ji < detectionNumber; ++ji)
{
rectangle(img, Point(faces[ji].x, faces[ji].y), Point(faces[ji].x + faces[ji].width, faces[ji].y + faces[ji].height), CV_RGB(255, 0, 0), 2);
}
}
//result display
imshow("Face Detection", img);
waitKey(0);
//return 0;
}