Files
@ a8281a147cb6
Branch filter:
Location: light9/web/calibrate/Light9Camera.ts - annotation
a8281a147cb6
6.2 KiB
video/MP2T
checkpoint show data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a 9bb0eb587d5b 9bb0eb587d5b ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a d5750b2aaa9e ae4b90efb55a ae4b90efb55a ae4b90efb55a d5750b2aaa9e d5750b2aaa9e d5750b2aaa9e d5750b2aaa9e ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a d5750b2aaa9e ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a e3af0ac507c8 ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a d5750b2aaa9e ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a d5750b2aaa9e ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a ae4b90efb55a | import debug from "debug";
import { css, html, LitElement, PropertyValueMap, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
@customElement("light9-camera")
export class Light9Camera extends LitElement {
static styles = [
css`
:host {
display: flex;
}
#video {
display: none;
}
#stack {
position: relative;
width: 640px;
height: 480px;
}
#stack > * {
position: absolute;
left: 0;
top: 0;
}
#stack > :first-child {
position: static;
}
#stack > img {
opacity: 0;
animation: fadeIn 1s 1s ease-in-out forwards;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
`,
];
videoEl!: HTMLVideoElement;
canvas!: HTMLCanvasElement;
ctx!: CanvasRenderingContext2D;
@state()
vtrack: MediaStreamTrack | undefined;
@property() saturatedPixelCount = 0;
@property() saturatedPixelFraction = 0;
@property() videoSettings: MediaTrackSettings & any = {};
render() {
const saturatedCountDisplay = `${this.saturatedPixelCount} (${(this.saturatedPixelFraction * 100).toFixed(2)}%)`;
return html`
<video id="video"></video>
<div id="stack">
<img src="zebra.png" />
<canvas id="canvas"></canvas>
</div>
<div id="controls">
<p>saturated pixels: ${saturatedCountDisplay}</p>
<light9-camera-settings-table .cam=${this} .videoSettings=${this.videoSettings}></light9-camera-settings-table>
</div>
`;
}
protected async firstUpdated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>) {
this.videoEl = this.shadowRoot!.getElementById("video") as HTMLVideoElement;
this.canvas = this.shadowRoot!.getElementById("canvas") as HTMLCanvasElement;
this.ctx = this.canvas.getContext("2d", { willReadFrequently: true })!;
const constraints: MediaStreamConstraints = {
video: {
facingMode: { ideal: "environment" },
frameRate: { max: 10 },
width: 640,
height: 480,
},
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
const t = stream.getVideoTracks()[0];
await t.applyConstraints({
brightness: 0,
contrast: 32,
colorTemperature: 4600,
exposureMode: "manual",
exposureTime: 250,
whiteBalanceMode: "manual",
// this could stop focus from moving around, but it also makes my cam
// click on every page reload
// focusMode: "manual",
// focusDistance: 235,
} as MediaTrackConstraints);
this.vtrack = t;
this.videoEl.srcObject = stream;
this.videoEl.play();
this.videoSettings = this.vtrack.getSettings();
this.redrawLoop();
}
redrawLoop() {
if (this.videoEl.videoWidth !== 0 && this.videoEl.videoHeight !== 0) {
this.redraw();
}
// todo: video frames come slower than raf is waiting
requestAnimationFrame(this.redrawLoop.bind(this));
}
public async set(k: string, v: any) {
if (!this.vtrack) {
throw new Error("vtrack");
}
await this.vtrack.applyConstraints({ [k]: v });
this.videoSettings = this.vtrack.getSettings();
}
private redraw() {
this.canvas.width = this.videoEl.videoWidth;
this.canvas.height = this.videoEl.videoHeight;
this.ctx.drawImage(this.videoEl, 0, 0);
this.makeSaturatedPixelsTransparent();
}
private makeSaturatedPixelsTransparent() {
const imageData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
const data = imageData.data;
this.saturatedPixelCount = 0;
for (let i = 0; i < data.length; i += 4) {
if (data[i] === 255 || data[i + 1] === 255 || data[i + 2] === 255) {
this.saturatedPixelCount += 1;
data[i + 3] = 0;
}
}
this.saturatedPixelFraction = this.saturatedPixelCount / (data.length / 4);
this.ctx.putImageData(imageData, 0, 0);
}
}
@customElement("light9-camera-settings-table")
export class Light9CameraSettingsTable extends LitElement {
static styles = [
css`
table {
border-collapse: collapse;
}
td {
border: 1px solid gray;
padding: 1px 6px;
}
`,
];
boring = [
"autoGainControl",
"aspectRatio",
"backgroundBlur",
"channelCount",
"deviceId",
"displaySurface",
"echoCancellation",
"eyeGazeCorrection",
"faceFraming",
"groupId",
"latency",
"noiseSuppression",
"pointsOfInterest",
"resizeMode",
"sampleRate",
"sampleSize",
"suppressLocalAudioPlayback",
"torch",
"voiceIsolation",
];
adjustable: Record<string, { min: string; max: string }> = {
focusDistance: { min: "0", max: "1023" },
brightness: { min: "0", max: "64" },
colorTemperature: { min: "2800", max: "6500" },
exposureTime: { min: "0", max: "800" },
};
@property() cam!: Light9Camera;
@property() videoSettings: MediaTrackSettings & any = {};
supportedByBrowser: MediaTrackSupportedConstraints;
constructor() {
super();
this.supportedByBrowser = navigator.mediaDevices.getSupportedConstraints();
}
render() {
const rows: TemplateResult<1>[] = [];
for (const key of Object.keys(this.supportedByBrowser)) {
if (!this.boring.includes(key)) {
this.renderRow(key, rows);
}
}
return html`<table>
${rows}
</table>`;
}
private renderRow(key: string, out: TemplateResult<1>[]) {
let valueDisplay = "";
if (this.videoSettings[key] !== undefined) {
valueDisplay = JSON.stringify(this.videoSettings[key]);
}
let adjuster = html``;
let conf = this.adjustable[key];
if (conf !== undefined) {
adjuster = html`
<input type="range" min="${conf.min}" max="${conf.max}" value="${this.videoSettings[key]}" data-param="${key}" @input=${this.setFromSlider} />
`;
}
out.push(
html`<tr>
<td>${key}</td>
<td>${valueDisplay}</td>
<td>${adjuster}</td>
</tr>`
);
}
async setFromSlider(ev: InputEvent) {
const el = ev.target as HTMLInputElement;
await this.cam.set(el.dataset.param as string, parseFloat(el.value));
}
}
|