@scoriiu/fenshot 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/detect.js +51 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@ Screenshot in. FEN out.
|
|
|
6
6
|
[](https://www.npmjs.com/package/@scoriiu/fenshot)
|
|
7
7
|
[](https://github.com/scoriiu/fenshot/blob/main/LICENSE)
|
|
8
8
|
|
|
9
|
-
Live demo: **[
|
|
9
|
+
Live demo: **[fenshot.com](https://fenshot.com/)**
|
|
10
10
|
|
|
11
11
|
Chessboard recognition that runs entirely in the browser: finds the board in any screenshot (chess.com, Lichess, book diagrams, reddit lightboxes), reads the position with a CNN tile classifier, and returns a FEN with per-tile confidence. No server, no account, nothing leaves the page.
|
|
12
12
|
|
|
13
|
-
Extracted from the position-import feature of [coachess.app](https://coachess.app), where it runs in production.
|
|
13
|
+
Extracted from the position-import feature of [coachess.app](https://coachess.app?ref=fenshot-npm), where it runs in production.
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -108,7 +108,7 @@ The model is reproducible, not just downloadable: the full training pipeline (as
|
|
|
108
108
|
|
|
109
109
|
- Board detection algorithm: [Elucidation/tensorflow_chessbot](https://github.com/Elucidation/tensorflow_chessbot) (MIT).
|
|
110
110
|
- Piece set and board theme assets used as training input: lichess ([lila](https://github.com/lichess-org/lila), free licenses). The corpus also includes other sites' themes (including chess.com) so the recognizer reads their screenshots too; those assets are training input only and are never redistributed, only trained weights ship.
|
|
111
|
-
- Built and maintained by [coachess.app](https://coachess.app).
|
|
111
|
+
- Built and maintained by [coachess.app](https://coachess.app?ref=fenshot-npm).
|
|
112
112
|
|
|
113
113
|
## License
|
|
114
114
|
|
package/dist/detect.js
CHANGED
|
@@ -161,7 +161,16 @@ function median(arr) {
|
|
|
161
161
|
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
|
|
162
162
|
}
|
|
163
163
|
/** Crop (zero-padded out of bounds) + nearest resize to 64x64,
|
|
164
|
-
* correlate against ideal checkerboard kernel.
|
|
164
|
+
* correlate against ideal checkerboard kernel.
|
|
165
|
+
*
|
|
166
|
+
* SIGNED, deliberately: the kernel's +1 cells are the crop's corner
|
|
167
|
+
* parity, and a chessboard's corner squares (a8/h1 side) are the
|
|
168
|
+
* LIGHT squares in either orientation, so a correctly aligned crop
|
|
169
|
+
* correlates positive. A crop misaligned by an odd number of tiles
|
|
170
|
+
* inverts the parity and goes negative, which is what lets the
|
|
171
|
+
* arbitration reject one-tile-shifted boxes that abs() used to score
|
|
172
|
+
* as high as the truth (found via the marble-theme web-eval case:
|
|
173
|
+
* sparse position, confident read, every piece one file off). */
|
|
165
174
|
function checkerboardScore(img, x0, y0, x1, y1) {
|
|
166
175
|
const w = x1 - x0;
|
|
167
176
|
const h = y1 - y0;
|
|
@@ -182,7 +191,7 @@ function checkerboardScore(img, x0, y0, x1, y1) {
|
|
|
182
191
|
score += (parity * px) / 64;
|
|
183
192
|
}
|
|
184
193
|
}
|
|
185
|
-
return
|
|
194
|
+
return score;
|
|
186
195
|
}
|
|
187
196
|
/** How many candidate line sequences the one-axis reconstruction
|
|
188
197
|
* arbitrates between. Piece edges and adjacent UI lines can form a
|
|
@@ -336,6 +345,37 @@ function reconstructFromCandidates(img, candidates, axis) {
|
|
|
336
345
|
}
|
|
337
346
|
return best;
|
|
338
347
|
}
|
|
348
|
+
/** A negative checkerboard correlation on the chosen box means its
|
|
349
|
+
* parity is inverted: the grid was found but placed an odd number of
|
|
350
|
+
* tiles off (an outer edge displacing an inner line in the peak
|
|
351
|
+
* sequence shifts the whole box by one tile). The true box is then
|
|
352
|
+
* one tile away in one direction; try all four and keep the best
|
|
353
|
+
* positive. Found via the marble-theme web-eval case: sparse
|
|
354
|
+
* position over low-contrast texture, every piece read one file off
|
|
355
|
+
* at high confidence. */
|
|
356
|
+
function repairParity(img, box) {
|
|
357
|
+
const score = checkerboardScore(img, box.x0, box.y0, box.x1, box.y1);
|
|
358
|
+
if (score >= 0)
|
|
359
|
+
return box;
|
|
360
|
+
const tile = Math.round((box.x1 - box.x0) / 8);
|
|
361
|
+
let best = box;
|
|
362
|
+
let bestScore = score;
|
|
363
|
+
const shifts = [
|
|
364
|
+
[tile, 0],
|
|
365
|
+
[-tile, 0],
|
|
366
|
+
[0, tile],
|
|
367
|
+
[0, -tile],
|
|
368
|
+
];
|
|
369
|
+
for (const [sx, sy] of shifts) {
|
|
370
|
+
const c = { x0: box.x0 + sx, y0: box.y0 + sy, x1: box.x1 + sx, y1: box.y1 + sy };
|
|
371
|
+
const s = checkerboardScore(img, c.x0, c.y0, c.x1, c.y1);
|
|
372
|
+
if (s > bestScore) {
|
|
373
|
+
bestScore = s;
|
|
374
|
+
best = c;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return best;
|
|
378
|
+
}
|
|
339
379
|
export function findChessboardCorners(img) {
|
|
340
380
|
const gradY = gradientRows(img);
|
|
341
381
|
const gradX = gradientCols(img);
|
|
@@ -350,10 +390,14 @@ export function findChessboardCorners(img) {
|
|
|
350
390
|
// One axis clean, the other lost to low contrast / piece-edge noise:
|
|
351
391
|
// rebuild the square board from the good axis. snapCorners (called by
|
|
352
392
|
// the recognizer) then refines the alignment a few pixels either way.
|
|
353
|
-
if (linesX && !linesY)
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
393
|
+
if (linesX && !linesY) {
|
|
394
|
+
const r = reconstructFromCandidates(img, candidatesX, "x");
|
|
395
|
+
return r ? repairParity(img, r) : null;
|
|
396
|
+
}
|
|
397
|
+
if (linesY && !linesX) {
|
|
398
|
+
const r = reconstructFromCandidates(img, candidatesY, "y");
|
|
399
|
+
return r ? repairParity(img, r) : null;
|
|
400
|
+
}
|
|
357
401
|
if (!linesX || !linesY)
|
|
358
402
|
return null;
|
|
359
403
|
const dx = median(linesX.slice(1).map((v, i) => v - linesX[i]));
|
|
@@ -381,5 +425,5 @@ export function findChessboardCorners(img) {
|
|
|
381
425
|
}
|
|
382
426
|
}
|
|
383
427
|
}
|
|
384
|
-
return best;
|
|
428
|
+
return best ? repairParity(img, best) : null;
|
|
385
429
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scoriiu/fenshot",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Screenshot in, FEN out. Chessboard recognition in the browser: detects the board in any screenshot or book diagram and reads the position with a CNN tile classifier.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|