Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

138
LINES

< > BotCompany Repo | #1026088 // Test Word Recognizers

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (11696L/64K).

1  
!7
2  
3  
cmodule TestWordRecognizers > DynSingleFunctionWithPrintLog {
4  
  S scoreModule;
5  
  long maxMBs = 256; // save dat memory
6  
  transient L<Result> results = syncList();
7  
  new Best<S> best;
8  
  
9  
  transient int maxDiff = 2;
10  
  
11  
  transient long dataSize;
12  
  transient new L<TestImage> images;
13  
  transient JProgressBar progressBar;
14  
  transient Map<S, BufferedImage> preloadedImages;
15  
  
16  
  srecord Recognizer(S name, IF1<BufferedImage, S> function, long size) {
17  
    toString { ret name; }
18  
  }
19  
  
20  
  // result for recognizer
21  
  srecord noeq Result(
22  
    Recognizer recognizer,
23  
    double score, Map<TestResult, Double> individualScores) {}
24  
    
25  
  // individual test result
26  
  srecord noeq TestResult(
27  
    TestImage test,
28  
    S line,
29  
    long time
30  
  ) {
31  
    void showDetails { activateFrame(quickShowImage(test.img)); }
32  
    
33  
    Map<S, O> extraColumns() {
34  
      ret litorderedmap("Image" := test.img);
35  
    }
36  
  }
37  
  
38  
  srecord noeq TestImage(S caseName, BufferedImage img, S interpretation) {
39  
    toString { ret caseName + ": " + interpretation; }
40  
  }
41  
  
42  
  bool spaceToSpare() { ret toMB(dataSize) < maxMBs; }
43  
44  
  void doIt {
45  
    prepare();
46  
    results.clear();
47  
    dm_rcall clear(scoreModule);
48  
49  
    SimpleRecognizer r = simpleRecognizer();
50  
    testRecognizer("simpleRecognizer", r);
51  
    
52  
    r = simpleRecognizer();
53  
    r.wordImagePreprocessor = lambda1 bwAutoContrast;
54  
    testRecognizer("simpleRecognizer autoContrast", r);
55  
  }
56  
  
57  
  void testRecognizer(S desc, SimpleRecognizer r) {
58  
    testRecognizer(new Recognizer(
59  
      desc + " (" + r.sizeStats() + ")",
60  
      img -> r.recognize(BWImage(img)),
61  
      guessObjectSize(r)));
62  
  }
63  
  
64  
  void prepare runInQAndWait {
65  
    if (empty(images)) loadImages();
66  
    setField(scoreModule := dm_loadOrActivateScoreMatrixModule(scoreModule));
67  
  }
68  
69  
  Result scoreRecognizer(Recognizer rec) {
70  
    Result result = new(rec, 0, new LinkedHashMap);
71  
    
72  
    try {
73  
      new Scorer scorer;
74  
      for (TestImage img : images) {
75  
        long time = sysNow();
76  
        S out = rec.function.get(img.img);
77  
        time = sysNow()-time;
78  
        int diff = leven_limited(img.interpretation, out, maxDiff);
79  
        //double score = eq(out, img.groupedChars) ? 1 : 0; // TODO
80  
        double score = 1.0-doubleRatio(diff, min(l(img.interpretation), maxDiff));
81  
        scorer.addZeroToOne(score);
82  
        result.individualScores.put(new TestResult(img, out, time), score);
83  
      }
84  
    
85  
      print(scorer);
86  
      result.score = scorer.score();
87  
    } catch print e {
88  
      print("RECOGNIZER TOTAL FAIL");
89  
    }
90  
    
91  
    ret result;
92  
  }
93  
  
94  
  void loadImages {
95  
    File dir = screenClipsDir();
96  
    L<File> in = asLinkedList(listFilesWithExtension(".line", dir));
97  
    while (nempty(in)) {
98  
      if (!spaceToSpare())
99  
        break with print("Out of space (" + toM(dataSize) + " MB used), skipping " + nImages(in));
100  
      
101  
      File f = popFirst(in);
102  
      File fImg = replaceExtension(f, ".png");
103  
      if (!fileExists(fImg)) continue;
104  
      S desc = firstLineOfFile(f);
105  
      new Matches m;
106  
      if (!find3("the images * are the grouped characters *", desc, m)) continue;
107  
      S interpretation = dropSpaces($2); // why drop spaces?
108  
      interpretation = join(map deSquareBracket(ocr_parseGlyphs(interpretation));
109  
      //LS characters = ocr_parseGlyphs(interpretation);
110  
      pcall {
111  
        BufferedImage img = or_func(mapGet(preloadedImages, fileName(fImg)), () -> loadImage2(fImg));
112  
        images.add(new TestImage(fileName(fImg), img, interpretation);
113  
        dataSize += bufferedImageDataSize(img);
114  
        print("Have " + nImages(images) + ", data size: " + toM(dataSize) + " MB");
115  
      }
116  
    }
117  
    print("Loading done");
118  
  }
119  
  
120  
  start { setFunctionName("Run tests"); }
121  
  
122  
  visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText()));
123  
  
124  
  void testRecognizer(Recognizer rec) {
125  
    prepare();
126  
    Result r = scoreRecognizer(rec);
127  
    print("Score for " + rec + ": " + r.score);
128  
    results.add(r);
129  
    if (best.put(name, r.score))
130  
      print("NEW BEST!");
131  
    change();
132  
    dm_rcall add(scoreModule,
133  
      dm_rcall newEntry(scoreModule, r.recognizer, r.score, r.individualScores));
134  
  }
135  
  
136  
  Map<S, BufferedImage> _getReloadData() { ret mapFieldToField('caseName, 'img, images); }
137  
  void _setReloadData(Map<S, BufferedImage> map) { preloadedImages = map; }
138  
}

Author comment

Began life as a copy of #1026083

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: #1026088
Snippet name: Test Word Recognizers
Eternal ID of this version: #1026088/38
Text MD5: e40cc0367a21a554ec36e9e0843db4c2
Transpilation MD5: 334b07ea55cfc6f55e78c0f318bd96ac
Author: stefan
Category: javax / ocr
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-21 22:20:08
Source code size: 4437 bytes / 138 lines
Pitched / IR pitched: No / No
Views / Downloads: 333 / 655
Version history: 37 change(s)
Referenced in: [show references]