Исследование и разработка алгоритмов распознавания лиц

Обзор существующих алгоритмов для обнаружения лиц. Выравнивание лица с помощью разнообразных фильтров. Использование каскадного классификатора Хаара для поиска лиц на изображении. Распознавание лиц людей с использованием локальных бинарных шаблонов.

Рубрика Программирование, компьютеры и кибернетика
Вид дипломная работа
Язык русский
Дата добавления 30.09.2016
Размер файла 332,4 K

Отправить свою хорошую работу в базу знаний просто. Используйте форму, расположенную ниже

Студенты, аспиранты, молодые ученые, использующие базу знаний в своей учебе и работе, будут вам очень благодарны.

23.77425

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

34.00935

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

35.81765

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

35.927803

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

35.96275

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

36.15985

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

36.2037

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

36.24445

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

37.15775

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

37.72155

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

38.0915

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

38.5365

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

39.11535

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

40.1183

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

40.83395

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

41.35815

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

41.78735

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

43.3433

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

44.0983

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

44.43485

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

44.441948

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

44.454002

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

44.59

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

44.612198

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

44.6786

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

44.779503

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

44.849403

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

45.0011

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

45.02565

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

45.1193

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

46.9468

2

1

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

47.414352

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

48.490402

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

48.67815

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

49.0149

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister3.jpg

50.16235

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

51.45775

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

51.60845

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister4.jpg

51.834396

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

55.97485

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister1.jpg

56.7955

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

61.69295

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

63.181152

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

70.920105

2

3

/home/mihail/diploma/aberdeen_shortened/2/alison3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

73.107

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian4.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

75.7419

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian1.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

90.403595

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian2.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

93.04085

1

3

/home/mihail/diploma/aberdeen_shortened/1/adrian3.jpg

/home/mihail/diploma/aberdeen_shortened/3/alister2.jpg

102.02286

Приложение 3. Листинг

face_recognizer.cpp

#include "opencv2/core/core.hpp"

#include "opencv2/contrib/contrib.hpp"

#include "opencv2/opencv.hpp"

#include <cctype>

#include <iostream>

#include <iterator>

#include <stdio.h>

#include <dirent.h>

#include <unistd.h>

#include <fstream>

#include <cerrno>

#include <sstream>

#include <math.h>

#include <algorithm> // std::sort

using namespace cv;

using namespace std;

string cascadeName = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml";

Rect detectAndCut(Mat &img, CascadeClassifier& cascade, bool normalize = true)

{

vector<Rect> faces;

if (normalize) {

cvtColor(img, img, COLOR_BGR2GRAY);

resize(img, img, img.size(), 0, 0, INTER_LINEAR);

equalizeHist(img, img);

}

cascade.detectMultiScale(img, faces,

1.1, 2, 0

|CASCADE_FIND_BIGGEST_OBJECT

//|CASCADE_DO_ROUGH_SEARCH

|CASCADE_SCALE_IMAGE

,

Size(30, 30));

for(vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++)

{

return *r;

}

throw "No faces were found";

}

static void fill_vectors(Mat& etalon_image, Mat& candidate_image, vector<Mat>& images, vector<int>& labels) {

images.push_back(etalon_image);

labels.push_back(0);

images.push_back(candidate_image);

labels.push_back(0);

}

struct Sort {

bool operator() (Rect i, Rect j) {

return (i.height > j.height);

}

} mySort;

vector<Rect> getTheBiggestEyes(vector<Rect> eyes) {

if (eyes.size() <= 2) {

return eyes;

}

vector<Rect> realEyes;

sort(eyes.begin(), eyes.end(), mySort);

for (int i = 0; i < 2; i++) {

realEyes.push_back(eyes.at(i));

}

return realEyes;

}

