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

115
LINES

< > BotCompany Repo | #1026083 // Test Text Recognizers [v2, with rects, OK but can't show details yet]

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

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

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

Author comment

Began life as a copy of #1026080

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: #1026083
Snippet name: Test Text Recognizers [v2, with rects, OK but can't show details yet]
Eternal ID of this version: #1026083/9
Text MD5: b8d978124cdbd7245388c74234f73706
Transpilation MD5: 664796e271b0dc0f4de74a71b1e9cf0a
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 23:50:37
Source code size: 3638 bytes / 115 lines
Pitched / IR pitched: No / No
Views / Downloads: 199 / 337
Version history: 8 change(s)
Referenced in: [show references]