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

50
LINES

< > BotCompany Repo | #1024813 // ChessOCR_TwoStageRecognizer

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

Libraryless. Click here for Pure Java version (7753L/52K).

// type parameter is rect + FEN
// not persistable
sclass ChessOCR_TwoStageRecognizer extends SteppableAndBest<Pair<Rect, S>> {
  replace Segmenter with IF1<BufferedImage, L<Rect>>.
  
  // input
  BufferedImage inputImage;
  Iterator<? extends Segmenter> segmenterStream;
  ChessPieceRecognizer pieceRecognizer;
  bool verbose;
  
  // internal
  L<Rect> rectQueue = syncLinkedList();
  new Set<Rect> rectsTried;

  *() {}
  *(ChessPieceRecognizer *pieceRecognizer, BufferedImage *inputImage, Iterator<? extends Segmenter> *segmenterStream) {}
  
  public bool step() {
    if (nempty(rectQueue))
      ret true with tryRect(popFirst(rectQueue));
    if (segmenterStream.hasNext()) {
      Segmenter seg = segmenterStream.next();
      if (seg == null) true;
      L<Rect> rects = seg.get(inputImage);
      addAll(rectQueue, rects);
      if (verbose) print("Segmenter " + seg + " returned: " + rects);
      true;
    }
    false;
  }
  
  // returns true if rect not seen before
  bool tryRect(Rect r) {
    r = rectSetHeightToWidth(r); // square it
    if (!rectsTried.add(r)) false; // seen
    BufferedImage board = clipBufferedImage(inputImage, r);
    S fen, double score = unpair chessOCR_recognizeBoard_FENAndScore(board, pieceRecognizer);
    if (verbose) print("Score " + score + " for rect: " + r);
    best.put(pair(r, fen), score);
    true;
  }
  
  void verbose(bool verbose) {
    best.verboseNewBest = this.verbose = verbose;
  }
  
  Rect bestBoardLocation() { ret pairA(best!); }
  Pair<Rect, Double> boardLocationAndScore() { ret pair(pairA(best!), score()); }
}

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: #1024813
Snippet name: ChessOCR_TwoStageRecognizer
Eternal ID of this version: #1024813/16
Text MD5: 5a74bac35875500579d30957d305e37e
Transpilation MD5: 59c279b13a4f47d0c60a41c7c79539d8
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-28 15:38:59
Source code size: 1634 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 310 / 771
Version history: 15 change(s)
Referenced in: #1024815 - ChessOCR_TwoStageRecognizer (old version with rect stream)
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)