-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcascadeClassifier.php
More file actions
87 lines (83 loc) · 2.76 KB
/
cascadeClassifier.php
File metadata and controls
87 lines (83 loc) · 2.76 KB
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
<?php
use CV\Mat;
use CV\CascadeClassifier;
use CV\Size;
use CV\Point;
use CV\Scalar;
use CV\VideoCapture;
use const CV\{
COLOR_BGR2GRAY, CASCADE_SCALE_IMAGE
};
use function CV\{
cvtColor, equalizeHist, ellipse, circle, imshow, waitKey
};
$face_cascade_name = "haarcascade_frontalface_alt.xml";
$eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
$face_cascade = new CascadeClassifier();
$eyes_cascade = new CascadeClassifier();
$window_name = "Capture - Face detection";
function detectAndDisplay(Mat $frame)
{
global $face_cascade;
global $eyes_cascade;
global $window_name;
$faces = [];
$frame_gray = null;
$frame_gray = cvtColor($frame, COLOR_BGR2GRAY);
equalizeHist($frame_gray, $frame_gray);
//-- Detect faces
$face_cascade->detectMultiScale($frame_gray, $faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, new Size(30, 30));
for ($i = 0; $i < count($faces); $i++) {
$center = new Point($faces[$i]->x + $faces[$i]->width / 2, $faces[$i]->y + $faces[$i]->height / 2);
ellipse($frame, $center, new Size($faces[$i]->width / 2, $faces[$i]->height / 2), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0);
$faceROI = $frame_gray->getImageROI($faces[$i]);
$eyes = [];
//-- In each face, detect eyes
$eyes_cascade->detectMultiScale($faceROI, $eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, new Size(30, 30));
for ($j = 0; $j < count($eyes); $j++) {
$eye_center = new Point ($faces[$i]->x + $eyes[$j]->x + $eyes[$j]->width / 2, $faces[$i]->y + $eyes[$j]->y + $eyes[$j]->height / 2);
$radius = round(($eyes[$j]->width + $eyes[$j]->height) * 0.25);
circle($frame, $eye_center, $radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- Show what you got
imshow($window_name, $frame);
}
function run()
{
global $face_cascade;
global $face_cascade_name;
global $eyes_cascade;
global $eyes_cascade_name;
$capture = new VideoCapture();
$frame = null;
//-- 1. Load the cascades
if (!$face_cascade->load($face_cascade_name)) {
printf("--(!)Error loading face cascade\n");
return -1;
};
if (!$eyes_cascade->load($eyes_cascade_name)) {
printf("--(!)Error loading eyes cascade\n");
return -1;
};
//-- 2. Read the video stream
$capture->open(-1);
if (!$capture->isOpened()) {
printf("--(!)Error opening video capture\n");
return -1;
}
while ($capture->read($frame)) {
if ($frame->empty()) {
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay($frame);
$key = waitKey(10);
if ($key == 27) {
break;
} // escape
}
return 0;
}
run();