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

105
LINES

< > BotCompany Repo | #1024838 // ChessOCR_DynTestPieceRecognizers

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (13448L/97K).

abstract sclass ChessOCR_DynTestPieceRecognizers extends DynSingleFunctionWithPrintLog {
  replace Recognizer with ChessPieceRecognizer.
  
  S scoreModule;
  transient L<Result> results = syncList();
  new Best<S> best;
  
  transient new L<TestImage> images;
  transient JProgressBar progressBar;
  transient O onRecognition; // voidfunc(Recognizer, TestImage, Pair<S, Double> out)
  
  // result for recognizer
  srecord noeq Result(
    Recognizer recognizer,
    double score, Map<TestResult, Double> individualScores) {}
    
  // individual test result
  srecord noeq TestResult(
    TestImage test,
    S pieceFound,
    double confidence
  ) {}
  
  srecord noeq TestImage(S caseName, BufferedImage img, S piece) {
    toString { ret caseName + ": " + piece; }
  }
  
  void doIt {
    prepare();
    results.clear();
    dm_call(scoreModule, 'clear);
    testRecognizers();
  }
  
  // override me
  void testRecognizers {
    new ChessPieceRecognizer rec;
    rec.version = 2;
    rec.load();
    testRecognizer(rec);
    testRecognizer(chessOCR_defaultPieceRecognizer());
  }
  
  void prepare {
    if (empty(images)) loadImages();
    
    if (!dm_moduleExists(scoreModule))
      setField(scoreModule := dm_showNewModule("#1024827/ScoreMatrix"));
    else
      dm_activateModule(scoreModule);
  }

  Result scoreRecognizer(S name, Recognizer rec) {
    Result result = new(rec, 0, new LinkedHashMap);
    
    try {
      new Scorer scorer;
      for (TestImage img : images) {
        Pair<S, Double> out = rec.recognize(img.img);
        callF(onRecognition, rec, img, out);
        bool ok = eqic(pairA(out), img.piece);
        scorer.add(ok);
        result.individualScores.put(new TestResult(img, out.a, out.b), ok ? 1.0 : 0.0);
      }
    
      print(scorer);
      result.score = scorer.score();
    } catch print e {
      print("RECOGNIZER TOTAL FAIL");
    }
    
    ret result;
  }
  
  void loadImages {
    for (unpair S piece, BufferedImage img :
      //chessOCR_squareImagesCollectorFromAGIBlue().allImages()
      map(p -> pair(p.b, loadImage2(p.a)), chessPieceImagesFromAGIBlue())
    ) {
      images.add(new TestImage(md5OfBufferedImage(img), img, piece));
    }
    print("Loading done");
  }
  
  start { setFunctionName("Run tests"); }
  
  visual centerAndSouthWithMargin(super, withMargin(progressBar = jProgressBarWithText()));
  
  // API
  
  void testRecognizer(Recognizer rec) {
    S name = str(rec);
    prepare();
    long time = sysNow();
    Result r = scoreRecognizer(name, rec);
    time = sysNow()-time;
    print("Score for " + name + ": " + r.score);
    results.add(r);
    if (best.put(name, r.score))
      print("NEW BEST!");
    change();
    dm_call(scoreModule, 'add, dm_call(scoreModule, 'newEntry,
      r.recognizer, r.score, renderElapsedTimePleasantly(time), r.individualScores));
  }
}

Author comment

Began life as a copy of #1024825

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: #1024838
Snippet name: ChessOCR_DynTestPieceRecognizers
Eternal ID of this version: #1024838/6
Text MD5: 736651ed9d7ae6acddbf264237ca2c76
Transpilation MD5: 6dcb728373d51679643cbc058bac179e
Author: stefan
Category: javax / chess ocr
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-30 14:46:29
Source code size: 2963 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 271 / 643
Version history: 5 change(s)
Referenced in: [show references]