Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 1 | import {CameraImage} from 'y2020/vision/vision_generated'; |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame^] | 2 | import {ImageMatchResult} from 'y2020/vision/sift/sift_generated' |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 3 | |
| 4 | export class ImageHandler { |
| 5 | private canvas = document.createElement('canvas'); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 6 | private imageBuffer: Uint8ClampedArray|null = null; |
| 7 | private imageTimestamp: flatbuffers.Long|null = null; |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame^] | 8 | private result: ImageMatchResult|null = null; |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 9 | private resultTimestamp: flatbuffers.Long|null = null; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 10 | |
| 11 | constructor() { |
| 12 | document.body.appendChild(this.canvas); |
| 13 | } |
| 14 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 15 | handleImage(data: Uint8Array): void { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 16 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 17 | const image = CameraImage.getRootAsCameraImage(fbBuffer); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 18 | this.imageTimestamp = image.monotonicTimestampNs(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 19 | |
| 20 | const width = image.cols(); |
| 21 | const height = image.rows(); |
| 22 | if (width === 0 || height === 0) { |
| 23 | return; |
| 24 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 25 | this.imageBuffer = new Uint8ClampedArray(width * height * 4); // RGBA |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 26 | |
| 27 | // Read four bytes (YUYV) from the data and transform into two pixels of |
| 28 | // RGBA for canvas |
| 29 | for (const j = 0; j < height; j++) { |
| 30 | for (const i = 0; i < width; i += 2) { |
| 31 | const y1 = image.data((j * width + i) * 2); |
| 32 | const u = image.data((j * width + i) * 2 + 1); |
| 33 | const y2 = image.data((j * width + i + 1) * 2); |
| 34 | const v = image.data((j * width + i + 1) * 2 + 1); |
| 35 | |
| 36 | // Based on https://en.wikipedia.org/wiki/YUV#Converting_between_Y%E2%80%B2UV_and_RGB |
| 37 | const c1 = y1 - 16; |
| 38 | const c2 = y2 - 16; |
| 39 | const d = u - 128; |
| 40 | const e = v - 128; |
| 41 | |
| 42 | imageBuffer[(j * width + i) * 4 + 0] = (298 * c1 + 409 * e + 128) >> 8; |
| 43 | imageBuffer[(j * width + i) * 4 + 1] = |
| 44 | (298 * c1 - 100 * d - 208 * e + 128) >> 8; |
| 45 | imageBuffer[(j * width + i) * 4 + 2] = (298 * c1 + 516 * d + 128) >> 8; |
| 46 | imageBuffer[(j * width + i) * 4 + 3] = 255; |
| 47 | imageBuffer[(j * width + i) * 4 + 4] = (298 * c2 + 409 * e + 128) >> 8; |
| 48 | imageBuffer[(j * width + i) * 4 + 5] = |
| 49 | (298 * c2 - 100 * d - 208 * e + 128) >> 8; |
| 50 | imageBuffer[(j * width + i) * 4 + 6] = (298 * c2 + 516 * d + 128) >> 8; |
| 51 | imageBuffer[(j * width + i) * 4 + 7] = 255; |
| 52 | } |
| 53 | } |
| 54 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 55 | draw(); |
| 56 | } |
| 57 | |
| 58 | handleImageMetadata(data: Uint8Array): void { |
| 59 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame^] | 60 | this.result = ImageMatchResult.getRootAsImageMatchResult(fbBuffer); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 61 | this.resultTimestamp = result.imageMonotonicTimestampNs(); |
| 62 | draw(); |
| 63 | } |
| 64 | |
| 65 | draw(): void { |
| 66 | if (imageTimestamp.low !== resultTimestamp.low || |
| 67 | imageTimestamp.high !== resultTimestamp.high) { |
| 68 | return; |
| 69 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 70 | const ctx = this.canvas.getContext('2d'); |
| 71 | |
| 72 | this.canvas.width = width; |
| 73 | this.canvas.height = height; |
| 74 | const idata = ctx.createImageData(width, height); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 75 | idata.data.set(this.imageBuffer); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 76 | ctx.putImageData(idata, 0, 0); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 77 | ctx.beginPath(); |
| 78 | for (const feature of this.result.getFeatures()) { |
| 79 | // Based on OpenCV drawKeypoint. |
| 80 | ctx.arc(feature.x, feature.y, feature.size, 0, 2 * Math.PI); |
| 81 | ctx.moveTo(feature.x, feature.y); |
| 82 | // TODO(alex): check that angle is correct (0?, direction?) |
| 83 | const angle = feature.angle * Math.PI / 180; |
| 84 | ctx.lineTo( |
| 85 | feature.x + feature.radius * cos(angle), |
| 86 | feature.y + feature.radius * sin(angle)); |
| 87 | } |
| 88 | ctx.stroke(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 91 | getId(): string { |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 92 | return CameraImage.getFullyQualifiedName(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 93 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 94 | |
| 95 | getResultId(): string { |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame^] | 96 | return ImageMatchResult.getFullyQualifiedName(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 97 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 98 | } |