Mat alignByEyes(Mat image, Rect face) {

// preparation

Mat smallImgROI = image(face);

const char *cascade_eye = "/usr/share/opencv/haarcascades/haarcascade_eye.xml";

CascadeClassifier eyesClassifier;

vector<Rect> eyes;

if(!eyesClassifier.load(cascade_eye))

{

cerr << "ERROR: Could not load classifier cascade" << endl;

exit(-1);

}

eyesClassifier.detectMultiScale(smallImgROI, eyes,

1.2, 3, 0

//|CASCADE_FIND_BIGGEST_OBJECT

//|CASCADE_DO_ROUGH_SEARCH

//|CASCADE_SCALE_IMAGE

,

Size(15, 15));

eyes = getTheBiggestEyes(eyes);

// for(vector<Rect>::const_iterator r = eyes.begin(); r != eyes.end(); r++) {

// CvPoint p1 = { face.x + r->x, face.y + r->y };

// CvPoint p2 = { face.x + r->x + r->width, face.y + r->y + r->height };

// rectangle(image, p1, p2, CV_RGB(0, 255, 0), 1, 4, 0);

// printf(" %d %d %d %d\n", face.x + r->x, face.y + r->y, r->width, r->height);

// }

if (eyes.size() == 2) {

int xHalfEye[2], yHalfEye[2];

int i = 0;

for(vector<Rect>::const_iterator r = eyes.begin(); r != eyes.end(); r++, i++) {

xHalfEye[i] = face.x + r->x + r->width / 2;

yHalfEye[i] = face.y + r->y + r->height / 2;

}

// rotation

double rotateAngle = atan((double)(yHalfEye[0] - yHalfEye[1]) / (double)(xHalfEye[0] - xHalfEye[1]))

* 180 / M_PI;

Point2f pc(image.cols/2., image.rows/2.);

Mat r = getRotationMatrix2D(pc, rotateAngle, 1.0);

warpAffine(image, image, r, image.size());

CascadeClassifier cascade;

if(!cascade.load(cascadeName)) {

cerr << "ERROR: Could not load classifier cascade" << endl;

exit(-1);

}

try {

face = detectAndCut(image, cascade, false);

} catch (char const* msg) {

cout << msg << endl;

exit(-1);

}

smallImgROI = image(face);

eyesClassifier.detectMultiScale(smallImgROI, eyes,

1.2, 3, 0

//|CASCADE_FIND_BIGGEST_OBJECT

//|CASCADE_DO_ROUGH_SEARCH

//|CASCADE_SCALE_IMAGE

,

Size(15, 15));

eyes = getTheBiggestEyes(eyes);

// for(vector<Rect>::const_iterator r = eyes.begin(); r != eyes.end(); r++) {

// CvPoint p1 = { face.x + r->x, face.y + r->y };

// CvPoint p2 = { face.x + r->x + r->width, face.y + r->y + r->height };

// rectangle(image, p1, p2, CV_RGB(0, 255, 0), 1, 4, 0);

// printf(" %d %d %d %d\n", r->x, r->y, r->width, r->height);

// }

if (eyes.size() == 2) {

int xHalfEye[2], yHalfEye[2];

int i = 0;

for(vector<Rect>::const_iterator r = eyes.begin(); r != eyes.end(); r++, i++) {

xHalfEye[i] = face.x + r->x + r->width / 2;

yHalfEye[i] = face.y + r->y + r->height / 2;

}

// cropping

int currentBetweenPupilsDistance = abs(xHalfEye[0] - xHalfEye[1]);

int currentLeftEyeX = xHalfEye[0] < xHalfEye[1] ? xHalfEye[0] : xHalfEye[1];

Rect myROI;

myROI.x = currentLeftEyeX - 0.89 * currentBetweenPupilsDistance;

myROI.width = 2.78 * currentBetweenPupilsDistance;

myROI.y = yHalfEye[0] - 0.39 * myROI.width;

myROI.height = myROI.width;

image = image(myROI);

//resize(image, image, Size(200, 200));

}

}

return image;

}

int main(int argc, const char *argv[]) {

if (argc != 3) {

cout << "usage: " << argv[0] << " <etalon_path> <candidate_path>" << endl;

exit(1);

}

string etalon = string(argv[1]);

string candidate = string(argv[2]);

CascadeClassifier cascade;

if(!cascade.load(cascadeName))

{

cerr << "ERROR: Could not load classifier cascade" << endl;

return -1;

}

Mat etalon_image, candidate_image;

Rect etalonFace, candidateFace;

etalon_image = imread(etalon, 1);

try {

etalonFace = detectAndCut(etalon_image, cascade);

} catch (char const* msg) {

cout << msg << endl;

return -1;

}

etalon_image = alignByEyes(etalon_image, etalonFace);

candidate_image = imread(candidate, 1);

try {

candidateFace = detectAndCut(candidate_image, cascade);

} catch (char const* msg) {

cout << msg << endl;

return -1;

}

candidate_image = alignByEyes(candidate_image, candidateFace);

int medianBlurFilterIntensity = 3;

int gaussianBlurFilterIntensity = medianBlurFilterIntensity;

medianBlur(etalon_image, etalon_image, medianBlurFilterIntensity);

medianBlur(candidate_image, candidate_image, medianBlurFilterIntensity);

GaussianBlur(etalon_image, etalon_image, Size(gaussianBlurFilterIntensity, gaussianBlurFilterIntensity), 0);

GaussianBlur(candidate_image, candidate_image, Size(gaussianBlurFilterIntensity, gaussianBlurFilterIntensity), 0);

// namedWindow("named_window");

// imshow("named_window", etalon_image);

// waitKey();

// imshow("named_window", candidate_image);

// waitKey();

vector<Mat> images;

vector<int> labels;

fill_vectors(etalon_image, candidate_image, images, labels);

if(images.size() <= 1) {

string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";

CV_Error(CV_StsError, error_message);

}

Mat testSample = images[images.size() - 1];

images.pop_back();

labels.pop_back();

Ptr<FaceRecognizer> model = createLBPHFaceRecognizer(2, 8, 6, 6); // try call with params: 2, 8, 6, 6

model->train(images, labels);

int predictedLabel = model->predict(testSample);

double confidence = 0.0;

model->predict(testSample, predictedLabel, confidence);

cout << confidence << endl;

return 0;

}

