-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
213 lines (181 loc) · 5.29 KB
/
main.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
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
/**************************************************************************************************/
/* Configuration settings */
/**/ int ID = 1;
/**/ const char *serverURL = "//192.168.1.14:3000";
/*************************************************************************************************/
#include <signal.h>
#include <queue>
#include <thread>
#include <unistd.h>
#include <new>
#include "opencv.h"
#include "communication.h"
#include "loaded_data.h"
#include "declarations.h"
#include "easywsclient.h"
using easywsclient::WebSocket;
static WebSocket::pointer ws1 = NULL;
typedef struct {
cv::Mat frame;
cv::Mat fg_mask;
double tick;
} frame_wrap_t;
int delay = 100000;
bool with_gui = false;
bool with_fps = false;
bool end_while = true;
bool person_flow = true;
queue<frame_wrap_t*> frames;
cv::VideoCapture cap;
sem_t *cap_m_1,*cap_m_2,*cap_m_3,*push_m_1,*push_m_2, *push_m_3,*write_to_list,*data_flow;
void openCV();
void BG_thread1();
void BG_thread2();
void BG_thread3();
void web_socket();
void handle_message(const std::string & message);
int main(int argc, char *argv[]) {
setbuf(stdout, NULL);
arguments_read(argc, &argv[0]);
dealock_void();
signal(SIGTERM, contro_c);
signal(SIGINT, contro_c);
init();
cap = init_cap_bg("/home/andrej/Music/colisions/video.mkv");
std::thread cv(openCV);
std::thread thread1(BG_thread1);
std::thread thread2(BG_thread2);
std::thread thread3(BG_thread3);
// std::thread socket (web_socket);
cv.join();
thread1.join();
thread2.join();
thread3.join();
// socket.join();
return EXIT_SUCCESS;
}
void openCV() {
cv::Mat frame, fg_mask;
int counter = 0;
double frame_tick;
if (with_gui) {
namedWindow("Threshold", 0);
namedWindow("Tracking", 0);
}
while (end_while) {
sem_wait(data_flow);
sem_wait(write_to_list);
frame = frames.front()->frame;
fg_mask = frames.front()->fg_mask;
frame_tick = frames.front()->tick;
delete frames.front();
frames.pop();
sem_post(write_to_list);
counter++;
if (counter == 3){
counter = 0;
sem_post(push_m_1);
}
int exit_code = ProcessFrame(&frame, &fg_mask, frame_tick);
if (exit_code != 0){
printf("WTF ???");
}
if(with_gui) {
waitKey(1);
}
}
}
void BG_thread1(){
while(end_while) {
frame_wrap_t *frame1 = new frame_wrap_t;
if (frame1 == NULL) {
printf("malloc failed\n");
}
sem_wait(cap_m_1);
if(!cap.read(frame1->frame)) {
fprintf(stderr, "The camera has been disconnected!\n");
dealock_void();
exit(EXIT_FAILURE);
}
usleep((__useconds_t) delay);
sem_post(cap_m_2);
frame1->tick = (double) cv::getTickCount();
BgSubtractor(&(frame1->frame), &(frame1->fg_mask));
sem_wait(push_m_1);
sem_wait(write_to_list);
frames.push(frame1);
sem_post(write_to_list);
sem_post(data_flow);
sem_post(push_m_2);
}
}
void BG_thread2() {
while(end_while) {
frame_wrap_t *frame2 = new frame_wrap_t;
if (frame2 == NULL) {
printf("malloc failed\n");
}
sem_wait(cap_m_2);
if(!cap.read(frame2->frame)) {
fprintf(stderr, "The camera has been disconnected!\n");
dealock_void();
exit(EXIT_FAILURE);
}
usleep((__useconds_t) delay);
sem_post(cap_m_3);
frame2->tick = (double) cv::getTickCount();
BgSubtractor(&(frame2->frame), &(frame2->fg_mask));
sem_wait(push_m_2);
sem_wait(write_to_list);
frames.push(frame2);
sem_post(write_to_list);
sem_post(data_flow);
sem_post(push_m_3);
}
}
void BG_thread3() {
while(end_while) {
frame_wrap_t *frame3 = new frame_wrap_t;
if (frame3 == NULL) {
printf("malloc failed\n");
}
sem_wait(cap_m_3);
if(!cap.read(frame3->frame)) {
fprintf(stderr, "The camera has been disconnected!\n");
dealock_void();
exit(EXIT_FAILURE);
}
usleep((__useconds_t) delay);
sem_post(cap_m_1);
frame3->tick = (double) cv::getTickCount();
BgSubtractor(&(frame3->frame), &(frame3->fg_mask));
sem_wait(push_m_3);
sem_wait(write_to_list);
frames.push(frame3);
sem_post(write_to_list);
sem_post(data_flow);
}
}
void web_socket(){
char json_message[40] ;
char url[50];
sprintf (url,"ws:%s",serverURL);
printf("%s\n",url);
save_settings_to_var();
save_tags();
ws1 = WebSocket::from_url(url);
sprintf (json_message,"{\"topic\":\"authPortal\",\"body\":\"%d\"}",ID);
ws1->send(json_message);
while (ws1->getReadyState() != WebSocket::CLOSED) {
ws1->poll();
ws1->dispatch(handle_message);
usleep (1 * pow(10,6)); //sleep 1s
}
delete ws1;
}
void handle_message(const std::string & message){
printf(">>> %s\n", message.c_str());
if (message == "ping"){
ws1->send("pong");
}
}