1
+ <?php
2
+ use CV \Mat ;
3
+ use CV \CascadeClassifier ;
4
+ use CV \Size ;
5
+ use CV \Point ;
6
+ use CV \Scalar ;
7
+ use CV \VideoCapture ;
8
+ use const CV \{
9
+ COLOR_BGR2GRAY , CASCADE_SCALE_IMAGE
10
+ };
11
+ use function CV \{
12
+ cvtColor , equalizeHist , ellipse , circle , imshow , waitKey
13
+ };
14
+
15
+ $ face_cascade_name = "haarcascade_frontalface_alt.xml " ;
16
+ $ eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml " ;
17
+ $ face_cascade = new CascadeClassifier ();
18
+ $ eyes_cascade = new CascadeClassifier ();
19
+ $ window_name = "Capture - Face detection " ;
20
+
21
+ function detectAndDisplay (Mat $ frame )
22
+ {
23
+ global $ face_cascade ;
24
+ global $ eyes_cascade ;
25
+ global $ window_name ;
26
+ $ faces = [];
27
+ $ frame_gray = null ;
28
+ $ frame_gray = cvtColor ($ frame , COLOR_BGR2GRAY );
29
+ equalizeHist ($ frame_gray , $ frame_gray );
30
+ //-- Detect faces
31
+ $ face_cascade ->detectMultiScale ($ frame_gray , $ faces , 1.1 , 2 , 0 | CASCADE_SCALE_IMAGE , new Size (30 , 30 ));
32
+ for ($ i = 0 ; $ i < count ($ faces ); $ i ++) {
33
+ $ center = new Point ($ faces [$ i ]->x + $ faces [$ i ]->width / 2 , $ faces [$ i ]->y + $ faces [$ i ]->height / 2 );
34
+ ellipse ($ frame , $ center , new Size ($ faces [$ i ]->width / 2 , $ faces [$ i ]->height / 2 ), 0 , 0 , 360 , new Scalar (255 , 0 , 255 ), 4 , 8 , 0 );
35
+ $ faceROI = $ frame_gray ->getImageROI ($ faces [$ i ]);
36
+ $ eyes = [];
37
+ //-- In each face, detect eyes
38
+ $ eyes_cascade ->detectMultiScale ($ faceROI , $ eyes , 1.1 , 2 , 0 | CASCADE_SCALE_IMAGE , new Size (30 , 30 ));
39
+ for ($ j = 0 ; $ j < count ($ eyes ); $ j ++) {
40
+ $ eye_center = new Point ($ faces [$ i ]->x + $ eyes [$ j ]->x + $ eyes [$ j ]->width / 2 , $ faces [$ i ]->y + $ eyes [$ j ]->y + $ eyes [$ j ]->height / 2 );
41
+ $ radius = round (($ eyes [$ j ]->width + $ eyes [$ j ]->height ) * 0.25 );
42
+ circle ($ frame , $ eye_center , $ radius , new Scalar (255 , 0 , 0 ), 4 , 8 , 0 );
43
+ }
44
+ }
45
+ //-- Show what you got
46
+ imshow ($ window_name , $ frame );
47
+ }
48
+
49
+ function run ()
50
+ {
51
+ global $ face_cascade ;
52
+ global $ face_cascade_name ;
53
+ global $ eyes_cascade ;
54
+ global $ eyes_cascade_name ;
55
+ $ capture = new VideoCapture ();
56
+ $ frame = null ;
57
+ //-- 1. Load the cascades
58
+ if (!$ face_cascade ->load ($ face_cascade_name )) {
59
+ printf ("--(!)Error loading face cascade \n" );
60
+ return -1 ;
61
+ };
62
+ if (!$ eyes_cascade ->load ($ eyes_cascade_name )) {
63
+ printf ("--(!)Error loading eyes cascade \n" );
64
+ return -1 ;
65
+ };
66
+ //-- 2. Read the video stream
67
+ $ capture ->open (-1 );
68
+ if (!$ capture ->isOpened ()) {
69
+ printf ("--(!)Error opening video capture \n" );
70
+ return -1 ;
71
+ }
72
+ while ($ capture ->read ($ frame )) {
73
+ if ($ frame ->empty ()) {
74
+ printf (" --(!) No captured frame -- Break! " );
75
+ break ;
76
+ }
77
+ //-- 3. Apply the classifier to the frame
78
+ detectAndDisplay ($ frame );
79
+ $ key = waitKey (10 );
80
+ if ($ key == 27 ) {
81
+ break ;
82
+ } // escape
83
+ }
84
+ return 0 ;
85
+ }
86
+
87
+ run ();
0 commit comments