Calculator.java

import java.util.ArrayList;

public class Calculator {

private float xAverageTheirs, standardDeviationTheirs, xAverageStrangers, standardDeviationStrangers;

private ArrayList<ArrayList<String>> source;

public Calculator(ArrayList<ArrayList<String>> source) {

this.source = source;

}

public void calculate(int group) {

float sumTheirs = 0, sumStrangers = 0;

int quantityTheirs = 0, quantityStrangers = 0;

for (ArrayList<String> row : source) {

if (row.get(0).equals(row.get(1))) {

sumTheirs += Float.parseFloat(row.get(group));

quantityTheirs++;

} else {

sumStrangers += Float.parseFloat(row.get(group));

quantityStrangers++;

}

}

xAverageTheirs = sumTheirs / quantityTheirs;

xAverageStrangers = sumStrangers / quantityStrangers;

float numeratorTheirs = 0, numeratorStrangers = 0;

for (ArrayList<String> row : source) {

if (row.get(0).equals(row.get(1))) {

float xMinusXAvg = Float.parseFloat(row.get(group)) - xAverageTheirs;

numeratorTheirs += xMinusXAvg * xMinusXAvg;

} else {

float xMinusXAvg = Float.parseFloat(row.get(group)) - xAverageStrangers;

numeratorStrangers += xMinusXAvg * xMinusXAvg;

}

}

standardDeviationTheirs = (float)Math.sqrt(numeratorTheirs / quantityTheirs);

standardDeviationStrangers = (float)Math.sqrt(numeratorStrangers / quantityStrangers);

}

public float getXAverageTheirs() {

return xAverageTheirs;

}

public float getStandardDeviationTheirs() {

return standardDeviationTheirs;

}

public float getXAverageStrangers() {

return xAverageStrangers;

}

public float getStandardDeviationStrangers() {

return standardDeviationStrangers;

}

}

Client.java

public class Client

{

public static String interimStatisticsFilePath = null;

public static String folderWithPhotos = null;

public static String executableFilename = null;

public static void main (String args[])

{

interimStatisticsFilePath = args[0];

folderWithPhotos = args[1];

executableFilename = args[2];

Recognizer r = new Recognizer();

r.recognize();

// r.recognizeShuffle();

Statistics s = new Statistics();

s.gather();

}

}

IMatcher.java

public interface IMatcher

{

final static float NO_FACE_ETALON = -1;

final static float NO_FACE_CANDIDATE = -2;

final static float INTERNAL_ERROR = -3;

final static float NO_SUCH_FILE = -4;

public float compare(String etalon, String candidate) throws Exception;

}

OpenCVMatcher.java

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class OpenCVMatcher implements IMatcher

{

private String etalon, candidate;

private float extractAndMatch() throws Exception {

Process p;

String command = null;

String result = null;

try {

command = Client.executableFilename + " " + etalon + " " + candidate;

p = Runtime.getRuntime().exec(command);

String line = "";

BufferedReader errors = new BufferedReader(new InputStreamReader(p.getErrorStream()));

BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = output.readLine()) != null) {

result = line;

}

while ((errors.readLine()) != null) {}

p.waitFor();

} catch (Exception e) {

throw e;

}

try {

return Float.parseFloat(result);

} catch (NumberFormatException e) {

return -1;

}

}

public float compare(String etalon, String candidate) throws Exception

