-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvideo_process.js
135 lines (112 loc) · 4.59 KB
/
video_process.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
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
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
import * as Base64 from "./base64.js";
var detections=[];
var imgSaveRequested=0;
window.onload = (event) => {
init();
loadImg('saved_det');
}
async function init() {
// WebWorkers use `postMessage` and therefore work with Comlink.
const Apriltag = Comlink.wrap(new Worker("apriltag.js"));
// must call this to init apriltag detector; argument is a callback for when the detector is ready
window.apriltag = await new Apriltag(Comlink.proxy(() => {
// set camera info; we must define these according to the device and image resolution for pose computation
//window.apriltag.set_camera_info(double fx, double fy, double cx, double cy)
window.apriltag.set_tag_size(5, .5);
// start processing frames
window.requestAnimationFrame(process_frame);
}));
}
async function process_frame() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
let ctx = canvas.getContext("2d");
let imageData;
try {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
} catch (err) {
console.log("Failed to get video frame. Video not started ?");
setTimeout(process_frame, 500); // try again in 0.5 s
return;
}
let imageDataPixels = imageData.data;
let grayscalePixels = new Uint8Array(ctx.canvas.width * ctx.canvas.height); // this is the grayscale image we will pass to the detector
for (var i = 0, j = 0; i < imageDataPixels.length; i += 4, j++) {
let grayscale = Math.round((imageDataPixels[i] + imageDataPixels[i + 1] + imageDataPixels[i + 2]) / 3);
grayscalePixels[j] = grayscale; // single grayscale value
imageDataPixels[i] = grayscale;
imageDataPixels[i + 1] = grayscale;
imageDataPixels[i + 2] = grayscale;
}
ctx.putImageData(imageData, 0, 0);
// draw previous detection
detections.forEach(det => {
// draw tag borders
ctx.beginPath();
ctx.lineWidth = "5";
ctx.strokeStyle = "blue";
ctx.moveTo(det.corners[0].x, det.corners[0].y);
ctx.lineTo(det.corners[1].x, det.corners[1].y);
ctx.lineTo(det.corners[2].x, det.corners[2].y);
ctx.lineTo(det.corners[3].x, det.corners[3].y);
ctx.lineTo(det.corners[0].x, det.corners[0].y);
ctx.font = "bold 20px Arial";
var txt = ""+det.id;
ctx.fillStyle = "blue";
ctx.textAlign = "center";
ctx.fillText(txt, det.center.x, det.center.y+5);
ctx.stroke();
});
// detect aprilTag in the grayscale image given by grayscalePixels
detections = await apriltag.detect(grayscalePixels, ctx.canvas.width, ctx.canvas.height);
if (imgSaveRequested && detections.length > 0) {
let savep = Base64.bytesToBase64(ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height).data);
var det = JSON.stringify({
det_data: detections[0],
img_data: LZString.compressToUTF16(savep),
img_width: ctx.canvas.width,
img_height: ctx.canvas.height
});
//console.log("Saving detection data.");
localStorage.setItem("detectData", det);
buttonToggle();
loadImg('saved_det');
}
window.requestAnimationFrame(process_frame);
}
async function loadImg(targetHtmlElemId) {
var detectData = localStorage.getItem('detectData');
if (detectData) {
let detectDataObj = JSON.parse(detectData);
let savedPixels = Base64.base64ToBytes(LZString.decompressFromUTF16(detectDataObj.img_data));
delete detectDataObj.img_data;
const canvasSaved = document.getElementById(targetHtmlElemId+"_canvas");
let ctx = canvasSaved.getContext("2d");
canvasSaved.width = detectDataObj.img_width;
canvasSaved.height = detectDataObj.img_height;
let imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
imageData.data.set(savedPixels);
ctx.putImageData(imageData, 0, 0);
//console.log(detectDataObj.det_data);
let detDataSaved = document.getElementById(targetHtmlElemId+"_data");
detDataSaved.value=JSON.stringify(detectDataObj, null, 2);
} else console.log("detectData not found");
}
var button = document.getElementById('req_save');
button.addEventListener('click', function() {
buttonToggle();
//console.log("setImgSaveRequested", imgSaveRequested);
});
function buttonToggle() {
if (imgSaveRequested == 0) {
button.innerHTML = "Saving next detection... (press to cancel)";
imgSaveRequested = 1;
button.className += " active";
} else {
button.innerHTML = "Save next detection (local storage)";
imgSaveRequested = 0;
button.className.replace(" active", "");
}
}