@scoriiu/fenshot 0.1.3 → 0.1.5
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/LICENSE +1 -1
- package/README.md +3 -3
- package/dist/recognize.d.ts +15 -6
- package/dist/recognize.js +28 -10
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2026 SORTINO LABS S.R.L. (
|
|
3
|
+
Copyright (c) 2026 SORTINO LABS S.R.L. (kibitzo.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
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 [
|
|
13
|
+
Extracted from the position-import feature of [kibitzo.com](https://kibitzo.com?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 [
|
|
111
|
+
- Built and maintained by [kibitzo.com](https://kibitzo.com?ref=fenshot-npm).
|
|
112
112
|
|
|
113
113
|
## License
|
|
114
114
|
|
package/dist/recognize.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ export declare const CONFIDENCE_FLOOR = 0.7;
|
|
|
26
26
|
export interface BoardScanResult extends RecognitionResult {
|
|
27
27
|
corners: BoardCorners;
|
|
28
28
|
reliable: boolean;
|
|
29
|
+
/** True when the read satisfies the one invariant every real chess
|
|
30
|
+
* position has: exactly one king per side. Detector false positives
|
|
31
|
+
* on board-free UI (settings pages, card grids) typically read as
|
|
32
|
+
* sparse kingless scatters; callers doing automatic whole-page scans
|
|
33
|
+
* should treat implausible reads as "no board found". */
|
|
34
|
+
plausible: boolean;
|
|
29
35
|
}
|
|
30
36
|
export interface RecognizerOptions {
|
|
31
37
|
/** URL of the tile classifier model (`chess-tiles-v2.onnx`, shipped
|
|
@@ -57,12 +63,15 @@ export type TileClassifier = (corners: BoardCorners) => Promise<RecognitionResul
|
|
|
57
63
|
* the classifier is injected. Exercised directly by the golden test
|
|
58
64
|
* suite and usable from Node with any rasterizer.
|
|
59
65
|
*
|
|
60
|
-
* An
|
|
61
|
-
* has no position to analyze, and
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
+
* An implausible read is unlikely to be the board the user means: an
|
|
67
|
+
* empty board has no position to analyze, and a kingless scatter is
|
|
68
|
+
* usually the detector locking onto board-free UI (whole-screen
|
|
69
|
+
* captures can include decorative boards, placeholder boards, and
|
|
70
|
+
* card grids that look like boards). When a pass reads implausible,
|
|
71
|
+
* the region is masked and the scan repeats, preferring a plausible
|
|
72
|
+
* board found later. The best implausible read is still returned when
|
|
73
|
+
* nothing better exists, preferring piece-bearing reads over empty
|
|
74
|
+
* ones; callers decide how much to trust it via `plausible`.
|
|
66
75
|
*/
|
|
67
76
|
export declare function recognizeGray(gray: GrayImage, classify: TileClassifier): Promise<BoardScanResult | null>;
|
|
68
77
|
export declare function createRecognizer(options: RecognizerOptions): Recognizer;
|
package/dist/recognize.js
CHANGED
|
@@ -44,6 +44,17 @@ function imageToGray(img) {
|
|
|
44
44
|
return rgbaToGray(data.data, cw, ch);
|
|
45
45
|
}
|
|
46
46
|
const EMPTY_PLACEMENT = "8/8/8/8/8/8/8/8";
|
|
47
|
+
function isPlausiblePosition(placement) {
|
|
48
|
+
let white = 0;
|
|
49
|
+
let black = 0;
|
|
50
|
+
for (const ch of placement) {
|
|
51
|
+
if (ch === "K")
|
|
52
|
+
white++;
|
|
53
|
+
else if (ch === "k")
|
|
54
|
+
black++;
|
|
55
|
+
}
|
|
56
|
+
return white === 1 && black === 1;
|
|
57
|
+
}
|
|
47
58
|
/** Detection passes per image: the first board found can be an empty
|
|
48
59
|
* decorative board (a page can contain several board-like regions,
|
|
49
60
|
* including this library's own UI); masking it and rescanning finds
|
|
@@ -70,6 +81,7 @@ async function scanOnce(gray, classify) {
|
|
|
70
81
|
...best,
|
|
71
82
|
corners: bestCorners,
|
|
72
83
|
reliable: best.minConfidence >= CONFIDENCE_FLOOR,
|
|
84
|
+
plausible: isPlausiblePosition(best.placement),
|
|
73
85
|
};
|
|
74
86
|
}
|
|
75
87
|
/** Flatten a region so its gradients vanish and the detector cannot
|
|
@@ -91,26 +103,32 @@ function maskRegion(gray, box) {
|
|
|
91
103
|
* the classifier is injected. Exercised directly by the golden test
|
|
92
104
|
* suite and usable from Node with any rasterizer.
|
|
93
105
|
*
|
|
94
|
-
* An
|
|
95
|
-
* has no position to analyze, and
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
106
|
+
* An implausible read is unlikely to be the board the user means: an
|
|
107
|
+
* empty board has no position to analyze, and a kingless scatter is
|
|
108
|
+
* usually the detector locking onto board-free UI (whole-screen
|
|
109
|
+
* captures can include decorative boards, placeholder boards, and
|
|
110
|
+
* card grids that look like boards). When a pass reads implausible,
|
|
111
|
+
* the region is masked and the scan repeats, preferring a plausible
|
|
112
|
+
* board found later. The best implausible read is still returned when
|
|
113
|
+
* nothing better exists, preferring piece-bearing reads over empty
|
|
114
|
+
* ones; callers decide how much to trust it via `plausible`.
|
|
100
115
|
*/
|
|
101
116
|
export async function recognizeGray(gray, classify) {
|
|
102
117
|
let working = gray;
|
|
103
|
-
let
|
|
118
|
+
let fallback = null;
|
|
104
119
|
for (let pass = 0; pass < MAX_SCAN_PASSES; pass++) {
|
|
105
120
|
const result = await scanOnce(working, classify);
|
|
106
121
|
if (!result)
|
|
107
122
|
break;
|
|
108
|
-
if (result.
|
|
123
|
+
if (result.plausible)
|
|
109
124
|
return result;
|
|
110
|
-
|
|
125
|
+
const fallbackIsEmpty = !fallback || fallback.placement === EMPTY_PLACEMENT;
|
|
126
|
+
if (fallbackIsEmpty && (!fallback || result.placement !== EMPTY_PLACEMENT)) {
|
|
127
|
+
fallback = result;
|
|
128
|
+
}
|
|
111
129
|
working = maskRegion(working, result.corners);
|
|
112
130
|
}
|
|
113
|
-
return
|
|
131
|
+
return fallback;
|
|
114
132
|
}
|
|
115
133
|
export function createRecognizer(options) {
|
|
116
134
|
let ortPromise = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scoriiu/fenshot",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"onnx",
|
|
46
46
|
"lichess"
|
|
47
47
|
],
|
|
48
|
-
"author": "
|
|
48
|
+
"author": "Kibitzo (https://kibitzo.com)",
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"repository": {
|
|
51
51
|
"type": "git",
|