{

this.etalon = etalon;

this.candidate = candidate;

return extractAndMatch();

}

}

Parser.java

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Parser {

public static ArrayList<ArrayList<String>> parse(String filepath, int[] groups) throws Exception {

String line;

try (

InputStream fis = new FileInputStream(filepath);

InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));

BufferedReader br = new BufferedReader(isr);

) {

ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();

Pattern pattern = Pattern.compile("^(.+);(.+);(.+);(.+);(.+)$", Pattern.MULTILINE);

while ((line = br.readLine()) != null) {

Matcher m = null;

m = pattern.matcher(line);

m.find();

ArrayList<String> temp = new ArrayList<String>();

for(int group : groups) {

temp.add(m.group(group));

}

results.add(temp);

}

return results;

}

}

}

Percentager.java

public class Percentager

{

public static void main (String args[])

{

String etalonFilename = "/home/mihail/diploma/aberdeen_face_database/amellanby15.jpg";

String candidateFilename = "/home/mihail/diploma/aberdeen_face_database/andrew7.jpg";

Client.executableFilename = "/home/mihail/workspace/binary/face_recognizer";

IMatcher opencvMatcher = new OpenCVMatcher();

float resultOpenCV = 0, resultOpenCV1 = 0, resultOpenCV2 = 0;

try {

resultOpenCV1 = opencvMatcher.compare(etalonFilename, candidateFilename);

resultOpenCV2 = opencvMatcher.compare(candidateFilename, etalonFilename);

resultOpenCV = (resultOpenCV1 + resultOpenCV2) / 2;

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println(e.getMessage());

}

//System.out.println("Confidence: " + getPercentFromDistance(resultOpenCV) + "%");

System.out.println("These people are identical: " + getAnswerFromDistance(resultOpenCV));

}

private static String getAnswerFromDistance(float resultOpenCV) {

if (resultOpenCV < 20)

return "true";

else if (resultOpenCV >= 26)

return "false";

else

return "not sure";

}

private static int getPercentFromDistance(float resultOpenCV) {

//distance thresholds

int absolutelyTrueThreshold = 13;

int trueThreshold = 46;

int notSureThreshold = 177;

int falseThreshold = 501;

//percent thresholds

int absolutelyTruePercent = 99;

int truePercent = 70;

int notSurePercent = 30;

int falsePercent = 0;

//steps

double absolutelyTrueStep = 1d / absolutelyTrueThreshold;

double trueStep = 29d / (trueThreshold - absolutelyTrueThreshold);

double notSureStep = 40d / (notSureThreshold - trueThreshold);

double falseStep = 30d / (falseThreshold - notSureThreshold);

//logic

double confidencePercent = 0;

if (resultOpenCV > notSureThreshold) {

confidencePercent = falsePercent + ((falseThreshold - resultOpenCV) * falseStep);

} else if (resultOpenCV > trueThreshold) {

confidencePercent = notSurePercent + ((notSureThreshold - resultOpenCV) * notSureStep);

} else if (resultOpenCV > absolutelyTrueThreshold) {

confidencePercent = truePercent + ((trueThreshold - resultOpenCV) * trueStep);

} else {

confidencePercent = absolutelyTruePercent + ((absolutelyTrueThreshold - resultOpenCV) * absolutelyTrueStep);

}

return (int)confidencePercent;

}

}

PhotosContainer.java

public class PhotosContainer

{

private int label;

private String filename;

public PhotosContainer(int label, String filename)

{

this.label = label;

this.filename = filename;

}

public int getLabel()

{

return label;

}

public String getFilename()

{

return filename;

}

}

Recognizer.java

import java.io.File;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

