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).

!7

cmodule TestWordRecognizers > DynSingleFunctionWithPrintLog {
  S scoreModule;
  long maxMBs = 256; // save dat memory
  transient L<Result> results = syncList();
  new Best<S> best;
  
  transient int maxDiff = 2;
  
  transient long dataSize;
  transient new L<TestImage> images;
  transient JProgressBar progressBar;
  transient Map<S, BufferedImage> preloadedImages;
  
  srecord Recognizer(S name, IF1<BufferedImage, S> function, long size) {
    toString { ret name; }
  }
  
  // result for recognizer
  srecord noeq Result(
    Recognizer recognizer,
    double score, Map<TestResult, Double> individualScores) {}
    
  // individual test result
  srecord noeq TestResult(
    TestImage test,
    S line,
    long time
  ) {
    void showDetails { activateFrame(quickShowImage(test.img)); }
    
    Map<S, O> extraColumns() {
      ret litorderedmap("Image" := test.img);
    }
  }
  
  srecord noeq TestImage(S caseName, BufferedImage img, S interpretation) {
    toString { ret caseName + ": " + interpretation; }
  }
  
  bool spaceToSpare() { ret toMB(dataSize) < maxMBs; }

  void doIt {
    prepare();
    results.clear();
    dm_rcall clear(scoreModule);

    SimpleRecognizer r = simpleRecognizer();
    testRecognizer("simpleRecognizer", r);
    
    r = simpleRecognizer();
    r.wordImagePreprocessor = lambda1 bwAutoContrast;
    testRecognizer("simpleRecognizer autoContrast", r);
  }
  
  void testRecognizer(S desc, SimpleRecognizer r) {
    testRecognizer(new Recognizer(
      desc + " (" + r.sizeStats() + ")",
      img -> r.recognize(BWImage(img)),
      guessObjectSize(r)));
  }
  
  void prepare runInQAndWait {
    if (empty(images)) loadImages();
    setField(scoreModule := dm_loadOrActivateScoreMatrixModule(scoreModule));
  }

  Result scoreRecognizer(Recognizer rec) {
    Result result = new(rec, 0, new LinkedHashMap);
    
    try {
      new Scorer scorer;
      for (TestImage img : images) {
        long time = sysNow();
        S out = rec.function.get(img.img);
        time = sysNow()-time;
        int diff = leven_limited(img.interpretation, out, maxDiff);
        //double score = eq(out, img.groupedChars) ? 1 : 0; // TODO
        double score = 1.0-doubleRatio(diff, min(l(img.interpretation), maxDiff));
        scorer.addZeroToOne(score);
        result.individualScores.put(new TestResult(img, out, time), score);
      }
    
      print(scorer);
      result.score = scorer.score();
    } catch print e {
      print("RECOGNIZER TOTAL FAIL");
    }
    
    ret result;
  }
  
  void loadImages {
    File dir = screenClipsDir();
    L<File> in = asLinkedList(listFilesWithExtension(".line", dir));
    while (nempty(in)) {
      if (!spaceToSpare())
        break with print("Out of space (" + toM(dataSize) + " MB used), skipping " + nImages(in));
      
      File f = popFirst(in);
      File fImg = replaceExtension(f, ".png");
      if (!fileExists(fImg)) continue;
      S desc = firstLineOfFile(f);
      new Matches m;
      if (!find3("the images * are the grouped characters *", desc, m)) continue;
      S interpretation = dropSpaces($2); // why drop spaces?
      interpretation = join(map deSquareBracket(ocr_parseGlyphs(interpretation));
      //LS characters = ocr_parseGlyphs(interpretation);
      pcall {
        BufferedImage img = or_func(mapGet(preloadedImages, fileName(fImg)), () -> loadImage2(fImg));
        images.add(new TestImage(fileName(fImg), img, interpretation);
        dataSize += bufferedImageDataSize(img);
        print("Have " + nImages(images) + ", data size: " + toM(dataSize) + " MB");
      }
    }
    print("Loading done");
  }
  
  start { setFunctionName("Run tests"); }
  
  visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText()));
  
  void testRecognizer(Recognizer rec) {
    prepare();
    Result r = scoreRecognizer(rec);
    print("Score for " + rec + ": " + r.score);
    results.add(r);
    if (best.put(name, r.score))
      print("NEW BEST!");
    change();
    dm_rcall add(scoreModule,
      dm_rcall newEntry(scoreModule, r.recognizer, r.score, r.individualScores));
  }
  
  Map<S, BufferedImage> _getReloadData() { ret mapFieldToField('caseName, 'img, images); }
  void _setReloadData(Map<S, BufferedImage> map) { preloadedImages = map; }
}

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: 334 / 657
Version history: 37 change(s)
Referenced in: