-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
238 lines (191 loc) · 4.68 KB
/
main.cpp
File metadata and controls
238 lines (191 loc) · 4.68 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <iomanip>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/tracking.hpp"
#include "opencv2/tracking/tldDataset.hpp"
#define SIZEX 640
#define SIZEY 480
using namespace std;
using namespace cv;
CascadeClassifier face_cascade;
Ptr<BackgroundSubtractor> pMOG2;
MultiTracker trackers;
void usage(char *s);
char *filename;
struct coord {
int x;
int y;
};
/*
* Class which represent a person as an object to be tracked with coordinates
*/
class Person
{
public:
//virtual ~Person ();
Person (int id, int xi, int yi, int max_agei);
void updateCoord (int xn, int yn);
void incAge();
private:
int i;
int x;
int y;
int max_age;
int age;
string dir;
bool done;
vector<coord> tracks;
};
/* Constructor */
Person::Person (int id, int xi, int yi, int max_agei) {
i = id;
x = xi;
y = yi;
max_age = max_agei;
}
/*
* Update the position and the "tracks":
* - Last position is stored in tracks
* - Position is updated with current values
* - age is set to 0
*/
void Person::updateCoord (int xn, int yn) {
struct coord ncord;
ncord.x = x;
ncord.y = y;
tracks.push_back(ncord);
x = xn;
y = yn;
age = 0;
}
/*
* Increment age of the last detection
*/
void Person::incAge()
{
age++;
if (age > max_age) {
done = true;
}
}
vector<Person> persons;
void detect(Mat frame) {
Mat frame_gray, bgless, bin, kernelo, kernelc;
Mat binc, bino;
Size sz = frame.size();
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
char str[20];
double area, areaTh = 0;
Rect r;
kernelo = Mat::ones(3, 3, CV_8UC1);
kernelc = Mat::ones(11, 11, CV_8UC1);
pMOG2->apply(frame, bgless);
threshold(bgless, bin, 200, 255, THRESH_BINARY);
//opening erode dilate
morphologyEx(bin, bino, 0, kernelo);
// closing dilate erode
morphologyEx(bino, binc, 1, kernelc);
// Put some text
sprintf(str,"%d pers", 0);
putText(frame, str, Point2f(50,50), FONT_HERSHEY_SIMPLEX, 1,
Scalar(0,0,255,255), 3);
// Draw a line
line(frame, Point2f(0, sz.height/2), Point2f(sz.width, sz.height/2),
Scalar(0, 0, 0), 8, 2);
// Draw green contours
findContours(binc, contours, hierarchy, CV_RETR_EXTERNAL,
CV_CHAIN_APPROX_NONE, Point(0, 0));
for(int i = 0; i < contours.size(); i++)
{
drawContours(frame, contours, (int)i, Scalar(0,0,255,255), 2,
8, hierarchy, 0, Point());
area=contourArea(contours[i],false);
if(area>areaTh) {
areaTh = area;
r = boundingRect(contours[i]);
rectangle(frame, r, Scalar(0,0,255,255), 2, 8);
}
}
imshow("Frame 1", frame);
imshow("Post", binc);
//imshow("Background less", bgless);
//imshow("Binarized", bin);
}
int main(int argc, char *argv[])
{
Ptr<TrackerKCF> tracker = TrackerKCF::create();
VideoCapture camera("peopleCounter.avi");
//VideoCapture camera(0);
camera.set(CV_CAP_PROP_FRAME_WIDTH, SIZEX);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, SIZEY);
if (!camera.isOpened())
{
cout << "Cannot open camera" << endl;
return 1;
}
pMOG2 = createBackgroundSubtractorMOG2();
for(;;)
{
Mat frame;
camera.grab();
//
camera.retrieve(frame);
tracker->init(frame, bbox);
detect(frame);
//
if (waitKey(30) == 27) break;
}
camera.release();
return 0;
//int opt;
//if(argc == 1)
//{
//usage(argv[0]);
//exit(1);
//}
//// x: signifie que x attend un paramètre
//while ((opt = getopt(argc,argv,"hc:m:t:")) != EOF){
//switch(opt)
//{
//case 'h':
//usage(argv[0]);
//exit(1);
//case 'c':
////filename = optarg;
////cout << "Nouveau net : "<< filename <<endl ;
////createNet(filename);
////return 0;
//case '?':
//if (optopt == 'c' || optopt == 'm' || optopt == 't')
//fprintf (stderr,
//"Option -%c requiert un argument.\n",
//optopt);
//else if (isprint (optopt))
//fprintf (stderr,
//"Option inconnue `-%c'.\n",
//optopt);
//else
//fprintf (stderr,
//"Caractere option inconnue`\\x%x'.\n",
//optopt);
//return 1;
//default:
//usage(argv[0]);
//}
//}
return 0;
}
void usage(char *s)
{
cout<<"Usage: "<<s<<" [-option] [argument]"<<endl;
cout<<"option: "<<"-h affiche l'aide"<<endl;
}