public class Recognizer {

List<PhotosContainer> photoNames = new ArrayList<PhotosContainer>();

public void listFilesForFolder(final File folder) {

for (final File fileEntry : folder.listFiles()) {

if (fileEntry.isDirectory()) {

listFilesForFolder(fileEntry);

} else {

photoNames.add(new PhotosContainer(Integer.parseInt(folder.getName()),

Client.folderWithPhotos + '/' + folder.getName() + '/' + fileEntry.getName()));

}

}

}

public void recognizeShuffle() {

final File folder = new File(Client.folderWithPhotos);

try {

compare(folder);

} catch (Exception e) {

e.getMessage();

}

}

private void compare(File folder) throws Exception {

File[] pathsList = folder.listFiles();

Random rand = new Random();

for (int i = 0; i < 5; i++) {

File randFolder = pathsList[rand.nextInt(pathsList.length)];

File[] fileList = randFolder.listFiles();

File randEtalonFile = fileList[rand.nextInt(fileList.length)];

File randCandidateFile = fileList[rand.nextInt(fileList.length)];

while (randEtalonFile.getName().equals(randCandidateFile.getName())) {

randCandidateFile = fileList[rand.nextInt(fileList.length)];

}

IMatcher opencvMatcher = new OpenCVMatcher();

float resultOpenCV1 = opencvMatcher.compare(randEtalonFile.getAbsolutePath(), randCandidateFile.getAbsolutePath());

float resultOpenCV2 = opencvMatcher.compare(randCandidateFile.getAbsolutePath(), randEtalonFile.getAbsolutePath());

float resultOpenCV = (resultOpenCV1 + resultOpenCV2) / 2;

System.out.println(randEtalonFile.getAbsolutePath() + " and " + randCandidateFile.getAbsolutePath()

+ " are identical: " + getAnswerFromDistance(resultOpenCV) + " (distance = " + resultOpenCV + ")");

}

for (int i = 0; i < 5; i++) {

File randEtalonFolder = pathsList[rand.nextInt(pathsList.length)];

File[] fileList = randEtalonFolder.listFiles();

File randEtalonFile = fileList[rand.nextInt(fileList.length)];

File randCandidateFolder = pathsList[rand.nextInt(pathsList.length)];

fileList = randCandidateFolder.listFiles();

File randCandidateFile = fileList[rand.nextInt(fileList.length)];

while (randEtalonFile.getName().equals(randCandidateFile.getName())) {

randCandidateFile = fileList[rand.nextInt(fileList.length)];

}

IMatcher opencvMatcher = new OpenCVMatcher();

float resultOpenCV1 = opencvMatcher.compare(randEtalonFile.getAbsolutePath(), randCandidateFile.getAbsolutePath());

float resultOpenCV2 = opencvMatcher.compare(randCandidateFile.getAbsolutePath(), randEtalonFile.getAbsolutePath());

float resultOpenCV = (resultOpenCV1 + resultOpenCV2) / 2;

System.out.println(randEtalonFile.getAbsolutePath() + " and " + randCandidateFile.getAbsolutePath()

+ " are identical: " + getAnswerFromDistance(resultOpenCV) + " (distance = " + resultOpenCV + ")");

}

}

private static String getAnswerFromDistance(float resultOpenCV) {

if (resultOpenCV == -1) {

return "no faces were found";

} else if (resultOpenCV < 28)

return "true";

else if (resultOpenCV >= 28)

return "false";

else

return "not sure";

}

public void recognize() {

try (PrintWriter writer = new PrintWriter(Client.interimStatisticsFilePath, "UTF-8")) {

final File folder = new File(Client.folderWithPhotos);

listFilesForFolder(folder);

int count = (photoNames.size() * photoNames.size() - photoNames.size()) / 2;

int i = 0;

for (Iterator<PhotosContainer> iterator = photoNames.iterator(); iterator.hasNext();) {

PhotosContainer etalon = iterator.next();

for (PhotosContainer candidate : photoNames) {

if (!etalon.getFilename().equals(candidate.getFilename())) {

// System.out.println(etalon.getLabel() + ";" + candidate.getLabel() + ";" + etalon.getFilename() +

// ";" + candidate.getFilename());

IMatcher opencvMatcher = new OpenCVMatcher();

float resultOpenCV1 = opencvMatcher.compare(etalon.getFilename(), candidate.getFilename());

float resultOpenCV2 = opencvMatcher.compare(candidate.getFilename(), etalon.getFilename());

float resultOpenCV = (resultOpenCV1 + resultOpenCV2) / 2;

// IMatcher openbrMatcher = new OpenBRMatcher();

// float resultOpenBR = openbrMatcher.compare(etalon.getFilename(), candidate.getFilename());

// System.out.println(etalon.getLabel() + ";" + candidate.getLabel() + ";" + etalon.getFilename() +

// ";" + candidate.getFilename() + ";" + resultOpenCV);

writer.println(etalon.getLabel() + ";" + candidate.getLabel() + ";" + etalon.getFilename() +

";" + candidate.getFilename() + ";" + resultOpenCV);

i++; System.out.println(((float)i/count*100) + "%");

}

}

iterator.remove();

}

/* Exceptions processing */

} catch (Exception e) {

//some exception message

e.printStackTrace();

}

}

}

Statistics.java

