/
BodyDetection.cpp
137 lines (99 loc) · 2.93 KB
/
BodyDetection.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include <iostream>
#include <stdio.h>
#include <fstream>
int persone;
using namespace std;
using namespace cv;
bool detection(Mat frame);
/** Global variables*/
//here you modify the path of haar classifier
String body_cascade_name = "/home/nico/opencv-3.0.0-rc1/data/haarcascades/haarcascade_frontalface_default.xml";//ubuntu
//String body_cascade_name = "C:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml";//windows
CascadeClassifier body_cascade;
string window_name = "Capture - body detection Local pc";
int main(int argc, char* argv[])
{
VideoCapture vcap;
Mat image;
const string videoStreamAddress = "http://localhost:8082";//put here the camera IP
//open the video stream and make sure it's opened
if (!body_cascade.load(body_cascade_name))
{
printf("--(!)Error loading\n"); return -1;
}
if (!vcap.open(0))//use videoStreamAddress in brackets if you want to use IP camera
{
cout << "Error opening video stream or file" <<endl;
return -1;
}
bool result,result_1;
int i = 0;
char x[50];
string z;
bool detect =true;
while (1)
{
if (!vcap.read(image))
{
cout << "No frame" << endl;
waitKey(0);
}
result = detection(image);
if ((result == true) && detect==true)
{
cout << "detected" << endl;
//sostituire il percosro per il file Npersone.txt
ofstream file("/home/nico/node/Npersone.txt"); //se il file non esiste lo crea, altrimenti lo sovrascrive!used on ubuntu system
if(!file) {
cout<<"Errore nella creazione del file!";
return -1;
}
file <<persone;
file.close(); //close the file
sprintf(x, "curl http://localhost:3000/geofence");
z.assign(x);
system(z.c_str());
detect = false;
}
if ((detect == false))
{
i = 0;
////creiamo un tempo d'attesa di 5 (9000) minuti prima di inviare un'altra mail in presenza di persone
for (i; i < 9000;i++)
{
vcap.read(image);
imshow(window_name, image);
waitKey(10);
}
detect = true;
}
else
{
cout << "nobody" << endl;
}
}
}
//######################################################################################à
bool detection(Mat frame)
{
vector<Rect> bodys;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
/**detect body */
body_cascade.detectMultiScale(frame_gray, bodys, 1.8, 2, 3, Size(30, 30));
persone =bodys.size();
/**draw ellipse*/
for (unsigned int j = 0; j < bodys.size(); j++)
{
Point center(bodys[j].x + bodys[j].width*0.5, bodys[j].y + +bodys[j].height*0.5);
ellipse(frame, center, Size(bodys[j].width*0.5, bodys[j].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
}
imshow(window_name, frame);
if (bodys.empty()){ waitKey(10); return false; }
else{waitKey(2500); return true; }
}