forked from MCG-NJU/MixFormerV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
259 lines (196 loc) · 6.86 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <dirent.h>
#include <cmath>
#include <string>
#include <chrono>
#include <vector>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <future>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <curl/curl.h>
#include <chrono>
#include <sstream>
#include <nlohmann/json.hpp>
nlohmann::json trackedData;
static int client_socket;
// Server configurations
const char* server_ip = "127.0.0.1"; // Change this to the server's IP
const int server_port = 8002;
cv::Rect gTrackBbox;
bool bResult=false;
bool have_box=false;
static void sleepMs(int ms)
{
struct timeval delay;
delay.tv_sec = 0;
delay.tv_usec = ms * 1000; // ms
select(0, NULL, NULL, NULL, &delay);
}
int socket_init(){
// Create a socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
return 1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
// Connect to the server
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
return 1;
}
return 0;
}
void boundingBoxThread(){
std::cout << "Started bounding box thread" << std::endl;
char buffer[1024];
while (true) {
// Clear the buffer
if (bResult) {
int bytesRead = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytesRead == -1) {
std::cerr << "Error receiving data." << std::endl;
break;
} else if (bytesRead == 0) {
// Connection closed by the server
std::cout << "Server closed the connection." << std::endl;
break;
}
// Process the received data
std::string receivedData(buffer, bytesRead);
// std::cout << "Raw Received Data: " << receivedData << std::endl;
// Check if the received data is empty or too small
if (receivedData.empty() || receivedData.size() < 5) {
continue;
}
if (receivedData.substr(0, 4) == "TRK ") {
std::string jsonData = receivedData.substr(4);
try {
trackedData = nlohmann::json::parse(jsonData);
// Process parsedJson
// Example: Pri nt the parsed data
// std::cout << "Received JSON: " << trackedData << std::endl;
have_box = true;
} catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON Parsing Error: " << e.what() << std::endl;
// Handle the parsing error gracefully
}
}
memset(buffer, 0, sizeof(buffer));
}
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
}
void sendImage(int client_socket, const cv::Mat& image) {
int rows = image.rows;
int cols = image.cols;
// Prepend the "RGB " flag and image dimensions
std::string data = "RGB ";
// Get a pointer to the image data
const uchar* imageData = image.ptr();
// Append raw image data to the string
data.append(reinterpret_cast<const char*>(imageData), image.total() * image.elemSize());
// std::cout<<data.size()<<std::endl;
// Send the data
send(client_socket, data.c_str(), data.size(), 0);
}
void sendMessages(int client_socket, const char* message) {
std::string data = "TEXT" + std::string(message); // Prepend "TEXT" flag
send(client_socket, data.c_str(), data.size(), 0);
}
bool advance_track_init(cv::Mat _frame){
float x,y,w,h;
x = gTrackBbox.x * 1.0;
y = gTrackBbox.y * 1.0;
w = gTrackBbox.width * 1.0;
h = gTrackBbox.height * 1.0;
std::string json_message = "{\"x\": " + std::to_string(x) + ", \"y\": " + std::to_string(y) +
", \"w\": " + std::to_string(w) + ", \"h\": " + std::to_string(h) + "}";
const char* cString = json_message.c_str();
// // Copy the JSON message to shared memory
// std::strcpy(json_data, json_message.c_str());
sendMessages(client_socket,cString);
cv::Mat frameTemp = _frame.clone();
//frameTemp = frameTemp(cv::Range(26, 1050), cv::Range(786, 1810)).clone(); // 1024 * 1024
cv::resize(frameTemp, frameTemp, cv::Size(512, 512), 0, 0, cv::INTER_AREA); // 512 * 512
sendImage(client_socket, frameTemp);
return true;
}
bool advance_track_result(){
// std::cout<<trackedData<<std::endl;
// Access individual fields
int x = trackedData["x"];
int y = trackedData["y"];
int w = trackedData["w"];
int h = trackedData["h"];
int end = trackedData["end"];
std::cout << "Received tracked data:" << std::endl;
std::cout << "x: " << x << ", y: " << y << ", w: " << w << ", h: " << h << ", end: " << end << std::endl;\
// if (end==1){
// current_papilla.x = -1;
// current_papilla.y = -1;
// return true;
// }
// current_papilla.x = x + w/2.0;
// current_papilla.y = y + h/2.0;
gTrackBbox.width = w;
gTrackBbox.height = h;
gTrackBbox.x = x;
gTrackBbox.y = y;
return true;
}
int main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <video_file_path>" << std::endl;
return 1;
}
std::string videoFilePath = argv[1];
//init socket
socket_init();
//init bounding box thread
std::thread myThread(boundingBoxThread);
sleepMs(200);// 等待设备m
myThread.detach();
// Read video
cv::VideoCapture video(videoFilePath);
// Exit if video is not opened
if(!video.isOpened())
{
std::cout << "Could not read video file" << std::endl;
return 1;
}
// Read first frame
cv::Mat frame;
bool ok = video.read(frame);
// Resize the frame
cv::Mat resized_frame;
cv::resize(frame, resized_frame, cv::Size(512, 512), 0, 0, cv::INTER_LINEAR);
// Define initial bounding box
cv::Rect2d bbox(287, 23, 86, 320);
gTrackBbox = cv::selectROI(resized_frame, false);
bResult = advance_track_init(resized_frame);
// tracker->init(frame, bbox);
while(video.read(frame))
{
cv::resize(frame, resized_frame, cv::Size(512, 512), 0, 0, cv::INTER_LINEAR);
sendImage(client_socket, resized_frame);
if(have_box){
advance_track_result();
}
cv::rectangle(resized_frame, gTrackBbox, cv::Scalar( 255, 0, 0 ), 2, 1 );
cv::imshow("Tracking", resized_frame);
// Exit if ESC pressed.
int k = cv::waitKey(1);
if(k == 27)
{
break;
}
}
}