-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
444 lines (364 loc) · 14.4 KB
/
Copy pathmain.cpp
File metadata and controls
444 lines (364 loc) · 14.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "config.h"
#include "cv.h"
#include "network.h"
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
vector<Point2f> load_points_from_file() {
vector<Point2f> pts;
ifstream fin(POINTS_FILE);
if (!fin.is_open()) {
cerr << "Could not open " << POINTS_FILE << endl;
exit(1);
}
string contents((istreambuf_iterator<char>(fin)), istreambuf_iterator<char>());
fin.close();
const char* p = contents.c_str();
vector<double> nums;
while (*p) {
if (*p == '-' || isdigit(static_cast<unsigned char>(*p))) {
char* endptr = nullptr;
double val = strtod(p, &endptr);
nums.push_back(val);
p = endptr;
} else {
++p;
}
}
if (nums.size() < REQUIRED_POINTS * 2) {
cerr << "Not enough numbers in " << POINTS_FILE
<< ". Need " << REQUIRED_POINTS * 2 << ", found " << nums.size() << endl;
exit(1);
}
for (int i = 0; i < REQUIRED_POINTS; ++i) {
double x = nums[2 * i];
double y = nums[2 * i + 1];
pts.emplace_back(static_cast<float>(x), static_cast<float>(y));
}
cout << "Loaded " << pts.size() << " calibration points from "
<< POINTS_FILE << endl;
return pts;
}
string mapToJson(const std::map<std::string, std::string>& data) {
std::stringstream json;
json << "{";
bool first = true;
for (const auto& kv : data) {
if (!first) json << ",";
json << "\"" << kv.first << "\":\"" << kv.second << "\"";
first = false;
}
json << "}";
return json.str();
}
bool calculate_all_key_positions(const vector<Point2f>& points) {
WHITE_KEY_BOUNDARIES.clear();
if (points.size() < 4) {
cerr << "Error: Need at least 4 ROI points for line extrapolation.\n";
return false;
}
Point2f roi_tl = points[0];
Point2f roi_tr = points[1];
Point2f roi_br = points[2];
Point2f roi_bl = points[3];
int num_lines = 9;
for (int i = 0; i < num_lines; ++i) {
float t = static_cast<float>(i) / static_cast<float>(num_lines - 1);
float top_x = (1.0f - t) * roi_tl.x + t * roi_tr.x;
float top_y = (1.0f - t) * roi_tl.y + t * roi_tr.y;
float bot_x = (1.0f - t) * roi_bl.x + t * roi_br.x;
float bot_y = (1.0f - t) * roi_bl.y + t * roi_br.y;
WHITE_KEY_BOUNDARIES.push_back({ Point2f(top_x, top_y), Point2f(bot_x, bot_y) });
}
return true;
}
void writeToFramebuffer(const Mat &frame, const char* fbdev)
{
static int fbfd = -1;
static char *fbp = nullptr;
static long screensize = 0;
if (fbfd < 0) {
fbfd = open(fbdev, O_RDWR);
if (fbfd < 0) {
cerr << "Error opening framebuffer device " << fbdev << endl;
return;
}
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
cerr << "Error reading fixed screen info" << endl;
close(fbfd);
fbfd = -1;
return;
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
cerr << "Error reading variable screen info" << endl;
close(fbfd);
fbfd = -1;
return;
}
screensize = vinfo.yres_virtual * finfo.line_length;
fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if (fbp == MAP_FAILED) {
cerr << "Error mapping framebuffer" << endl;
close(fbfd);
fbfd = -1;
fbp = nullptr;
return;
}
}
if (!fbp) return;
Mat frameResized;
if (frame.cols != FRAME_WIDTH || frame.rows != FRAME_HEIGHT) {
resize(frame, frameResized, Size(FRAME_WIDTH, FRAME_HEIGHT));
} else {
frameResized = frame;
}
Mat frameRGB16;
cvtColor(frameResized, frameRGB16, COLOR_BGR2BGR565);
long bytesToCopy = std::min(screensize,
(long)(frameRGB16.total() * frameRGB16.elemSize()));
memcpy(fbp, frameRGB16.data, bytesToCopy);
}
void start_live_tracking(
const map<string, Mat>& KEY_MASKS,
const map<string, vector<vector<Point>>>& KEY_MAPPING_POLYGONS,
const Mat& roi_mask,
const Mat& initial_frame_bgr
) {
VideoCapture cap;
if (!open_camera(cap)) {
return;
}
Mat prev_rgb;
cvtColor(initial_frame_bgr, prev_rgb, COLOR_BGR2RGB);
prev_rgb.convertTo(prev_rgb, CV_32FC3);
int frame_height = initial_frame_bgr.rows;
int frame_width = initial_frame_bgr.cols;
map<string, double> previous_motion_centroid;
cout << "\n--- LIVE TRACKING STARTED (framebuffer only). Press Ctrl+C to stop ---\n";
while (true) {
Mat frame;
if (!cap.read(frame)) {
cerr << "Failed to read frame from camera.\n";
break;
}
// Flip across Y axis (mirror) like in Python version
flip(frame, frame, 1);
if (frame.cols != FRAME_WIDTH || frame.rows != FRAME_HEIGHT) {
resize(frame, frame, Size(FRAME_WIDTH, FRAME_HEIGHT));
}
Mat frame_rgb;
cvtColor(frame, frame_rgb, COLOR_BGR2RGB);
Mat curr32;
frame_rgb.convertTo(curr32, CV_32FC3);
// Motion detection
Mat diffSq = dist_sq(curr32, prev_rgb);
Mat motion_mask;
compare(diffSq, THRESHOLD * THRESHOLD, motion_mask, CMP_GT); // 0/255, CV_8U
Mat motion_mask_roi;
bitwise_and(motion_mask, roi_mask, motion_mask_roi);
Mat motion_mask_u8;
motion_mask_roi.convertTo(motion_mask_u8, CV_8UC1);
vector<Point> motion_coords;
findNonZero(motion_mask_u8, motion_coords);
vector<string> highlighted_keys;
map<string, double> current_motion_centroid;
if (motion_coords.size() > 50) {
// For each key, check overlap
for (const auto& kv : KEY_MASKS) {
const string& label = kv.first;
const Mat& k_mask = kv.second;
Mat overlap;
bitwise_and(motion_mask_u8, k_mask, overlap);
int overlapCount = countNonZero(overlap);
if (overlapCount > 50) {
vector<Point> overlap_coords;
findNonZero(overlap, overlap_coords);
if (!overlap_coords.empty()) {
double sumY = 0.0;
for (const Point& p : overlap_coords) sumY += p.y;
double key_motion_y = sumY / overlap_coords.size();
current_motion_centroid[label] = key_motion_y;
auto itPrev = previous_motion_centroid.find(label);
if (itPrev != previous_motion_centroid.end()) {
double prev_y = itPrev->second;
if (key_motion_y > prev_y + MIN_VERTICAL_MOVEMENT) {
highlighted_keys.push_back(label);
}
}
}
}
}
}
// Visualize: start from camera frame
Mat display = frame.clone();
// 1) Static key drawing (outlines & fills)
for (const auto& kv : KEY_MAPPING_POLYGONS) {
const string& label = kv.first;
const vector<vector<Point>>& polys = kv.second;
if (label.find('#') != string::npos) {
// black keys
for (const auto& poly : polys) {
vector<vector<Point>> pvec{ poly };
fillPoly(display, pvec, Scalar(0, 0, 0)); // fill black
polylines(display, pvec, true, Scalar(255, 255, 255), 1); // white outline
}
} else {
// white keys: outlines only (light gray)
for (const auto& poly : polys) {
vector<vector<Point>> pvec{ poly };
polylines(display, pvec, true, Scalar(200, 200, 200), 1);
}
}
}
// 2) Dynamic highlighting on pressed keys
for (const string& label : highlighted_keys) {
auto itMask = KEY_MASKS.find(label);
auto itPoly = KEY_MAPPING_POLYGONS.find(label);
if (itMask == KEY_MASKS.end() || itPoly == KEY_MAPPING_POLYGONS.end())
continue;
const Mat& key_mask = itMask->second;
const vector<vector<Point>>& polys = itPoly->second;
// solid color layer
Mat highlight_layer_bgr(display.size(), display.type(), Scalar::all(0));
for (const auto& poly : polys) {
vector<vector<Point>> pvec{ poly };
fillPoly(highlight_layer_bgr, pvec, KEY_HIGHLIGHT_COLOR);
}
// blended
Mat blended_key_area;
addWeighted(highlight_layer_bgr, HIGHLIGHT_ALPHA,
display, 1.0 - HIGHLIGHT_ALPHA, 0.0, blended_key_area);
// combine with mask
Mat key_mask_bgr, key_mask_inv_bgr;
cvtColor(key_mask, key_mask_bgr, COLOR_GRAY2BGR);
Mat key_mask_inv;
bitwise_not(key_mask, key_mask_inv);
cvtColor(key_mask_inv, key_mask_inv_bgr, COLOR_GRAY2BGR);
Mat blended_foreground, background_no_key;
bitwise_and(blended_key_area, key_mask_bgr, blended_foreground);
bitwise_and(display, key_mask_inv_bgr, background_no_key);
display = background_no_key + blended_foreground;
}
// 3) Draw note labels
draw_note_labels(display, KEY_MAPPING_POLYGONS, GLOBAL_KEY_CENTROIDS);
// 4) Status text
if (!highlighted_keys.empty()) {
string status = "KEYS PLAYED: ";
for (size_t i = 0; i < highlighted_keys.size(); ++i) {
if (i > 0) status += ", ";
status += highlighted_keys[i];
}
putText(display, status,
Point(10, frame_height - 10),
FONT_HERSHEY_SIMPLEX, 0.7,
Scalar(0, 255, 0), 2);
} else {
string status = "No Downward Press Detected";
putText(display, status,
Point(10, frame_height - 10),
FONT_HERSHEY_SIMPLEX, 0.7,
Scalar(0, 0, 255), 2);
}
// 5) SEND PRESSED NOTES OVER TCP (white / black separated)
if (!highlighted_keys.empty()) {
std::map<std::string, std::string> packet;
std::stringstream white_ss;
std::stringstream black_ss;
for (const std::string& note : highlighted_keys) {
if (note.find('#') != std::string::npos) {
// black key
black_ss << note << " ";
} else {
// white key
white_ss << note << " ";
}
}
if (!white_ss.str().empty()) {
packet["white"] = white_ss.str();
}
if (!black_ss.str().empty()) {
packet["black"] = black_ss.str();
}
if (!packet.empty()) {
std::string json = mapToJson(packet);
std::cout << "[NET] Built packet: " << json << "\n";
if (clientSocket >= 0) {
bool ok = sendString(json);
if (!ok) {
std::cerr << "[NET] sendString failed for packet: " << json << "\n";
}
} else {
std::cerr << "[NET] Socket not connected, would have sent: " << json << "\n";
}
}
}
// 6) Write to framebuffer (480x272)
writeToFramebuffer(display, "/dev/fb0");
// Update previous frame + centroids
prev_rgb = curr32.clone();
previous_motion_centroid = current_motion_centroid;
// No waitKey / imshow -> stop with Ctrl+C from shell
}
cap.release();
}
// ------------------------
// MAIN
// ------------------------
int main() {
// 0. Set up TCP connection
if (!initializeConnection(SERVER_IP, SERVER_PORT)) {
std::cerr << "[NET] Warning: could not connect to server, running without network.\n";
}
// 1. Initial frame capture
VideoCapture temp_cap;
if (!open_camera(temp_cap)) {
cerr << "FATAL: Could not open camera for initial frame.\n";
closeConnection();
return -1;
}
Mat initial_frame_bgr;
if (!temp_cap.read(initial_frame_bgr)) {
cerr << "FATAL: Could not capture initial frame.\n";
temp_cap.release();
closeConnection();
return -1;
}
temp_cap.release();
// Flip initial frame across Y axis
flip(initial_frame_bgr, initial_frame_bgr, 1);
if (initial_frame_bgr.cols != FRAME_WIDTH || initial_frame_bgr.rows != FRAME_HEIGHT) {
resize(initial_frame_bgr, initial_frame_bgr, Size(FRAME_WIDTH, FRAME_HEIGHT));
}
// 2. Load calibration points
vector<Point2f> points_list = load_points_from_file();
// 3. Compute white key boundaries
if (!calculate_all_key_positions(points_list)) {
cerr << "Key position calculation failed.\n";
closeConnection();
return -1;
}
cout << "Key mapping geometry calculated.\n";
// 4. Initialize key mapping (polygons + centroids) + ROI mask
map<string, vector<vector<Point>>> KEY_MAPPING_POLYGONS;
Mat roi_mask;
initialize_key_mapping(initial_frame_bgr, points_list, KEY_MAPPING_POLYGONS, roi_mask);
// 5. Build key masks
map<string, Mat> KEY_MASKS = build_key_masks(
KEY_MAPPING_POLYGONS,
initial_frame_bgr.cols,
initial_frame_bgr.rows
);
// 6. Start live tracking (framebuffer only + TCP send)
start_live_tracking(KEY_MASKS, KEY_MAPPING_POLYGONS, roi_mask, initial_frame_bgr);
// 7. Cleanup
closeConnection();
return 0;
}