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