Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 1 | import {Channel} from 'aos/configuration_generated'; |
| 2 | import {Connection} from 'aos/network/www/proxy'; |
| 3 | import {Connect} from 'aos/network/connect_generated'; |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 4 | import {ImageMatchResult} from 'y2020/vision/sift/sift_generated' |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 5 | import {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 | */ |
| 11 | const 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 Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 53 | |
| 54 | export class ImageHandler { |
| 55 | private canvas = document.createElement('canvas'); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 56 | private select = document.createElement('select'); |
| 57 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 58 | private imageBuffer: Uint8ClampedArray|null = null; |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 59 | private image: CameraImage|null = null; |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 60 | private imageTimestamp: flatbuffers.Long|null = null; |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 61 | private result: ImageMatchResult|null = null; |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 62 | private resultTimestamp: flatbuffers.Long|null = null; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 63 | private width = 0; |
| 64 | private height = 0; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 65 | private selectedIndex = 0; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 66 | private imageSkipCount = 3; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 67 | |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 68 | constructor(private readonly connection: Connection) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 69 | 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 Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 74 | document.body.appendChild(this.canvas); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 75 | |
| 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 Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 108 | handleSelect(ev: Event) { |
| 109 | this.selectedIndex = ev.target.selectedIndex; |
| 110 | } |
| 111 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 112 | handleImage(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 113 | console.log('got an image to process'); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 114 | if (this.imageSkipCount != 0) { |
| 115 | this.imageSkipCount--; |
| 116 | return; |
| 117 | } else { |
| 118 | this.imageSkipCount = 3; |
| 119 | } |
| 120 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 121 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 122 | this.image = CameraImage.getRootAsCameraImage(fbBuffer); |
| 123 | this.imageTimestamp = this.image.monotonicTimestampNs(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 124 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 125 | this.width = this.image.cols(); |
| 126 | this.height = this.image.rows(); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 127 | if (this.width === 0 || this.height === 0) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 130 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 131 | this.draw(); |
| 132 | } |
| 133 | |
| 134 | convertImage(): void { |
| 135 | this.imageBuffer = new Uint8ClampedArray(this.width * this.height * 4); // RGBA |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 136 | // Read four bytes (YUYV) from the data and transform into two pixels of |
| 137 | // RGBA for canvas |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 138 | for (const j = 0; j < this.height; j++) { |
| 139 | for (const i = 0; i < this.width; i += 2) { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 140 | 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 Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 144 | |
| 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 Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 151 | 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 Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 157 | this.imageBuffer[(j * this.width + i) * 4 + 3] = 255; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 158 | 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 Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 164 | this.imageBuffer[(j * this.width + i) * 4 + 7] = 255; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | handleImageMetadata(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 170 | console.log('got an image match result to process'); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 171 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 172 | this.result = ImageMatchResult.getRootAsImageMatchResult(fbBuffer); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 173 | this.resultTimestamp = this.result.imageMonotonicTimestampNs(); |
| 174 | this.draw(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | draw(): void { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 178 | if (!this.imageTimestamp || !this.resultTimestamp || |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 179 | this.imageTimestamp.low !== this.resultTimestamp.low || |
| 180 | this.imageTimestamp.high !== this.resultTimestamp.high) { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 181 | 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 Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 186 | this.convertImage(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 187 | const ctx = this.canvas.getContext('2d'); |
| 188 | |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 189 | this.canvas.width = this.width; |
| 190 | this.canvas.height = this.height; |
| 191 | const idata = ctx.createImageData(this.width, this.height); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 192 | idata.data.set(this.imageBuffer); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 193 | ctx.putImageData(idata, 0, 0); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 194 | 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 { |
| 201 | const imageMatch = this.result.imageMatches(this.selectedIndex - 1); |
| 202 | for (const i = 0; i < imageMatch.matchesLength(); i++) { |
| 203 | const featureIndex = imageMatch.matches(i).queryFeature(); |
| 204 | this.drawFeature(this.result.features(featureIndex)); |
| 205 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 206 | } |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame^] | 207 | |
| 208 | // Draw 'center' of target. |
| 209 | // TODO(alex) adjust to new location in flatbuffer for these fields |
| 210 | //ctx.strokeStyle = 'red'; |
| 211 | //ctx.beginPath(); |
| 212 | //ctx.arc( |
| 213 | // this.result.targetPointX(), this.result.targetPointY(), 20, 0, |
| 214 | // 2 * Math.PI); |
| 215 | //ctx.stroke(); |
| 216 | |
| 217 | while (this.select.lastChild) { |
| 218 | this.select.removeChild(this.select.lastChild); |
| 219 | } |
| 220 | const defaultOption = document.createElement('option'); |
| 221 | defaultOption.innerText = 'Show all features'; |
| 222 | defaultOption.setAttribute('value', 0); |
| 223 | this.select.appendChild(defaultOption); |
| 224 | for (const i = 0; i < this.result.imageMatchesLength(); i++) { |
| 225 | const imageMatch = this.result.imageMatches(i); |
| 226 | const option = document.createElement('option'); |
| 227 | option.setAttribute('value', i + 1); |
| 228 | option.innerText = |
| 229 | `Show image ${i} features (${imageMatch.matchesLength()})`; |
| 230 | this.select.appendChild(option); |
| 231 | } |
| 232 | this.select.selectedIndex = this.selectedIndex; |
| 233 | } |
| 234 | |
| 235 | // Based on OpenCV drawKeypoint. |
| 236 | private drawFeature(feature: Feature) { |
| 237 | const ctx = this.canvas.getContext('2d'); |
| 238 | ctx.beginPath(); |
| 239 | ctx.arc(feature.x(), feature.y(), feature.size(), 0, 2 * Math.PI); |
| 240 | ctx.stroke(); |
| 241 | |
| 242 | ctx.beginPath(); |
| 243 | ctx.moveTo(feature.x(), feature.y()); |
| 244 | const angle = feature.angle() * Math.PI / 180; |
| 245 | ctx.lineTo( |
| 246 | feature.x() + feature.size() * Math.cos(angle), |
| 247 | feature.y() + feature.size() * Math.sin(angle)); |
| 248 | ctx.stroke(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 249 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 250 | } |