Libraryless. Click here for Pure Java version (13448L/97K).
1 | abstract sclass ChessOCR_DynTestPieceRecognizers extends DynSingleFunctionWithPrintLog { |
2 | replace Recognizer with ChessPieceRecognizer. |
3 | |
4 | S scoreModule; |
5 | transient L<Result> results = syncList(); |
6 | new Best<S> best; |
7 | |
8 | transient new L<TestImage> images; |
9 | transient JProgressBar progressBar; |
10 | transient O onRecognition; // voidfunc(Recognizer, TestImage, Pair<S, Double> out) |
11 | |
12 | // result for recognizer |
13 | srecord noeq Result( |
14 | Recognizer recognizer, |
15 | double score, Map<TestResult, Double> individualScores) {} |
16 | |
17 | // individual test result |
18 | srecord noeq TestResult( |
19 | TestImage test, |
20 | S pieceFound, |
21 | double confidence |
22 | ) {} |
23 | |
24 | srecord noeq TestImage(S caseName, BufferedImage img, S piece) { |
25 | toString { ret caseName + ": " + piece; } |
26 | } |
27 | |
28 | void doIt { |
29 | prepare(); |
30 | results.clear(); |
31 | dm_call(scoreModule, 'clear); |
32 | testRecognizers(); |
33 | } |
34 | |
35 | // override me |
36 | void testRecognizers { |
37 | new ChessPieceRecognizer rec; |
38 | rec.version = 2; |
39 | rec.load(); |
40 | testRecognizer(rec); |
41 | testRecognizer(chessOCR_defaultPieceRecognizer()); |
42 | } |
43 | |
44 | void prepare { |
45 | if (empty(images)) loadImages(); |
46 | |
47 | if (!dm_moduleExists(scoreModule)) |
48 | setField(scoreModule := dm_showNewModule("#1024827/ScoreMatrix")); |
49 | else |
50 | dm_activateModule(scoreModule); |
51 | } |
52 | |
53 | Result scoreRecognizer(S name, Recognizer rec) { |
54 | Result result = new(rec, 0, new LinkedHashMap); |
55 | |
56 | try { |
57 | new Scorer scorer; |
58 | for (TestImage img : images) { |
59 | Pair<S, Double> out = rec.recognize(img.img); |
60 | callF(onRecognition, rec, img, out); |
61 | bool ok = eqic(pairA(out), img.piece); |
62 | scorer.add(ok); |
63 | result.individualScores.put(new TestResult(img, out.a, out.b), ok ? 1.0 : 0.0); |
64 | } |
65 | |
66 | print(scorer); |
67 | result.score = scorer.score(); |
68 | } catch print e { |
69 | print("RECOGNIZER TOTAL FAIL"); |
70 | } |
71 | |
72 | ret result; |
73 | } |
74 | |
75 | void loadImages { |
76 | for (unpair S piece, BufferedImage img : |
77 | //chessOCR_squareImagesCollectorFromAGIBlue().allImages() |
78 | map(p -> pair(p.b, loadImage2(p.a)), chessPieceImagesFromAGIBlue()) |
79 | ) { |
80 | images.add(new TestImage(md5OfBufferedImage(img), img, piece)); |
81 | } |
82 | print("Loading done"); |
83 | } |
84 | |
85 | start { setFunctionName("Run tests"); } |
86 | |
87 | visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText())); |
88 | |
89 | // API |
90 | |
91 | void testRecognizer(Recognizer rec) { |
92 | S name = str(rec); |
93 | prepare(); |
94 | long time = sysNow(); |
95 | Result r = scoreRecognizer(name, rec); |
96 | time = sysNow()-time; |
97 | print("Score for " + name + ": " + r.score); |
98 | results.add(r); |
99 | if (best.put(name, r.score)) |
100 | print("NEW BEST!"); |
101 | change(); |
102 | dm_call(scoreModule, 'add, dm_call(scoreModule, 'newEntry, |
103 | r.recognizer, r.score, renderElapsedTimePleasantly(time), r.individualScores)); |
104 | } |
105 | } |
Began life as a copy of #1024825
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1024838 |
Snippet name: | ChessOCR_DynTestPieceRecognizers |
Eternal ID of this version: | #1024838/6 |
Text MD5: | 736651ed9d7ae6acddbf264237ca2c76 |
Transpilation MD5: | 6dcb728373d51679643cbc058bac179e |
Author: | stefan |
Category: | javax / chess ocr |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-08-30 14:46:29 |
Source code size: | 2963 bytes / 105 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 273 / 647 |
Version history: | 5 change(s) |
Referenced in: | [show references] |