Libraryless. Click here for Pure Java version (16304L/116K).
1 | abstract sclass ChessOCR_DynTestBoardFinders > DynSingleFunctionWithPrintLog { |
2 | replace Segmenter with IF1<BufferedImage, L<Rect>>. |
3 | |
4 | S scoreModule; |
5 | long maxMBs = 256; // save dat memory |
6 | transient L<Result> results = syncList(); |
7 | new Best<S> best; |
8 | |
9 | int triesAllowed = 10; // how many rects each segmenter may produce |
10 | |
11 | transient long dataSize; |
12 | transient new L<TestImage> images; |
13 | transient JProgressBar progressBar; |
14 | |
15 | // result for segmenter |
16 | srecord noeq Result( |
17 | S segmenterName, Segmenter segmenter, |
18 | double score, Map<TestResult, Double> individualScores) {} |
19 | |
20 | // individual test result |
21 | srecord noeq TestResult( |
22 | TestImage test, |
23 | L<Rect> rectsFound |
24 | ) {} |
25 | |
26 | srecord noeq TestImage(S caseName, BufferedImage img, Rect boardLocation) { |
27 | toString { ret caseName; } |
28 | } |
29 | |
30 | bool spaceToSpare() { ret toMB(dataSize) < maxMBs; } |
31 | |
32 | void doIt { |
33 | prepare(); |
34 | results.clear(); |
35 | dm_call(scoreModule, 'clear); |
36 | |
37 | Cl<S> presets = agiBlue_chessBoardSegmenterPresetNames(); |
38 | for ping (S preset : iterateUpdatingProgressBar(progressBar, presets)) |
39 | _testSegmenter(preset, parameterizedSegmenterFromAGIBlue(preset)); |
40 | } |
41 | |
42 | void prepare runInQAndWait { |
43 | if (empty(images)) loadImages(); |
44 | |
45 | if (!dm_moduleExists(scoreModule)) |
46 | setField(scoreModule := dm_showNewModule("#1024787/ScoreMatrix")); |
47 | else |
48 | dm_activateModule(scoreModule); |
49 | } |
50 | |
51 | Result scoreSegmenter(S name, Segmenter seg) { |
52 | Result result = new(name, seg, 0, new LinkedHashMap); |
53 | |
54 | try { |
55 | new Scorer scorer; |
56 | for (TestImage img : images) { |
57 | L<Rect> out = seg.get(img.img); |
58 | double tolerance = imageDiameter(img.img)/48; |
59 | double score2 = 0; |
60 | for (Rect r : takeFirst(triesAllowed, out)) |
61 | score2 = max(score2, video_scoreRect_score(img.boardLocation, r, tolerance)); |
62 | print("Score: " + score2); |
63 | scorer.addZeroToOne(score2); |
64 | result.individualScores.put(new TestResult(img, out), score2); |
65 | } |
66 | |
67 | print(scorer); |
68 | result.score = scorer.score(); |
69 | } catch print e { |
70 | print("SEGMENTER TOTAL FAIL"); |
71 | } |
72 | |
73 | ret result; |
74 | } |
75 | |
76 | void loadImages { |
77 | LPair<S, Rect> in = asLinkedList(agiBlue_chess_trainingImagesWithBoardLocations()); |
78 | while (nempty(in)) { |
79 | if (!spaceToSpare()) |
80 | break with print("Out of space (" + toM(dataSize) + " MB used), skipping " + nImages(in)); |
81 | S name, Rect location = unpair popFirst(in); |
82 | pcall { |
83 | BufferedImage img = loadImage2(parseImageSnippetURL(agiBlue_lookupInSlice(agiBlue_chessBoardRecognitionTrainingImagesSliceID(), name, "input image"))); |
84 | images.add(new TestImage(name, img, location)); |
85 | dataSize += bufferedImageDataSize(img); |
86 | print("Have " + nImages(images) + ", data size: " + toM(dataSize) + " MB"); |
87 | } |
88 | } |
89 | print("Loading done"); |
90 | } |
91 | |
92 | start { setFunctionName("Run tests"); } |
93 | |
94 | visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText())); |
95 | |
96 | void _testSegmenter(S name, Segmenter seg) { |
97 | prepare(); |
98 | Result r = scoreSegmenter(name, seg); |
99 | print("Score for " + name + ": " + r.score); |
100 | results.add(r); |
101 | if (best.put(name, r.score)) |
102 | print("NEW BEST!"); |
103 | change(); |
104 | dm_call(scoreModule, 'add, dm_call(scoreModule, 'newEntry, r.segmenterName, r.score, r.individualScores)); |
105 | } |
106 | |
107 | // API |
108 | |
109 | void testSegmenter(S name, virtual Segmenter _seg) { |
110 | Segmenter seg = img -> (L<Rect>) quickImport(callF(_seg, img)); |
111 | _testSegmenter(name, seg); |
112 | } |
113 | } |
Began life as a copy of #1024783
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: | #1024846 |
Snippet name: | ChessOCR_DynTestBoardFinders |
Eternal ID of this version: | #1024846/13 |
Text MD5: | 4857332b7104f000c8ee8e47ec56c97a |
Transpilation MD5: | 20976742d9c9e56d9a3a90ecf3c5ace8 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-08-29 13:18:09 |
Source code size: | 3675 bytes / 113 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 308 / 738 |
Version history: | 12 change(s) |
Referenced in: | [show references] |