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