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

117
LINES

< > BotCompany Repo | #1026080 // Test Text Recognizers [v1, just texts]

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

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

1  
!7
2  
3  
cmodule TestTextRecognizers > DynSingleFunctionWithPrintLog {
4  
  replace Recognizer with IF1<BufferedImage, LS>.
5  
  
6  
  S scoreModule;
7  
  long maxMBs = 256; // save dat memory
8  
  transient L<Result> results = syncList();
9  
  new Best<S> best;
10  
  
11  
  transient long dataSize;
12  
  transient new L<TestImage> images;
13  
  transient JProgressBar progressBar;
14  
  
15  
  // result for recognizer
16  
  srecord noeq Result(
17  
    S recognizerName, Recognizer recognizer,
18  
    double score, Map<TestResult, Double> individualScores) {}
19  
    
20  
  // individual test result
21  
  srecord noeq TestResult(
22  
    TestImage test,
23  
    TreeSet<S> linesFound
24  
  ) {}
25  
  
26  
  srecord noeq TestImage(S caseName, BufferedImage img, TreeSet<S> expectedLines) {
27  
    toString { ret caseName; }
28  
  }
29  
  
30  
  bool spaceToSpare() { ret toMB(dataSize) < maxMBs; }
31  
32  
  void doIt {
33  
    prepare();
34  
    results.clear();
35  
    dm_rcall clear(scoreModule);
36  
37  
    _testRecognizer('ocr_recognizeMultiLine, lambda1 ocr_recognizeMultiLine);
38  
  }
39  
  
40  
  void prepare runInQAndWait {
41  
    if (empty(images)) loadImages();
42  
    
43  
    if (!dm_moduleExists(scoreModule))
44  
      setField(scoreModule := dm_showNewModule("#1024823/ScoreMatrix"));
45  
    else
46  
      dm_activateModule(scoreModule);
47  
  }
48  
49  
  Result scoreRecognizer(S name, Recognizer seg) {
50  
    Result result = new(name, seg, 0, new LinkedHashMap);
51  
    
52  
    try {
53  
      new Scorer scorer;
54  
      for (TestImage img : images) {
55  
        LS out = seg.get(img.img);
56  
        TreeSet<S> outSet = new(out);
57  
        Set<S> found = setIntersection(outSet, img.expectedLines);
58  
        //Set<S> extra = setMinusSet(outSet, img.expectedLines);
59  
        double score = doubleRatio(l(found), l(img.expectedLines));
60  
        print("Score: " + score);
61  
        scorer.addZeroToOne(score);
62  
        result.individualScores.put(new TestResult(img, outSet), score);
63  
      }
64  
    
65  
      print(scorer);
66  
      result.score = scorer.score();
67  
    } catch print e {
68  
      print("RECOGNIZER TOTAL FAIL");
69  
    }
70  
    
71  
    ret result;
72  
  }
73  
  
74  
  void loadImages {
75  
    File dir = javaxDataDir("Screen shots for text recognition");
76  
    L<File> in = asLinkedList(listFilesWithExtension(".expectedlines", dir));
77  
    while (nempty(in)) {
78  
      if (!spaceToSpare())
79  
        break with print("Out of space (" + toM(dataSize) + " MB used), skipping " + nImages(in));
80  
      
81  
      File f = popFirst(in);
82  
      LS lines = quotedOnly_unquote(tlft(loadTextFile(f)));
83  
      if (empty(lines)) continue;
84  
      File fImg = imageFileWithSameBaseName(f);
85  
      if (fImg == null) continue;
86  
      pcall {
87  
        BufferedImage img = loadImage2(fImg);
88  
        images.add(new TestImage(fileName(fImg), img, new TreeSet(lines)));
89  
        dataSize += bufferedImageDataSize(img);
90  
        print("Have " + nImages(images) + ", data size: " + toM(dataSize) + " MB");
91  
      }
92  
    }
93  
    print("Loading done");
94  
  }
95  
  
96  
  start { setFunctionName("Run tests"); }
97  
  
98  
  visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText()));
99  
  
100  
  void _testRecognizer(S name, Recognizer rec) {
101  
    prepare();
102  
    Result r = scoreRecognizer(name, rec);
103  
    print("Score for " + name + ": " + r.score);
104  
    results.add(r);
105  
    if (best.put(name, r.score))
106  
      print("NEW BEST!");
107  
    change();
108  
    dm_call(scoreModule, 'add,
109  
      dm_call(scoreModule, 'newEntry, r.recognizerName, r.score, r.individualScores));
110  
  }
111  
  
112  
  // API
113  
  
114  
  void testRecognizer(S name, virtual Recognizer _rec) {
115  
    _testRecognizer(name, img -> (LS) quickImport(callF(_rec, img)));
116  
  }
117  
}

Author comment

Began life as a copy of #1024846

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: #1026080
Snippet name: Test Text Recognizers [v1, just texts]
Eternal ID of this version: #1026080/14
Text MD5: e29e5525f2833df05a0bc30bd3e410ee
Transpilation MD5: 3633949dae9fc3d6ecc9243c300589bc
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-19 21:45:57
Source code size: 3590 bytes / 117 lines
Pitched / IR pitched: No / No
Views / Downloads: 221 / 330
Version history: 13 change(s)
Referenced in: [show references]