Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 125bfb5
Showing
12 changed files
with
1,207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
#include <io.h> | ||
#include <direct.h> | ||
#include <string> | ||
#include <iomanip> | ||
#include <Windows.h> | ||
#include <AtlBase.h> | ||
#include <opencv2/opencv.hpp> | ||
|
||
#include "stdafx.h" | ||
//https://github.com/ShiqiYu/libfacedetection | ||
#include "facedetect-dll.h" | ||
//#pragma comment(lib,"libfacedetect.lib") | ||
|
||
|
||
using namespace cv; | ||
using namespace std; | ||
|
||
void getFiles( string path, vector<pair<string,int> >& files ) { | ||
//文件句柄 | ||
long hFile = 0; | ||
//文件信息 | ||
struct _finddata_t fileinfo; | ||
|
||
string p; | ||
|
||
static int auto_label = 1; | ||
int label=0; | ||
p.clear(); | ||
FILE *fp = fopen(p.assign(path).append("/label.txt").c_str(),"r"); | ||
if(fp==NULL) | ||
{ | ||
label = auto_label++; | ||
cout<<path<<" label:"<<label<<endl; | ||
} | ||
else | ||
{ | ||
fscanf(fp,"%d",&label); | ||
if(auto_label<label) auto_label = label + 1; | ||
fclose(fp); | ||
} | ||
|
||
|
||
if ((hFile = _findfirst(p.assign(path).append("/*").c_str(),&fileinfo)) != -1) { | ||
|
||
do { | ||
//如果是目录,迭代之 | ||
//如果不是,加入列表 | ||
if ((fileinfo.attrib & _A_SUBDIR)) { | ||
if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) | ||
getFiles( p.assign(path).append("/").append(fileinfo.name), files ); | ||
} else { | ||
if(strstr(fileinfo.name,"png")!=NULL||strstr(fileinfo.name,"bmp")!=NULL||strstr(fileinfo.name,"jpg")!=NULL||strstr(fileinfo.name,"tif")!=NULL) | ||
files.push_back( make_pair(p.assign(path).append("\\").append(fileinfo.name),label) ); | ||
} | ||
} while (_findnext( hFile, &fileinfo ) == 0); | ||
|
||
_findclose(hFile); | ||
} | ||
} | ||
//static void getFiles(LPCTSTR path, vector<pair<LPCTSTR,int> > &filesPathVector) | ||
//{ | ||
// struct _tfinddata64_t c_file; | ||
// intptr_t hFile; | ||
// TCHAR root[MAX_PATH]; | ||
// string p; | ||
// hFile=_tfindfirst64(p.assign(path).append("/*.*").c_str(),&c_file); | ||
// if( hFile == -1) | ||
// return; | ||
// static int auto_label = 1; | ||
// int label=0; | ||
// p.clear(); | ||
// FILE *fp = fopen(p.assign(path).append("/label.txt").c_str(),"r"); | ||
// if(fp==NULL) | ||
// { | ||
// label = auto_label++; | ||
// cout<<path<<" label:"<<label<<endl; | ||
// } | ||
// else | ||
// { | ||
// fscanf(fp,"%d",&label); | ||
// if(auto_label<label) auto_label = label + 1; | ||
// } | ||
// | ||
// //search all files recursively. | ||
// do | ||
// { | ||
// if(_tcslen(c_file.name)==1&&c_file.name[0]==_T('.') | ||
// ||_tcslen(c_file.name)==2&&c_file.name[0]==_T('.')&&c_file.name[1]==_T('.')) | ||
// continue; | ||
// TCHAR *fullPath =new TCHAR[MAX_PATH]; | ||
// _tcscpy(fullPath,path); | ||
// _tcscat(fullPath,_T("\\")); | ||
// _tcscat(fullPath,c_file.name); | ||
// if(c_file.attrib&_A_SUBDIR) | ||
// { | ||
// getFiles(fullPath,filesPathVector); | ||
// } | ||
// else | ||
// { | ||
// if(strstr(c_file.name,"png")!=NULL||strstr(c_file.name,"bmp")!=NULL||strstr(c_file.name,"JPG")!=NULL | ||
// ||strstr(c_file.name,"jpg")!=NULL||strstr(c_file.name,"tif")!=NULL) | ||
// //if(strstr(c_file.name,"yml")!=NULL) | ||
// filesPathVector.push_back(make_pair(fullPath,label)); | ||
// } | ||
// } | ||
// while( _tfindnext64( hFile, &c_file ) == 0); | ||
// //close search handle | ||
// _findclose(hFile); | ||
//} | ||
|
||
int _tmain(int argc, char* argv[]) | ||
{ | ||
char * filename = new char[100]; | ||
char folder[]="G:\\CASIA-WebFace"; | ||
vector<pair<string, int> > files; | ||
string p; | ||
FILE *fp = fopen(p.assign(folder).append("/list.txt").c_str(),"r"); | ||
if(fp==NULL) | ||
{ | ||
fp = fopen(p.assign(folder).append("/list.txt").c_str(),"w"); | ||
getFiles(folder,files); | ||
int fileSize=files.size(); | ||
for(int f=0;f<fileSize;f++){ | ||
fprintf(fp,"%s %d\n",files[f].first.c_str(),files[f].second); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
char imgpath[256]; | ||
int label; | ||
while(!feof(fp)) | ||
{ | ||
fscanf(fp,"%s %d",imgpath,&label); | ||
files.push_back( make_pair(imgpath,label) ); | ||
} | ||
} | ||
fclose(fp); | ||
int fileSize=files.size(); | ||
int * pResults = NULL; | ||
|
||
|
||
for(int f=0;f<fileSize;f++){ | ||
Mat gray = imread(files[f].first,0); | ||
imshow("original",gray); | ||
|
||
///////////////////////////////////////////// | ||
//// frontal face detection | ||
//// it's fast, but cannot detect side view faces | ||
//////////////////////////////////////////// | ||
////!!! The input image must be a gray one (single-channel) | ||
////!!! DO NOT RELEASE pResults !!! | ||
//pResults = facedetect_frontal((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step, | ||
//1.2f, 2, 24); | ||
//printf("%d frontal faces detected.\n", (pResults ? *pResults : 0)); | ||
////print the detection results | ||
//for(int i = 0; i < (pResults ? *pResults : 0); i++) | ||
//{ | ||
// short * p = ((short*)(pResults+1))+6*i; | ||
// int x = p[0]; | ||
// int y = p[1]; | ||
// int w = p[2]; | ||
// int h = p[3]; | ||
// int neighbors = p[4]; | ||
// imshow("face",gray(Rect(x,y,w,h))); | ||
// printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x,y,w,h,neighbors); | ||
//} | ||
|
||
/////////////////////////////////////////// | ||
// multiview face detection | ||
// it can detection side view faces, but slower than the frontal face detection. | ||
////////////////////////////////////////// | ||
//!!! The input image must be a gray one (single-channel) | ||
//!!! DO NOT RELEASE pResults !!! | ||
pResults = facedetect_multiview((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step, | ||
1.2f, 4, 24); | ||
printf("%d faces detected.\n", (pResults ? *pResults : 0)); | ||
|
||
//print the detection results | ||
for(int i = 0; i < (pResults ? *pResults : 0); i++) | ||
{ | ||
short * p = ((short*)(pResults+1))+6*i; | ||
int x = p[0]; | ||
int y = p[1]; | ||
int w = p[2]; | ||
int h = p[3]; | ||
int neighbors = p[4]; | ||
int angle = p[5]; | ||
imshow("face",gray(Rect(x,y,w,h))); | ||
printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle); | ||
//waitKey(1); | ||
} | ||
if(pResults==NULL) | ||
{ | ||
waitKey(0); | ||
} | ||
else | ||
{ | ||
waitKey(0); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
function [mappedX, mapping] = JointBayesian(X, labels) | ||
%LDA Perform the LDA algorithm | ||
% | ||
% [mappedX, mapping] = lda(X, labels, no_dims) | ||
% | ||
% The function runs LDA on a set of datapoints X. The variable | ||
% no_dims sets the number of dimensions of the feature points in the | ||
% embedded feature space (no_dims >= 1, default = 2). The maximum number | ||
% for no_dims is the number of classes in your data minus 1. | ||
% The function returns the coordinates of the low-dimensional data in | ||
% mappedX. Furthermore, it returns information on the mapping in mapping. | ||
% | ||
% | ||
|
||
% This file is part of the Matlab Toolbox for Dimensionality Reduction. | ||
% The toolbox can be obtained from http://homepage.tudelft.nl/19j49 | ||
% You are free to use, change, or redistribute this code in any way you | ||
% want for non-commercial purposes. However, it is appreciated if you | ||
% maintain the name of the original author. | ||
% | ||
% (C) Laurens van der Maaten, Delft University of Technology | ||
% Make sure data is zero mean | ||
% mapping.mean = mean(X, 1); | ||
% [COEFF,SCORE] = princomp(X,'econ'); | ||
% X = SCORE(:,1:400); | ||
% X = bsxfun(@minus,X,mapping.mean); | ||
m = length(labels); | ||
n = size(X,2); | ||
|
||
% Make sure labels are nice | ||
[classes, bar, labels] = unique(labels); | ||
nc = length(classes); | ||
|
||
% Intialize Sw | ||
Sw = zeros(size(X, 2), size(X, 2)); | ||
|
||
cur = {}; | ||
withinCount = 0; | ||
numberBuff = zeros(1000,1); | ||
% numberInvert = zeros(1000,1); | ||
maxNumberInOneClass = []; | ||
for i=1:nc | ||
% Get all instances with class i | ||
cur{i} = X(labels == i,:); | ||
if size(cur{i},1)>1 | ||
withinCount = withinCount + size(cur{i},1); | ||
end; | ||
if numberBuff(size(cur{i},1)) ==0 | ||
numberBuff(size(cur{i},1)) = 1; | ||
maxNumberInOneClass = [maxNumberInOneClass;size(cur{i},1)]; | ||
end; | ||
end; | ||
disp([nc withinCount]); | ||
fprintf('prepare done, maxNumberInOneClass=%d.\n',length(maxNumberInOneClass)); | ||
tic; | ||
u = zeros(n,nc); | ||
ep = zeros(n,withinCount); | ||
nowp = 1; | ||
% Sum over classes | ||
for i=1:nc | ||
% Update within-class scatter | ||
u(:,i) = mean(cur{i},1)'; | ||
|
||
if size(cur{i},1)>1 | ||
ep(:,nowp:nowp+ size(cur{i}, 1)-1) = bsxfun(@minus,cur{i}',u(:,i)); | ||
nowp = nowp + size(cur{i}, 1); | ||
% C = cov(cur{i}); | ||
% p = size(cur{i}, 1) / withinCount; | ||
% Sw = Sw + (p * C); | ||
end; | ||
end; | ||
Su = cov(u'); | ||
Sw = cov(ep'); | ||
|
||
% Su = u*u'/nc; | ||
% Sw = ep*ep'/withinCount; | ||
% Su = cov(rand(n,n)); | ||
% Sw = cov(rand(5*n,n)); | ||
fprintf('LDA matrix done.\n'); | ||
toc; | ||
% F = inv(Sw); | ||
% mapping.Su = Su; | ||
% mapping.Sw = Sw; | ||
% mapping.G = -1 .* (2 * Su + Sw) \ Su / Sw; | ||
% mapping.A = inv(Su + Sw) - (F + mapping.G); | ||
% mappedX = X; | ||
% end | ||
|
||
oldSw = Sw; | ||
% Gs = cell(maxNumberInOneClass,1); | ||
SuFG = cell(1000,1); | ||
SwG = cell(1000,1); | ||
|
||
for l=1:500 | ||
% tic; | ||
F = inv(Sw); | ||
ep =zeros(n,m); | ||
nowp = 1; | ||
for g = 1:1000 | ||
if numberBuff(g)==1 | ||
G = -1 .* (g .* Su + Sw) \ Su / Sw; | ||
SuFG{g} = Su * (F + g.*G); | ||
SwG{g} = Sw*G; | ||
end; | ||
end; | ||
for i=1:nc | ||
nnc = size(cur{i}, 1); | ||
% G = Gs{nnc}; | ||
u(:,i) = sum(SuFG{nnc} * cur{i}',2); | ||
ep(:,nowp:nowp+ size(cur{i}, 1)-1) = bsxfun(@plus,cur{i}',sum(SwG{nnc}*cur{i}',2)); | ||
nowp = nowp+ nnc; | ||
end; | ||
Su = cov(u'); | ||
Sw = cov(ep'); | ||
% Su = u*u'/nc; | ||
% Sw = ep*ep'/withinCount; | ||
fprintf('%d %f\n',l,norm(Sw - oldSw)/norm(Sw)); | ||
% toc; | ||
if norm(Sw - oldSw)/norm(Sw)<1e-6 | ||
break; | ||
end; | ||
oldSw = Sw; | ||
end; | ||
F = inv(Sw); | ||
mapping.G = -1 .* (2 * Su + Sw) \ Su / Sw; | ||
mapping.A = inv(Su + Sw) - (F + mapping.G); | ||
mapping.Sw = Sw; | ||
mapping.Su = Su; | ||
% mapping.U = chol(-G,'upper'); | ||
% mapping.COEFF = COEFF; | ||
% mapping.y = mapping.U * X'; | ||
mapping.c = zeros(m,1); | ||
for i = 1:m | ||
mapping.c(i) = X(i,:) * mapping.A * X(i,:)'; | ||
end; | ||
mappedX = X; | ||
|
Oops, something went wrong.