Skip to content

Commit

Permalink
add face recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesPikachu committed Dec 1, 2018
1 parent a3d51be commit 52112bb
Show file tree
Hide file tree
Showing 74 changed files with 178 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Application/360 Panorama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
![img](./results/pictures2/stitch_image_cropped.JPG)

# Usage
#### Step1:
#### Step1
```sh
Modify <line13~18> according to your needs.
Modify <line13~18> in <main.m> according to your needs.
```
#### Step2:
#### Step2
```sh
Run main.m to get the result.
```
34 changes: 34 additions & 0 deletions Application/Face Recognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Face Recognition via Eigenface
```sh
1. PCA to compute eigenface, show eigenface images
2. Project a face image into the face space, show the reconstructed face
3. Face Detection: finding the size and position of a face in an image
4. Face Recognition: Find the person in the training set for each detected face.
```

# Effect
#### smiling
![img](./results/smiling/eigenfaces.jpg)
![img](./results/smiling/reconstructedfaces.jpg)
#### nonsmiling
![img](./results/nonsmiling/eigenfaces.jpg)
![img](./results/nonsmiling/reconstructedfaces.jpg)

# Usage
## Get results
#### Step1
```sh
Modify <line15~17> in <main.m> according to your needs.
```
#### Step2
```sh
Run main.m to get the results.
```
## Show results
```sh
Modify <line10~12> in <showResults.m> according to your needs.
```
#### Step2
```sh
Run showResults.m to show the results.
```
18 changes: 9 additions & 9 deletions Application/Face Recognition/detector.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
Function:
Detect and recognize faces in a image.
Input:
-image:
-faces_matrix:
-eigenfaces:
-FACESIZE:
-image: the image need to be detected.
-faces_matrix: the face dataset.
-eigenfaces: the eigenface of 'dataset' obtained by PCA.
-FACESIZE: all roi will be resize to FACESIZE.
Output:
-face_coordinates:
-dist_det:
-dist_rec:
-face_coordinates: the coordinate and size of a detected face.
-dist_det: the distance between the croped roi and its reconstruct 'face'.
-dist_rec: the matched face in the dataset and the corresponding distance.
%}
function [face_coordinates, dists_det, dists_rec] = detector(image, faces_matrix, eigenfaces, FACESIZE)
face_coordinates = [];
Expand All @@ -23,7 +23,7 @@
thresh_det = 0.24;
thresh_rec = 0.21;
[faces_matrix_center, faces_matrix_mu] = centerlizeData(faces_matrix);
faces_matrix_construct = faces_matrix_center * eigenfaces * eigenfaces';
faces_matrix_reconstruct = faces_matrix_center * eigenfaces * eigenfaces';
if length(size(image)) == 3
image = rgb2gray(image);
end
Expand All @@ -46,7 +46,7 @@
roi_reconstruct = roi_project * eigenfaces';
dist_det = sum(abs(roi_reconstruct - roi_center)) / sum(roi_vector);
if dist_det < thresh_det
dist_rec = bsxfun(@minus, faces_matrix_construct, roi_reconstruct);
dist_rec = bsxfun(@minus, faces_matrix_reconstruct, roi_reconstruct);
dist_rec = sum(abs(dist_rec), 2) / sum(roi_vector);
[dist_rec, dist_rec_idx] = sort(dist_rec);
if dist_rec(1) < thresh_rec
Expand Down
35 changes: 35 additions & 0 deletions Application/Face Recognition/filterFace.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%{
Author:
Charles
Function:
Choose one of the rois that matched to the same face according to their match distance.
Input:
-face_coordinates: unfiltered face_coordinates.
-dists_det: unfiltered dists_det.
-dists_rec: unfiltered dists_rec.
Output:
-face_coordinates: filtered face_coordinates.
-dists_det: filtered dists_det.
-dists_rec: filtered dists_rec.
%}
function [face_coordinates_new, dists_det_new, dists_rec_new] = filterFace(face_coordinates, dists_det, dists_rec)
face_coordinates_new = [];
dists_det_new = [];
dists_rec_new = [];
reco_labels = unique(dists_rec(:, 1));
for i = 1: length(reco_labels)
index = find(dists_rec(:, 1) == reco_labels(i));
face_coordinate = face_coordinates(index, :);
dist_det = dists_det(index, :);
dist_rec = dists_rec(index, 2);
dist_rec2 = dists_rec(index, :);
index = find(dist_rec == min(dist_rec));
face_coordinates_new = [face_coordinates_new; face_coordinate(index, :)];
dists_det_new = [dists_det_new; dist_det(index, :)];
dists_rec_new = [dists_rec_new; dist_rec2(index, :)];
end
[~, index] = sort(dists_rec_new(:, 2));
face_coordinates_new = face_coordinates_new(index, :);
dists_det_new = dists_det_new(index, :);
dists_rec_new = dists_rec_new(index, :);
end
4 changes: 2 additions & 2 deletions Application/Face Recognition/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
close all;
addpath('./utils');
OUTPATH = './results/';
TRAINDATA = 'smiling';
TESTDATA = 'smiling';
TRAINDATA = 'nonsmiling';
TESTDATA = 'nonsmiling';
FACESIZE = [50, 50];
FACESPATH = ['./class_images/' TRAINDATA '_cropped/'];
PEOPLEPATH = ['./class_images/group/' TESTDATA '/'];
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions Application/Face Recognition/showResults.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%{
Author:
Charles
Function:
Show the results of face detection and recognition.
%}
close all;
clear;
OUTPATH = './results/';
TRAINDATA = 'nonsmiling';
addpath('./utils');
iou_thresh = 0.5;
num = 7;
for img_idx = 1: num
img_path = [OUTPATH TRAINDATA '/' num2str(img_idx) '.jpg'];
img = imread(img_path);
figure();
imshow(img);
load([OUTPATH TRAINDATA '/' num2str(img_idx) '_face_coordinates.mat']);
load([OUTPATH TRAINDATA '/' num2str(img_idx) '_dists_det.mat']);
load([OUTPATH TRAINDATA '/' num2str(img_idx) '_dists_rec.mat']);
[face_coordinates, dists_det, dists_rec] = filterFace(face_coordinates, dists_det, dists_rec);
coordinates = {};
count = 0;
for i = 1: size(face_coordinates, 1)
hold on;
color = 'green';
if isempty(coordinates)
rectangle('Position', face_coordinates(i, :), 'edgecolor', color);
text(face_coordinates(i, 1) - 5, face_coordinates(i, 2) - 5, num2str(dists_rec(i, 1)), 'color', color);
coordinates = [coordinates, face_coordinates(i, :)];
count = count + 1;
else
flag = true;
for j = 1: length(coordinates)
if bboxIoU(coordinates{j}, face_coordinates(i, :)) > iou_thresh
flag = false;
break
end
end
if flag
rectangle('Position', face_coordinates(i, :), 'edgecolor', color);
text(face_coordinates(i, 1) - 5, face_coordinates(i, 2) - 5, num2str(dists_rec(i, 1)), 'color', color);
coordinates = [coordinates, face_coordinates(i, :)];
count = count + 1;
end
end
if count == 3
break
end
end
end
34 changes: 34 additions & 0 deletions Application/Face Recognition/utils/bboxIoU.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
%{
Author:
Charles
Function:
Compute iou.
Input:
-box1: one of the boxes, (top_left_x, top_left_y, width, height).
-box2: one of the boxes, (top_left_x, top_left_y, width, height).
Output:
-iou: IoU = Overlapping area / (two boxes' total area - Overlapping area).
%}
function iou = bboxIoU(box1, box2)
mx = min(box1(1), box2(1));
Mx = max(box1(1)+box1(3), box2(1)+box2(3));
my = min(box1(2), box2(2));
My = max(box1(2)+box1(4), box2(2)+box2(4));
w1 = box1(3);
h1 = box1(4);
w2 = box2(3);
h2 = box2(4);
uw = Mx - mx;
uh = My - my;
cw = w1 + w2 - uw;
ch = h1 + h2 - uh;
if cw <=0 || ch <= 0
iou = 0;
else
area1 = w1 * h1;
area2 = w2 * h2;
carea = cw * ch;
uarea = area1 + area2 - carea;
iou = carea / uarea;
end
end
5 changes: 5 additions & 0 deletions Application/Face Recognition/utils/readTgaImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
info = readTgaHeader(info);
end
fid = fopen(info.Filename, 'rb', 'l');
if(fid < 0)
fprintf('could not open file %s\n', info.Filename);
return
end
fseek(fid, info.HeaderSize, 'bof');
bytesp = ceil(info.Depth) / 8;
npixels = info.Width * info.Height * bytesp;
if(~info.Rle)
Expand Down
4 changes: 2 additions & 2 deletions Harris/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ One of the famous keypoint detectors.
![img](./effect/processed.jpg)

# Usage
#### Step1:
#### Step1
```sh
Modify the image_path in line7 of Harris.m.
Choose to use line9(extractPoints1_1) or line10(extractPoints3_3, by default).
```
#### Step2:
#### Step2
```sh
Run Harris.m to get the result.
```
4 changes: 2 additions & 2 deletions Sift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ The version is a bad implementation, I would improve it when I'm free.
![img](./effect/processed.png)
# Usage
#### Step1:
#### Step1
```sh
Modify the image_path in line7 of test.m.
```
#### Step2:
#### Step2
```sh
Run test.m to get the result.
```

0 comments on commit 52112bb

Please sign in to comment.