import java.util.ArrayList;

public class Statistics {

public void gather() {

int []groupsToResult = {1, 2, 5};

ArrayList<ArrayList<String>> results = null;

try {

results = Parser.parse(Client.interimStatisticsFilePath, groupsToResult);

} catch (Exception e) {

System.out.println("Parser fucked up");

}

Calculator calculator = new Calculator(results);

calculator.calculate(2);

System.out.println(calculator.getXAverageTheirs() + " - " + calculator.getXAverageStrangers() + "; "

+ calculator.getStandardDeviationTheirs() + " - " + calculator.getStandardDeviationStrangers());

// calculator.calculate(3);

// System.out.println(calculator.getXAverageTheirs() + " - " + calculator.getXAverageStrangers() + "; "

// + calculator.getStandardDeviationTheirs() + " - " + calculator.getStandardDeviationStrangers());

// calculator.calculate(4);

// System.out.println(calculator.getXAverageTheirs() + " - " + calculator.getXAverageStrangers() + "; "

// + calculator.getStandardDeviationTheirs() + " - " + calculator.getStandardDeviationStrangers());

}

}

Time.java

public class Time {

final static long SECOND = 1000;

final static long MINUTE = 60 * Time.SECOND;

final static long HOUR = 60 * Time.MINUTE;

}

VisionLabsMatcher.java

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class VisionLabsMatcher implements IMatcher

{

private final String pathToEtalonTemp = "C:/temp/etalon_temp";

private final String pathToCandidateTemp = "C:/temp/candidate_temp";

private final String pathToEtalonKeys = "C:/temp/etalon_keys";

private final String pathToCandidateKeys = "C:/temp/candidate_keys";

private final String pathToReport = "C:/temp/report.csv";

private final static String HASP_FEATURE_EXPIRED = "HASP_FEATURE_EXPIRED";

private final static String NO_FACES_FOUND = "NoFacesFound";

private float extractAndMatch() throws Exception {

Process p;

String command = null;

try {

command = VisionLabsMatcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()

+ "/../Extractor -S " + pathToEtalonTemp + " -D " + pathToEtalonKeys + " -Dt DPM -St CNN -Ft VGG";

p = Runtime.getRuntime().exec(command);

String line = "";

BufferedReader errors = new BufferedReader(new InputStreamReader(p.getErrorStream()));

BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((output.readLine()) != null) {}

while ((line = errors.readLine()) != null) {

if (line.contains(HASP_FEATURE_EXPIRED)) {

throw new Exception(HASP_FEATURE_EXPIRED);

} else if (line.contains(NO_FACES_FOUND)) {

return NO_FACE_ETALON;

} else {

return INTERNAL_ERROR;

}

}

p.waitFor();

command = VisionLabsMatcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()

+ "/../Extractor -S " + pathToCandidateTemp + " -D " + pathToCandidateKeys + " -Dt DPM -St CNN -Ft VGG";

p = Runtime.getRuntime().exec(command);

errors = new BufferedReader(new InputStreamReader(p.getErrorStream()));

output = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((output.readLine()) != null) {}

while ((line = errors.readLine()) != null) {

return NO_FACE_CANDIDATE;

}

p.waitFor();

command = VisionLabsMatcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()

+ "/../Matcher2 -T " + pathToEtalonKeys + " -C " + pathToCandidateKeys + " -D " + pathToReport + " -M 1 -St CNN";

p = Runtime.getRuntime().exec(command);

errors = new BufferedReader(new InputStreamReader(p.getErrorStream()));

output = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((output.readLine()) != null) {}

while ((line = errors.readLine()) != null) {

return INTERNAL_ERROR;

}

p.waitFor();

} catch (Exception e) {

throw e;

}

return parseResult();

}

private float parseResult() {

Pattern pattern = Pattern.compile("^(.+); (.+); (.+); (.+)$",

Pattern.MULTILINE);

BufferedReader reader = null;

try {

reader = new BufferedReader(new FileReader(pathToReport));

String line = null;

Matcher m = null;

while ((line = reader.readLine()) != null) {

try {

m = pattern.matcher(line);

m.find();

} catch (Exception e) {

return INTERNAL_ERROR;

}

return Float.parseFloat(m.group(4));

}

} catch (Exception e) {

return INTERNAL_ERROR;

} finally {

try {

reader.close();

} catch (IOException e) {

}

}

return (float) -1;

}

public float compare (String etalon, String candidate) throws Exception

{

float result = NO_SUCH_FILE;

if(prepare(etalon, candidate)) {

result = extractAndMatch();

};

return result;

}

private boolean prepare(String etalon, String candidate) {

File etalonTemp = new File(pathToEtalonTemp);

etalonTemp.mkdir();

File candidateTemp = new File(pathToCandidateTemp);

candidateTemp.mkdir();

File etalonKeys = new File(pathToEtalonKeys);

etalonKeys.mkdir();

File candidateKeys = new File(pathToCandidateKeys);

candidateKeys.mkdir();

File sourceEtalon = new File(etalon);

File sourceCandidate = new File(candidate);

Path etalonTempPath = Paths.get(pathToEtalonTemp + "/etalon.jpg");

Path candidateTempPath = Paths.get(pathToCandidateTemp + "/candidate.jpg");

try {

Files.copy(sourceEtalon.toPath(), etalonTempPath, REPLACE_EXISTING);

Files.copy(sourceCandidate.toPath(), candidateTempPath, REPLACE_EXISTING);

} catch (IOException e) {

return false;

}

return true;

}

}

