blob: 5ef8d887798dd18fe1a5ba211c4ae7f8c908a084 [file] [log] [blame]
Alex Perryd1969882020-03-06 21:19:00 -08001import {Channel} from 'aos/configuration_generated';
2import {Connection} from 'aos/network/www/proxy';
3import {Connect} from 'aos/network/connect_generated';
Austin Schuhf6e71392020-02-26 23:10:15 -08004import {ImageMatchResult} from 'y2020/vision/sift/sift_generated'
Alex Perryd1969882020-03-06 21:19:00 -08005import {CameraImage} from 'y2020/vision/vision_generated';
6
7/*
8 * All the messages that are required to show an image with metadata.
9 * Messages not readable on the server node are ignored.
10 */
11const REQUIRED_CHANNELS = [
12 {
13 name: '/pi1/camera',
14 type: CameraImage.getFullyQualifiedName(),
15 },
16 {
17 name: '/pi2/camera',
18 type: CameraImage.getFullyQualifiedName(),
19 },
20 {
21 name: '/pi3/camera',
22 type: CameraImage.getFullyQualifiedName(),
23 },
24 {
25 name: '/pi4/camera',
26 type: CameraImage.getFullyQualifiedName(),
27 },
28 {
29 name: '/pi5/camera',
30 type: CameraImage.getFullyQualifiedName(),
31 },
32 {
33 name: '/pi1/camera/detailed',
34 type: ImageMatchResult.getFullyQualifiedName(),
35 },
36 {
37 name: '/pi2/camera/detailed',
38 type: ImageMatchResult.getFullyQualifiedName(),
39 },
40 {
41 name: '/pi3/camera/detailed',
42 type: ImageMatchResult.getFullyQualifiedName(),
43 },
44 {
45 name: '/pi4/camera/detailed',
46 type: ImageMatchResult.getFullyQualifiedName(),
47 },
48 {
49 name: '/pi5/camera/detailed',
50 type: ImageMatchResult.getFullyQualifiedName(),
51 },
52];
Alex Perry5f474f22020-02-01 12:14:24 -080053
54export class ImageHandler {
55 private canvas = document.createElement('canvas');
Alex Perryfffe2e32020-02-29 19:48:17 -080056 private select = document.createElement('select');
57
Alex Perryb41d5782020-02-09 17:06:40 -080058 private imageBuffer: Uint8ClampedArray|null = null;
Alex Perry3dfcb812020-03-04 19:32:17 -080059 private image: CameraImage|null = null;
Alex Perryb41d5782020-02-09 17:06:40 -080060 private imageTimestamp: flatbuffers.Long|null = null;
Austin Schuhf6e71392020-02-26 23:10:15 -080061 private result: ImageMatchResult|null = null;
Alex Perryb41d5782020-02-09 17:06:40 -080062 private resultTimestamp: flatbuffers.Long|null = null;
Alex Perry22824d72020-02-29 17:11:43 -080063 private width = 0;
64 private height = 0;
Alex Perryfffe2e32020-02-29 19:48:17 -080065 private selectedIndex = 0;
Alex Perry22824d72020-02-29 17:11:43 -080066 private imageSkipCount = 3;
Alex Perry5f474f22020-02-01 12:14:24 -080067
Alex Perryd1969882020-03-06 21:19:00 -080068 constructor(private readonly connection: Connection) {
Alex Perryfffe2e32020-02-29 19:48:17 -080069 document.body.appendChild(this.select);
70 const defaultOption = document.createElement('option');
71 defaultOption.innerText = 'Show all features';
72 this.select.appendChild(defaultOption);
73 this.select.addEventListener('change', (ev) => this.handleSelect(ev));
Alex Perry5f474f22020-02-01 12:14:24 -080074 document.body.appendChild(this.canvas);
Alex Perryd1969882020-03-06 21:19:00 -080075
76 this.connection.addConfigHandler(() => {
77 this.sendConnect();
78 });
79 this.connection.addHandler(ImageMatchResult.getFullyQualifiedName(), (data) => {
80 this.handleImageMetadata(data);
81 });
82 this.connection.addHandler(CameraImage.getFullyQualifiedName(), (data) => {
83 this.handleImage(data);
84 });
85 }
86
87 private sendConnect(): void {
88 const builder = new flatbuffers.Builder(512);
89 const channels: flatbuffers.Offset[] = [];
90 for (const channel of REQUIRED_CHANNELS) {
91 const nameFb = builder.createString(channel.name);
92 const typeFb = builder.createString(channel.type);
93 Channel.startChannel(builder);
94 Channel.addName(builder, nameFb);
95 Channel.addType(builder, typeFb);
96 const channelFb = Channel.endChannel(builder);
97 channels.push(channelFb);
98 }
99
100 const channelsFb = Connect.createChannelsToTransferVector(builder, channels);
101 Connect.startConnect(builder);
102 Connect.addChannelsToTransfer(builder, channelsFb);
103 const connect = Connect.endConnect(builder);
104 builder.finish(connect);
105 this.connection.sendConnectMessage(builder);
Alex Perry5f474f22020-02-01 12:14:24 -0800106 }
107
Alex Perryfffe2e32020-02-29 19:48:17 -0800108 handleSelect(ev: Event) {
109 this.selectedIndex = ev.target.selectedIndex;
110 }
111
Alex Perryb41d5782020-02-09 17:06:40 -0800112 handleImage(data: Uint8Array): void {
Alex Perry3dfcb812020-03-04 19:32:17 -0800113 console.log('got an image to process');
Alex Perry22824d72020-02-29 17:11:43 -0800114 if (this.imageSkipCount != 0) {
115 this.imageSkipCount--;
116 return;
117 } else {
118 this.imageSkipCount = 3;
119 }
120
Alex Perry5f474f22020-02-01 12:14:24 -0800121 const fbBuffer = new flatbuffers.ByteBuffer(data);
Alex Perry3dfcb812020-03-04 19:32:17 -0800122 this.image = CameraImage.getRootAsCameraImage(fbBuffer);
123 this.imageTimestamp = this.image.monotonicTimestampNs();
Alex Perry5f474f22020-02-01 12:14:24 -0800124
Alex Perry3dfcb812020-03-04 19:32:17 -0800125 this.width = this.image.cols();
126 this.height = this.image.rows();
Alex Perry22824d72020-02-29 17:11:43 -0800127 if (this.width === 0 || this.height === 0) {
Alex Perry5f474f22020-02-01 12:14:24 -0800128 return;
129 }
Alex Perry5f474f22020-02-01 12:14:24 -0800130
Alex Perry3dfcb812020-03-04 19:32:17 -0800131 this.draw();
132 }
133
134 convertImage(): void {
135 this.imageBuffer = new Uint8ClampedArray(this.width * this.height * 4); // RGBA
Alex Perry5f474f22020-02-01 12:14:24 -0800136 // Read four bytes (YUYV) from the data and transform into two pixels of
137 // RGBA for canvas
Alex Perry22824d72020-02-29 17:11:43 -0800138 for (const j = 0; j < this.height; j++) {
139 for (const i = 0; i < this.width; i += 2) {
Alex Perry3dfcb812020-03-04 19:32:17 -0800140 const y1 = this.image.data((j * this.width + i) * 2);
141 const u = this.image.data((j * this.width + i) * 2 + 1);
142 const y2 = this.image.data((j * this.width + i + 1) * 2);
143 const v = this.image.data((j * this.width + i + 1) * 2 + 1);
Alex Perry5f474f22020-02-01 12:14:24 -0800144
145 // Based on https://en.wikipedia.org/wiki/YUV#Converting_between_Y%E2%80%B2UV_and_RGB
146 const c1 = y1 - 16;
147 const c2 = y2 - 16;
148 const d = u - 128;
149 const e = v - 128;
150
Alex Perryfffe2e32020-02-29 19:48:17 -0800151 this.imageBuffer[(j * this.width + i) * 4 + 0] =
152 (298 * c1 + 409 * e + 128) >> 8;
153 this.imageBuffer[(j * this.width + i) * 4 + 1] =
154 (298 * c1 - 100 * d - 208 * e + 128) >> 8;
155 this.imageBuffer[(j * this.width + i) * 4 + 2] =
156 (298 * c1 + 516 * d + 128) >> 8;
Alex Perry22824d72020-02-29 17:11:43 -0800157 this.imageBuffer[(j * this.width + i) * 4 + 3] = 255;
Alex Perryfffe2e32020-02-29 19:48:17 -0800158 this.imageBuffer[(j * this.width + i) * 4 + 4] =
159 (298 * c2 + 409 * e + 128) >> 8;
160 this.imageBuffer[(j * this.width + i) * 4 + 5] =
161 (298 * c2 - 100 * d - 208 * e + 128) >> 8;
162 this.imageBuffer[(j * this.width + i) * 4 + 6] =
163 (298 * c2 + 516 * d + 128) >> 8;
Alex Perry22824d72020-02-29 17:11:43 -0800164 this.imageBuffer[(j * this.width + i) * 4 + 7] = 255;
Alex Perry5f474f22020-02-01 12:14:24 -0800165 }
166 }
Alex Perryb41d5782020-02-09 17:06:40 -0800167 }
168
169 handleImageMetadata(data: Uint8Array): void {
Alex Perry3dfcb812020-03-04 19:32:17 -0800170 console.log('got an image match result to process');
Alex Perryb41d5782020-02-09 17:06:40 -0800171 const fbBuffer = new flatbuffers.ByteBuffer(data);
Austin Schuhf6e71392020-02-26 23:10:15 -0800172 this.result = ImageMatchResult.getRootAsImageMatchResult(fbBuffer);
Alex Perry22824d72020-02-29 17:11:43 -0800173 this.resultTimestamp = this.result.imageMonotonicTimestampNs();
174 this.draw();
Alex Perryb41d5782020-02-09 17:06:40 -0800175 }
176
177 draw(): void {
Alex Perryfffe2e32020-02-29 19:48:17 -0800178 if (!this.imageTimestamp || !this.resultTimestamp ||
Alex Perry22824d72020-02-29 17:11:43 -0800179 this.imageTimestamp.low !== this.resultTimestamp.low ||
180 this.imageTimestamp.high !== this.resultTimestamp.high) {
Alex Perryf23c05d2020-03-07 13:52:02 -0800181 // console.log('image and result do not match');
182 // console.log(this.imageTimestamp.low, this.resultTimestamp.low);
183 // console.log(this.imageTimestamp.high, this.resultTimestamp.high);
Alex Perryb41d5782020-02-09 17:06:40 -0800184 return;
185 }
Alex Perry3dfcb812020-03-04 19:32:17 -0800186 this.convertImage();
Alex Perry5f474f22020-02-01 12:14:24 -0800187 const ctx = this.canvas.getContext('2d');
188
Alex Perry22824d72020-02-29 17:11:43 -0800189 this.canvas.width = this.width;
190 this.canvas.height = this.height;
191 const idata = ctx.createImageData(this.width, this.height);
Alex Perryb41d5782020-02-09 17:06:40 -0800192 idata.data.set(this.imageBuffer);
Alex Perry5f474f22020-02-01 12:14:24 -0800193 ctx.putImageData(idata, 0, 0);
Alex Perryfffe2e32020-02-29 19:48:17 -0800194 console.log('features: ', this.result.featuresLength();
195 if (this.selectedIndex === 0) {
196 for (const i = 0; i < this.result.featuresLength(); i++) {
197 const feature = this.result.features(i);
198 this.drawFeature(feature);
199 }
200 } else {
Alex Perryf23c05d2020-03-07 13:52:02 -0800201 console.log(this.result.imageMatchesLength(), this.result.cameraPosesLength());
Alex Perryfffe2e32020-02-29 19:48:17 -0800202 const imageMatch = this.result.imageMatches(this.selectedIndex - 1);
203 for (const i = 0; i < imageMatch.matchesLength(); i++) {
204 const featureIndex = imageMatch.matches(i).queryFeature();
205 this.drawFeature(this.result.features(featureIndex));
206 }
Alex Perryf23c05d2020-03-07 13:52:02 -0800207 // Draw center of target.
208 const cameraPose = this.result.cameraPoses(this.selctedIndex - 1);
209 ctx.strokeStyle = 'red';
210 ctx.beginPath();
211 ctx.arc(
212 cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(),
213 cameraPose.queryTargetPointRadius(), 0, 2 * Math.PI);
214 console.log(cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(), cameraPose.queryTargetPointRadius());
215 ctx.stroke();
Alex Perryb41d5782020-02-09 17:06:40 -0800216 }
Alex Perryfffe2e32020-02-29 19:48:17 -0800217
Alex Perryfffe2e32020-02-29 19:48:17 -0800218 while (this.select.lastChild) {
219 this.select.removeChild(this.select.lastChild);
220 }
221 const defaultOption = document.createElement('option');
222 defaultOption.innerText = 'Show all features';
223 defaultOption.setAttribute('value', 0);
224 this.select.appendChild(defaultOption);
225 for (const i = 0; i < this.result.imageMatchesLength(); i++) {
226 const imageMatch = this.result.imageMatches(i);
227 const option = document.createElement('option');
228 option.setAttribute('value', i + 1);
229 option.innerText =
230 `Show image ${i} features (${imageMatch.matchesLength()})`;
231 this.select.appendChild(option);
232 }
233 this.select.selectedIndex = this.selectedIndex;
234 }
235
236 // Based on OpenCV drawKeypoint.
237 private drawFeature(feature: Feature) {
238 const ctx = this.canvas.getContext('2d');
239 ctx.beginPath();
240 ctx.arc(feature.x(), feature.y(), feature.size(), 0, 2 * Math.PI);
241 ctx.stroke();
242
243 ctx.beginPath();
244 ctx.moveTo(feature.x(), feature.y());
245 const angle = feature.angle() * Math.PI / 180;
246 ctx.lineTo(
247 feature.x() + feature.size() * Math.cos(angle),
248 feature.y() + feature.size() * Math.sin(angle));
249 ctx.stroke();
Alex Perry5f474f22020-02-01 12:14:24 -0800250 }
Alex Perry5f474f22020-02-01 12:14:24 -0800251}