-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathface_pauseplay.cpp
100 lines (78 loc) · 2.05 KB
/
face_pauseplay.cpp
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
// Plays/pauses a media played by VLC media player, based on Haar face detection.
// Libraries used : OpenCV, LibVLC
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <vlc/vlc.h>
using namespace std;
using namespace cv;
// Create CC for HaarClassification
CascadeClassifier face_cascade;
// Face detection function, returns whether present
int faceDetect(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
// Convert to GRAY scale for cascade
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// Detect face
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(30,30));
// Return whether present
if(faces.size() == 0)
return 0;
return 1;
}
int main(int argc, char** argv)
{
// Variable declarations
VideoCapture capture;
Mat frame;
int esc;
int facePresent, facePast = 1;
// LibVLC requirements, plays video specified as a command line argument
libvlc_instance_t *instance = libvlc_new(0, NULL);
libvlc_media_t *media = libvlc_media_new_path(instance, argv[1]);
libvlc_media_player_t *mplayer = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
// Error checking
if(!face_cascade.load("XML/face.xml"))
{
printf("Error loading XML/face.xml\n");
return -1;
}
capture.open(-1);
if(!capture.isOpened())
{
printf("Error opening video capture.\n");
return -1;
}
libvlc_media_player_play(mplayer);
// Processing
while(capture.read(frame))
{
// More error checking
if(frame.empty())
{
printf("Empty frame.\n");
break;
}
// Function call, and pause/play depending on return value
facePresent = faceDetect(frame);
if(!facePresent && facePast)
libvlc_media_player_pause(mplayer);
if(facePresent && !facePast)
libvlc_media_player_play(mplayer);
facePast = facePresent;
// Premature break condition
esc = waitKey(10);
if((char)esc == 27)
{
printf("Escape pressed, breaking out.\n");
break;
}
}
return 0;
}