Список литературы

1) International Journal of Computer Vision 57(2), 137 - 154, 2004.

2) Journal of Signal and Information Processing, 173 - 175, 2013.

3) IJCSNS International Journal of Computer Science and Network Security, Vol.10 No.6, 2010.

4) M. Turk and A. Pentland, “Eigenfaces for Recognition”, Journal of Cognitive Neuroscience, Vol.3, No.1, 71 - 86, 1991.

5) International Journal of Advanced Technology in Engineering and Science Vol.1, Issue No.12, 2013.

6) Jigar M. Pandya, Devang Rathod, Jigna J. Jadav, “A Survey of Face Recognition approach”, International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol.3, Issue 1, 632, 2013.

7) Jyoti S. Bedre, Shubhangi Sapkal, “Comparative Study of Face Recognition Techniques: A Review”, Emerging Trends in Computer Science and Information Technology - 2012 (ETCSIT2012) Proceedings published in International Journal of Computer Applications (IJCA), 2012.

8) Mohammed Javed, Bhaskar Gupta, “Performance Comparison of Various Face Detection Techniques”, International Journal of Scientific Research Engineering & Technology (IJSRET) Vol.2 Issue No.1, 19 - 27, 2013.

9) J. R. Beveridge, P. J. Phillips, G. H. Givens, B. A. Draper, M. N. Teli, and D. S. Bolme, “When high-quality face images match poorly”, Proceedings, Ninth International Conference on Automatic Face and Gesture Recognition, 2011.

10) O. Barkan, J. Weill, L. Wolf, and H. Aronowitz. “Fast high dimensional vector multiplication face recognition”, ICCV, 2013.

11) T. Berg and P. N. Belhumeur. “Tom-vs-pete classifiers and identity-preserving alignment for face verification”, BMVC, 2012.

12) G. B. Huang, M. A. Mattar, H. Lee, and E. G. Learned-Miller. “Learning to align from scratch”, NIPS, 773 - 781, 2012.

13) Y. Taigman and L. Wolf. “Leveraging billions of faces to overcome performance barriers in unconstrained face recognition”, 2011.

14) Голубев М. Н. “Использование информации о цвете в алгоритме выделения лиц на базе бустинга”, Сб. науч. труд. 13-й всерос. науч.-техн. конф. “Нейроинформатика - 2011”, М, 2011. Ч. 3. С. 55 - 62.

15) Unsang Park, “Face Recognition: face in video, age invariance, and facial marks”, Michigan State University, 2009.

16) T. Ahonen, A. Hadid and M. Pietikainen, “Face description with Local Binary Patterns”, Application to Face Recognition. Machine Vision Group, University of Oulu, Finland, 2006.

17) T. Ahonen, A. Hadid, M. Pietikainen and T. Maenpaa. “Face recognition based on the appearance of local regions”, Proceedings of the 17th International Conference on Pattern Recognition, 2004.

18) R. Gottumukkal and V.K. Asari, “An Improved Face Recognition Technique Based on Modular PCA Approach”, Pattern Recognition Letters, Vol.25, 429 - 436, 2004.

19) T. Ojala, M. Pietikainen and D. Harwood, “A comparative study of texture measures with classification based on feature distributions”, Pattern Recognition Vol.29, 1996.

20) M. Turk and A. Pentland, “Eigenfaces for recognition”, Cognitive Neuroscience, 72 - 86, 1991.

