Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 1 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
| 2 | import * as connect from 'org_frc971/aos/network/connect_generated'; |
| 3 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
| 4 | import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 5 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 6 | import {Long} from 'org_frc971/external/com_github_google_flatbuffers/ts/long'; |
| 7 | import * as sift from 'org_frc971/y2020/vision/sift/sift_generated' |
| 8 | import * as vision from 'org_frc971/y2020/vision/vision_generated'; |
| 9 | |
| 10 | import Channel = configuration.aos.Channel; |
| 11 | import Configuration = configuration.aos.Configuration; |
| 12 | import Connect = connect.aos.message_bridge.Connect; |
| 13 | import CameraImage = vision.frc971.vision.CameraImage; |
| 14 | import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult; |
| 15 | import Feature = sift.frc971.vision.sift.Feature; |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * All the messages that are required to show an image with metadata. |
| 19 | * Messages not readable on the server node are ignored. |
| 20 | */ |
| 21 | const REQUIRED_CHANNELS = [ |
| 22 | { |
| 23 | name: '/pi1/camera', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 24 | type: CameraImage.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 25 | }, |
| 26 | { |
| 27 | name: '/pi2/camera', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 28 | type: CameraImage.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 29 | }, |
| 30 | { |
| 31 | name: '/pi3/camera', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 32 | type: CameraImage.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 33 | }, |
| 34 | { |
| 35 | name: '/pi4/camera', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 36 | type: CameraImage.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 37 | }, |
| 38 | { |
| 39 | name: '/pi5/camera', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 40 | type: CameraImage.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 41 | }, |
| 42 | { |
| 43 | name: '/pi1/camera/detailed', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 44 | type: ImageMatchResult.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 45 | }, |
| 46 | { |
| 47 | name: '/pi2/camera/detailed', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 48 | type: ImageMatchResult.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 49 | }, |
| 50 | { |
| 51 | name: '/pi3/camera/detailed', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 52 | type: ImageMatchResult.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 53 | }, |
| 54 | { |
| 55 | name: '/pi4/camera/detailed', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 56 | type: ImageMatchResult.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 57 | }, |
| 58 | { |
| 59 | name: '/pi5/camera/detailed', |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 60 | type: ImageMatchResult.getFullyQualifiedName(), |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 61 | }, |
| 62 | ]; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 63 | |
| 64 | export class ImageHandler { |
| 65 | private canvas = document.createElement('canvas'); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 66 | private select = document.createElement('select'); |
| 67 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 68 | private imageBuffer: Uint8ClampedArray|null = null; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 69 | private image: CameraImage|null = null; |
| 70 | private imageTimestamp: Long|null = null; |
| 71 | private result: ImageMatchResult|null = null; |
| 72 | private resultTimestamp: Long|null = null; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 73 | private width = 0; |
| 74 | private height = 0; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 75 | private selectedIndex = 0; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 76 | private imageSkipCount = 3; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 77 | |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 78 | constructor(private readonly connection: Connection) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 79 | document.body.appendChild(this.select); |
| 80 | const defaultOption = document.createElement('option'); |
| 81 | defaultOption.innerText = 'Show all features'; |
| 82 | this.select.appendChild(defaultOption); |
| 83 | this.select.addEventListener('change', (ev) => this.handleSelect(ev)); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 84 | document.body.appendChild(this.canvas); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 85 | |
| 86 | this.connection.addConfigHandler(() => { |
| 87 | this.sendConnect(); |
| 88 | }); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 89 | this.connection.addHandler( |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 90 | ImageMatchResult.getFullyQualifiedName(), (data) => { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 91 | this.handleImageMetadata(data); |
| 92 | }); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 93 | this.connection.addHandler(CameraImage.getFullyQualifiedName(), (data) => { |
| 94 | this.handleImage(data); |
| 95 | }); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private sendConnect(): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 99 | const builder = |
| 100 | new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder; |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 101 | const channels: flatbuffers.Offset[] = []; |
| 102 | for (const channel of REQUIRED_CHANNELS) { |
| 103 | const nameFb = builder.createString(channel.name); |
| 104 | const typeFb = builder.createString(channel.type); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 105 | Channel.startChannel(builder); |
| 106 | Channel.addName(builder, nameFb); |
| 107 | Channel.addType(builder, typeFb); |
| 108 | const channelFb = Channel.endChannel(builder); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 109 | channels.push(channelFb); |
| 110 | } |
| 111 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 112 | const channelsFb = |
| 113 | Connect.createChannelsToTransferVector(builder, channels); |
| 114 | Connect.startConnect(builder); |
| 115 | Connect.addChannelsToTransfer(builder, channelsFb); |
| 116 | const connect = Connect.endConnect(builder); |
Alex Perry | d196988 | 2020-03-06 21:19:00 -0800 | [diff] [blame] | 117 | builder.finish(connect); |
| 118 | this.connection.sendConnectMessage(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 121 | handleSelect(ev: Event) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 122 | this.selectedIndex = (ev.target as HTMLSelectElement).selectedIndex; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 125 | handleImage(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 126 | console.log('got an image to process'); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 127 | if (this.imageSkipCount != 0) { |
| 128 | this.imageSkipCount--; |
| 129 | return; |
| 130 | } else { |
| 131 | this.imageSkipCount = 3; |
| 132 | } |
| 133 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 134 | const fbBuffer = new ByteBuffer(data); |
| 135 | this.image = CameraImage.getRootAsCameraImage( |
| 136 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 137 | this.imageTimestamp = this.image.monotonicTimestampNs(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 138 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 139 | this.width = this.image.cols(); |
| 140 | this.height = this.image.rows(); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 141 | if (this.width === 0 || this.height === 0) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 142 | return; |
| 143 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 144 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 145 | this.draw(); |
| 146 | } |
| 147 | |
| 148 | convertImage(): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 149 | this.imageBuffer = |
| 150 | new Uint8ClampedArray(this.width * this.height * 4); // RGBA |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 151 | // Read four bytes (YUYV) from the data and transform into two pixels of |
| 152 | // RGBA for canvas |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 153 | for (let j = 0; j < this.height; j++) { |
| 154 | for (let i = 0; i < this.width; i += 2) { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 155 | const y1 = this.image.data((j * this.width + i) * 2); |
| 156 | const u = this.image.data((j * this.width + i) * 2 + 1); |
| 157 | const y2 = this.image.data((j * this.width + i + 1) * 2); |
| 158 | const v = this.image.data((j * this.width + i + 1) * 2 + 1); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 159 | |
| 160 | // Based on https://en.wikipedia.org/wiki/YUV#Converting_between_Y%E2%80%B2UV_and_RGB |
| 161 | const c1 = y1 - 16; |
| 162 | const c2 = y2 - 16; |
| 163 | const d = u - 128; |
| 164 | const e = v - 128; |
| 165 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 166 | this.imageBuffer[(j * this.width + i) * 4 + 0] = |
| 167 | (298 * c1 + 409 * e + 128) >> 8; |
| 168 | this.imageBuffer[(j * this.width + i) * 4 + 1] = |
| 169 | (298 * c1 - 100 * d - 208 * e + 128) >> 8; |
| 170 | this.imageBuffer[(j * this.width + i) * 4 + 2] = |
| 171 | (298 * c1 + 516 * d + 128) >> 8; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 172 | this.imageBuffer[(j * this.width + i) * 4 + 3] = 255; |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 173 | this.imageBuffer[(j * this.width + i) * 4 + 4] = |
| 174 | (298 * c2 + 409 * e + 128) >> 8; |
| 175 | this.imageBuffer[(j * this.width + i) * 4 + 5] = |
| 176 | (298 * c2 - 100 * d - 208 * e + 128) >> 8; |
| 177 | this.imageBuffer[(j * this.width + i) * 4 + 6] = |
| 178 | (298 * c2 + 516 * d + 128) >> 8; |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 179 | this.imageBuffer[(j * this.width + i) * 4 + 7] = 255; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | handleImageMetadata(data: Uint8Array): void { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 185 | console.log('got an image match result to process'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame^] | 186 | const fbBuffer = new ByteBuffer(data); |
| 187 | this.result = ImageMatchResult.getRootAsImageMatchResult( |
| 188 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 189 | this.resultTimestamp = this.result.imageMonotonicTimestampNs(); |
| 190 | this.draw(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | draw(): void { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 194 | if (!this.imageTimestamp || !this.resultTimestamp || |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 195 | this.imageTimestamp.low !== this.resultTimestamp.low || |
| 196 | this.imageTimestamp.high !== this.resultTimestamp.high) { |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 197 | // console.log('image and result do not match'); |
| 198 | // console.log(this.imageTimestamp.low, this.resultTimestamp.low); |
| 199 | // console.log(this.imageTimestamp.high, this.resultTimestamp.high); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 202 | this.convertImage(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 203 | const ctx = this.canvas.getContext('2d'); |
| 204 | |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 205 | this.canvas.width = this.width; |
| 206 | this.canvas.height = this.height; |
| 207 | const idata = ctx.createImageData(this.width, this.height); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 208 | idata.data.set(this.imageBuffer); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 209 | ctx.putImageData(idata, 0, 0); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 210 | console.log('features: ', this.result.featuresLength()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 211 | if (this.selectedIndex === 0) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 212 | for (let i = 0; i < this.result.featuresLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 213 | const feature = this.result.features(i); |
| 214 | this.drawFeature(feature); |
| 215 | } |
| 216 | } else { |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 217 | console.log(this.result.imageMatchesLength(), this.result.cameraPosesLength()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 218 | const imageMatch = this.result.imageMatches(this.selectedIndex - 1); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 219 | for (let i = 0; i < imageMatch.matchesLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 220 | const featureIndex = imageMatch.matches(i).queryFeature(); |
| 221 | this.drawFeature(this.result.features(featureIndex)); |
| 222 | } |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 223 | // Draw center of target. |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 224 | const cameraPose = this.result.cameraPoses(this.selectedIndex - 1); |
Alex Perry | f23c05d | 2020-03-07 13:52:02 -0800 | [diff] [blame] | 225 | ctx.strokeStyle = 'red'; |
| 226 | ctx.beginPath(); |
| 227 | ctx.arc( |
| 228 | cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(), |
| 229 | cameraPose.queryTargetPointRadius(), 0, 2 * Math.PI); |
| 230 | console.log(cameraPose.queryTargetPointX(), cameraPose.queryTargetPointY(), cameraPose.queryTargetPointRadius()); |
| 231 | ctx.stroke(); |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 232 | } |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 233 | |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 234 | while (this.select.lastChild) { |
| 235 | this.select.removeChild(this.select.lastChild); |
| 236 | } |
| 237 | const defaultOption = document.createElement('option'); |
| 238 | defaultOption.innerText = 'Show all features'; |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 239 | defaultOption.setAttribute('value', '0'); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 240 | this.select.appendChild(defaultOption); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 241 | for (let i = 0; i < this.result.imageMatchesLength(); i++) { |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 242 | const imageMatch = this.result.imageMatches(i); |
| 243 | const option = document.createElement('option'); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 244 | option.setAttribute('value', (i + 1).toString()); |
Alex Perry | fffe2e3 | 2020-02-29 19:48:17 -0800 | [diff] [blame] | 245 | option.innerText = |
| 246 | `Show image ${i} features (${imageMatch.matchesLength()})`; |
| 247 | this.select.appendChild(option); |
| 248 | } |
| 249 | this.select.selectedIndex = this.selectedIndex; |
| 250 | } |
| 251 | |
| 252 | // Based on OpenCV drawKeypoint. |
| 253 | private drawFeature(feature: Feature) { |
| 254 | const ctx = this.canvas.getContext('2d'); |
| 255 | ctx.beginPath(); |
| 256 | ctx.arc(feature.x(), feature.y(), feature.size(), 0, 2 * Math.PI); |
| 257 | ctx.stroke(); |
| 258 | |
| 259 | ctx.beginPath(); |
| 260 | ctx.moveTo(feature.x(), feature.y()); |
| 261 | const angle = feature.angle() * Math.PI / 180; |
| 262 | ctx.lineTo( |
| 263 | feature.x() + feature.size() * Math.cos(angle), |
| 264 | feature.y() + feature.size() * Math.sin(angle)); |
| 265 | ctx.stroke(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 266 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 267 | } |