-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop_left_cheek_viola_jones.m
102 lines (76 loc) · 3.01 KB
/
crop_left_cheek_viola_jones.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
clear all
close all
clc
addpath(genpath('..\Codes\face-release1.0-basic'));
addpath(genpath('..\Codes'));
% Load model for face detection
load face_p99.mat; % model
% Load AAM model
load cGN_DPM.mat;
% Set directory
cd '..\Codes';
% Define video paths
video_path = '..\Video_Person_ID\Experiment_Jun_2022\';
day_subfolders = ['Day1\'];
day_len = size(day_subfolders, 1);
person_subfolders = ['03\'];
person_len = size(person_subfolders, 1);
% Define out video path
out_video_path = '..\Left_Cheek_out\';
% For every day
for i=1:day_len
% For every person
for j=1:person_len
% Define video path
vpath = [video_path day_subfolders(i, :) person_subfolders(j, :)];
files = dir(vpath);
% For every video in vpath
for k=3:length(files)
% Load video
vfilename = [vpath files(k).name];
v = VideoReader(vfilename);
% Construct video writer
[fpath, name, ext] = fileparts(files(k).name);
% Forehead
out_vfilename = [out_video_path 'Left_Cheek\' day_subfolders(i, :) person_subfolders(j, :) name]
vwriter = VideoWriter(out_vfilename, 'Uncompressed AVI');
vwriter.FrameRate = v.FrameRate;
open(vwriter);
tic
% Extract 1st frame
frame = readFrame(v);
faceDetector = vision.CascadeObjectDetector('FrontalFaceCART'); %Create a detector object
img = rgb2gray(frame(:,:,:)); % convert to gray
BB = step(faceDetector,img); % Detect faces
[numRows,~] = size(BB);
if numRows==1
fitbox(1,1) = BB(1,2);
fitbox(1,2) = BB(1,1);
fitbox(1,3) = BB(1,3);
fitbox(1,4) = BB(1,4);
else
[~, idx] = max(BB(:,3));
fitbox(1,1) = BB(idx,2);
fitbox(1,2) = BB(idx,1);
fitbox(1,3) = BB(idx,3);
fitbox(1,4) = BB(idx,4);
end
% Create forehead rectangle
my_w = fitbox(1,3)*(1/3-sqrt(5)/9);
leftcheek_rect = [(fitbox(1,2) round(fitbox(1,3)/5)), fitbox(1,1) round(fitbox(1,3)/3 2*my_w), round(fitbox(1,3)/5), round(fitbox(1,3)/3-2*my_w)];
% For every frame
while hasFrame(v)
% Cropped face
cropped_frame = imcrop(frame, leftcheek_rect);
% Write cropped frame in new video
writeVideo(vwriter, cropped_frame);
% Read new frame
frame = readFrame(v);
%frame = undistortImage(frame,cameraParams);
end
close(vwriter);
toc
%break
end
end
end