21) M. Kirby and L. Sirovich, “Application of the Karhunen-Loeve procedure for the characterization of human faces”, IEEE Transactions on Pattern Analysis and Machine Intelligence, 103 - 108, 1990.

22) W. Zhao, R. Chellappa, P. J. Phillips, and A. Rosenfeld, “Face recognition: A literature survey”, ACM Computing Surveys (CSUR), 399 - 458, 2003.

23) S. Z. Li and A. K. Jain, “Handbook of Face Recognition”, Springer-Verlag, Secaucus, NJ, 2005.

24) W. Zhao and R. Chellappa “Robust face recognition using symmetric shape from-shading”, Technical Report, Center for Automation Research, University of Maryland, 1999.

25) T. Chen, Y. Wotao, S. Z. Xiang, D. Comaniciu, and T. S. Huang, “Total variation models for variable lighting face recognition”, IEEE Transactions on Pattern Analysis and Machine Intelligence, 1519 - 1524, 2006.

26) M. Grudin, “On internal representation in face recognition systems”, Pattern Recognition, 1161 - 1177, 2000.

27) P. A. Viola and M. J. Jones, “Robust real-time face detection”, International Journal of Computer Vision, 137 - 154, 2004.

Размещено на Allbest.ru


Подобные документы

  • Обзор алгоритмов распознания объектов на двумерных изображениях. Выбор языка программирования. Обнаружение устойчивых признаков изображения. Исследование алгоритмов поиска объектов на плоскости. Модификация алгоритма поиска максимума дискретной функции.

    дипломная работа [1,0 M], добавлен 16.06.2013

  • Обзор математических методов распознавания. Общая архитектура программы преобразования автомобильного номерного знака. Детальное описание алгоритмов: бинаризация изображения, удаление обрамления, сегментация символов и распознавание шаблонным методом.

    курсовая работа [4,8 M], добавлен 22.06.2011

  • Анализ существующих алгоритмов распознавания режимов работы газотурбинного двигателя. Метод группового учета аргументов, метод Байеса. Применение технологий системного моделирования на этапе проектирования интеллектуальной системы распознавания режимов.

    курсовая работа [1,4 M], добавлен 11.04.2012

  • Словесный, графический, табличный, программный способы представления алгоритма. Основные конструкции в любом алгоритмическом языке. Теория обнаружения, различения и оценивания сигналов. Радиолокационные системы обнаружения. Система распознавания образов.

    презентация [4,8 M], добавлен 09.06.2015

  • Обзор основных алгоритмов и методов распознавания лиц. Архитектура средств динамического отслеживания лиц в видеопоследовательности. Результаты тестирования на больших объемах видеоданных. Разработка алгоритмов и методов динамического отслеживания лиц.

    дипломная работа [5,9 M], добавлен 20.07.2014

  • Понятие визуальной системы ввода информации, ее сущность и особенности, место и роль в современном развитии интерфейсов между человеком и компьютером. Развитие технологии автоматического обнаружения и распознавания лица, контуров губ в видеопотоке.

    научная работа [94,3 K], добавлен 29.01.2009

  • Этапы разработки системы реального времени для распознавания лиц на статическом изображении в условиях сложных сцен. Основные понятия алгоритма AdaBoost. Использование примитивов Хаара для описания свойств изображений. Среда разработки "Borland Delphi".

    курсовая работа [6,8 M], добавлен 06.01.2011

  • Использование бинарных деревьев для поиска данных. Схемы алгоритмов работы с бинарным деревом. Проектирование алгоритмов и программ. Структура программного комплекса. Язык С# как средство для разработки автоматизированной информационной системы "Адрес".

    курсовая работа [914,9 K], добавлен 14.11.2013

  • Исследование проблемы сравнения звуковых файлов и определение степени их схожести. Сравнение файлов с использованием метода нечеткого поиска, основанного на метрике (расстоянии) Левенштейна. Сравнение MIDI-файлов и реализация алгоритмов считывания.

    курсовая работа [2,0 M], добавлен 14.07.2012

  • Понятие и особенности построения алгоритмов распознавания образов. Различные подходы к типологии методов распознавания. Изучение основных способов представления знаний. Характеристика интенсиональных и экстенсиональных методов, оценка их качества.

    презентация [31,6 K], добавлен 06.01.2014

Работы в архивах красиво оформлены согласно требованиям ВУЗов и содержат рисунки, диаграммы, формулы и т.д.
PPT, PPTX и PDF-файлы представлены только в архивах.
Рекомендуем скачать работу.