James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 1 | import {Channel, Configuration} from 'org_frc971/aos/configuration_generated'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 2 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 3 | import {ByteBuffer} from 'flatbuffers'; |
| 4 | import {ImageMatchResult, Feature} from 'org_frc971/y2020/vision/sift/sift_generated' |
| 5 | import {CameraImage} from 'org_frc971/frc971/vision/vision_generated'; |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 6 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 7 | export class ImageHandler { |
| 8 | private canvas = document.createElement('canvas'); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 9 | private select = document.createElement('select'); |
| 10 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 11 | private imageBuffer: Uint8ClampedArray|null = null; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 12 | private image: CameraImage|null = null; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 13 | private imageTimestamp: BigInt|null = null; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 14 | private result: ImageMatchResult|null = null; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 15 | private resultTimestamp: BigInt|null = null; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 16 | private width = 0; |
| 17 | private height = 0; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 18 | private selectedIndex = 0; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 19 | private imageSkipCount = 3; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 20 | |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 21 | constructor(private readonly connection: Connection) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 22 | document.body.appendChild(this.select); |
| 23 | const defaultOption = document.createElement('option'); |
| 24 | defaultOption.innerText = 'Show all features'; |
| 25 | this.select.appendChild(defaultOption); |
| 26 | this.select.addEventListener('change', (ev) => this.handleSelect(ev)); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 27 | document.body.appendChild(this.canvas); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 28 | |
| 29 | this.connection.addConfigHandler(() => { |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 30 | this.connection.addHandler( |
| 31 | '/camera', ImageMatchResult.getFullyQualifiedName(), (data) => { |
| 32 | this.handleImageMetadata(data); |
| 33 | }); |
| 34 | this.connection.addHandler( |
| 35 | '/camera', CameraImage.getFullyQualifiedName(), (data) => { |
| 36 | this.handleImage(data); |
| 37 | }); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 38 | }); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 41 | handleSelect(ev: Event) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 42 | this.selectedIndex = (ev.target as HTMLSelectElement).selectedIndex; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 45 | handleImage(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 46 | console.log('got an image to process'); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 47 | if (this.imageSkipCount != 0) { |
| 48 | this.imageSkipCount--; |
| 49 | return; |
| 50 | } else { |
| 51 | this.imageSkipCount = 3; |
| 52 | } |
| 53 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 54 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 55 | this.image = CameraImage.getRootAsCameraImage(fbBuffer); |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 56 | this.imageTimestamp = this.image.monotonicTimestampNs(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 57 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 58 | this.width = this.image.cols(); |
| 59 | this.height = this.image.rows(); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 60 | if (this.width === 0 || this.height === 0) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 61 | return; |
| 62 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 63 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 64 | this.draw(); |
| 65 | } |
| 66 | |
| 67 | convertImage(): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 68 | this.imageBuffer = |
| 69 | new Uint8ClampedArray(this.width * this.height * 4); // RGBA |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 70 | // Read four bytes (YUYV) from the data and transform into two pixels of |
| 71 | // RGBA for canvas |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 72 | for (let j = 0; j < this.height; j++) { |
| 73 | for (let i = 0; i < this.width; i += 2) { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 74 | const y1 = this.image.data((j * this.width + i) * 2); |
| 75 | const u = this.image.data((j * this.width + i) * 2 + 1); |
| 76 | const y2 = this.image.data((j * this.width + i + 1) * 2); |
| 77 | const v = this.image.data((j * this.width + i + 1) * 2 + 1); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 78 | |
| 79 | // Based on https://en.wikipedia.org/wiki/YUV#Converting_between_Y%E2%80%B2UV_and_RGB |
| 80 | const c1 = y1 - 16; |
| 81 | const c2 = y2 - 16; |
| 82 | const d = u - 128; |
| 83 | const e = v - 128; |
| 84 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 85 | this.imageBuffer[(j * this.width + i) * 4 + 0] = |
| 86 | (298 * c1 + 409 * e + 128) >> 8; |
| 87 | this.imageBuffer[(j * this.width + i) * 4 + 1] = |
| 88 | (298 * c1 - 100 * d - 208 * e + 128) >> 8; |
| 89 | this.imageBuffer[(j * this.width + i) * 4 + 2] = |
| 90 | (298 * c1 + 516 * d + 128) >> 8; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 91 | this.imageBuffer[(j * this.width + i) * 4 + 3] = 255; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 92 | this.imageBuffer[(j * this.width + i) * 4 + 4] = |
| 93 | (298 * c2 + 409 * e + 128) >> 8; |
| 94 | this.imageBuffer[(j * this.width + i) * 4 + 5] = |
| 95 | (298 * c2 - 100 * d - 208 * e + 128) >> 8; |
| 96 | this.imageBuffer[(j * this.width + i) * 4 + 6] = |
| 97 | (298 * c2 + 516 * d + 128) >> 8; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 98 | this.imageBuffer[(j * this.width + i) * 4 + 7] = 255; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | handleImageMetadata(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 104 | console.log('got an image match result to process'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 105 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 106 | this.result = ImageMatchResult.getRootAsImageMatchResult(fbBuffer); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 107 | this.resultTimestamp = this.result.imageMonotonicTimestampNs(); |
| 108 | this.draw(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | draw(): void { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 112 | if (!this.imageTimestamp || !this.resultTimestamp || |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 113 | this.imageTimestamp !== this.resultTimestamp) { |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 114 | // console.log('image and result do not match'); |
| 115 | // console.log(this.imageTimestamp.low, this.resultTimestamp.low); |
| 116 | // console.log(this.imageTimestamp.high, this.resultTimestamp.high); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 117 | return; |
| 118 | } |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 119 | this.convertImage(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 120 | const ctx = this.canvas.getContext('2d'); |
| 121 | |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 122 | this.canvas.width = this.width; |
| 123 | this.canvas.height = this.height; |
| 124 | const idata = ctx.createImageData(this.width, this.height); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 125 | idata.data.set(this.imageBuffer); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 126 | ctx.putImageData(idata, 0, 0); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 127 | console.log('features: ', this.result.featuresLength()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 128 | if (this.selectedIndex === 0) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 129 | for (let i = 0; i < this.result.featuresLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 130 | const feature = this.result.features(i); |
| 131 | this.drawFeature(feature); |
| 132 | } |
| 133 | } else { |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 134 | console.log(this.result.imageMatchesLength(), this.result.cameraPosesLength()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 135 | const imageMatch = this.result.imageMatches(this.selectedIndex - 1); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 136 | for (let i = 0; i < imageMatch.matchesLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 137 | const featureIndex = imageMatch.matches(i).queryFeature(); |
| 138 | this.drawFeature(this.result.features(featureIndex)); |
| 139 | } |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 140 | // Draw center of target. |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 141 | const cameraPose = this.result.cameraPoses(this.selectedIndex - 1); |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 142 | ctx.strokeStyle = 'red'; |
| 143 | ctx.beginPath(); |
| 144 | ctx.arc( |
| 145 | cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(), |
| 146 | cameraPose.queryTargetPointRadius(), 0, 2 * Math.PI); |
| 147 | console.log(cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(), cameraPose.queryTargetPointRadius()); |
| 148 | ctx.stroke(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 149 | } |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 150 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 151 | while (this.select.lastChild) { |
| 152 | this.select.removeChild(this.select.lastChild); |
| 153 | } |
| 154 | const defaultOption = document.createElement('option'); |
| 155 | defaultOption.innerText = 'Show all features'; |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 156 | defaultOption.setAttribute('value', '0'); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 157 | this.select.appendChild(defaultOption); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 158 | for (let i = 0; i < this.result.imageMatchesLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 159 | const imageMatch = this.result.imageMatches(i); |
| 160 | const option = document.createElement('option'); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 161 | option.setAttribute('value', (i + 1).toString()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 162 | option.innerText = |
| 163 | `Show image ${i} features (${imageMatch.matchesLength()})`; |
| 164 | this.select.appendChild(option); |
| 165 | } |
| 166 | this.select.selectedIndex = this.selectedIndex; |
| 167 | } |
| 168 | |
| 169 | // Based on OpenCV drawKeypoint. |
| 170 | private drawFeature(feature: Feature) { |
| 171 | const ctx = this.canvas.getContext('2d'); |
| 172 | ctx.beginPath(); |
| 173 | ctx.arc(feature.x(), feature.y(), feature.size(), 0, 2 * Math.PI); |
| 174 | ctx.stroke(); |
| 175 | |
| 176 | ctx.beginPath(); |
| 177 | ctx.moveTo(feature.x(), feature.y()); |
| 178 | const angle = feature.angle() * Math.PI / 180; |
| 179 | ctx.lineTo( |
| 180 | feature.x() + feature.size() * Math.cos(angle), |
| 181 | feature.y() + feature.size() * Math.sin(angle)); |
| 182 | ctx.stroke(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 183 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 184 | } |