-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (66 loc) · 2.36 KB
/
server.js
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
const Kinect2 = require("kinect2");
const kinect = new Kinect2();
const fs = require("fs");
const main = async (useRandom) => {
if (useRandom) {
console.log("Using random numbers");
const width = 512; // Kinect depth frame width
const height = 424; // Kinect depth frame height
// Generate a 2D array with random numbers
const depthArray = Array.from(
{ length: Math.floor(height * 1.6) },
() =>
Array.from({ length: width }, () =>
Math.floor(Math.random() * (3200 - 1958)) + 1958
)
);
// Write the output to a file, overwriting any existing content
fs.writeFileSync("output.txt", JSON.stringify(depthArray));
// Keep the program running to generate new random data
while (true) {
await new Promise((resolve) => setTimeout(resolve, 1000));
// Generate new random data
depthArray.forEach((row, y) =>
row.forEach((_, x) => {
depthArray[y][x] = Math.floor(Math.random() * (3200 - 1958)) + 1958;
})
);
// Write the new data to the file
fs.writeFileSync("output.txt", JSON.stringify(depthArray));
}
} else {
console.log("Using Kinect");
kinect.open();
kinect.openDepthReader();
let writing = false;
// Set up an event listener to receive depth frame data
kinect.on("depthFrame", (depthFrame) => {
if (!writing) {
writing = true;
const width = 512; // Kinect depth frame width
const height = 424; // Kinect depth frame height
// Convert the Buffer to a 2D array
const depthArray = Array.from(
{ length: Math.floor(height * 1.6) },
(_, y) =>
Array.from({ length: width }, (_, x) => {
const originalY = Math.floor(y / 1.6); // Map new y to original y
return depthFrame[originalY * width + x] * 25;
})
);
// Write the output to a file, overwriting any existing content
fs.writeFileSync("output.txt", JSON.stringify(depthArray));
// Schedule the next write after a 1-second delay
setTimeout(() => {
writing = false;
}, 100);
}
});
// Keep the program running to receive depth frame data
while (true) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
};
const useRandom = process.argv[2] === "random";
main(useRandom);