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

89
LINES

< > BotCompany Repo | #1030512 // Microsecond Line Finder v1 [OK, works 99.9% of the time]

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

Uses 1059K of libraries. Click here for Pure Java version (11318L/61K).

!7

set flag BWIntegralImage_brightnessCheat.

cmodule2 IILineFinder > DynImageSurfaceAndEnabled {
  switchable int imageSize = 1000;
  switchable int warmups = 10; // per round
  switchable int initialWarmups = 100000;
  ExpectedAndActual<Pair<Pt>> lastError;
  
  transient Pair<Pt> actual, recognized;
  transient BWIntegralImage ii;
  transient ImageSurface isReconstructed;
  transient S status, errorStatus;
  transient new Scorer scorer;
  transient new Average conversionTime, recTime;
  transient NotTooOften clearRecTime = nto_everyTenSeconds();
  transient long rounds;
  
  visualize {
    JComponent c = centerAndSouthWithMargin(
      jhgrid(jCenteredSection_fontSizePlus(10, "Input", super.visualize()),
        jCenteredSection_fontSizePlus(10, "Recognition", jscroll_centered(isReconstructed = doubleBufferedImageSurface()))),
      jvstack(fontSizePlus(10, dm_centeredLabel status()), fontSizePlus(5, dm_centeredLabel errorStatus())));
    enableDoubleBuffering();
    ret c;
  }

  start {
    thread "Calc" enter {
      while (true) {
        if (deleted()) ret;
        if (!enabled) continue with sleepSeconds(1);
        pcall {
          if (clearRecTime!) {
            recTime.clear();
            conversionTime.clear();
          }
          makeImage();
          recognize();
          score();
          reconstruct();
          status();
        }
      }
    }
  }
  
  void makeImage {
    Pt a = randomPt(imageSize), b = randomPt(imageSize);
    LineWithColor line = new(a, b, Color.black, 1);
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, line));
    setImage(img);
    long time = nanoTime();
    ii = new BWIntegralImage(img);
    conversionTime.add(nanoTime()-time);
    actual = sortedPair(a, b);
  }
  
  void recognize {
    int w = rounds == 0 ? initialWarmups : warmups;
    ++rounds;
    repeat w { bwii_detectSingleLine(ii); }
    long time = nanoTime();
    Pair<Pt> output = bwii_detectSingleLine(ii);
    recTime.add(nanoTime()-time);
    recognized = sortedPair(output);
  }
  
  void score {
    bool ok = eq(actual, recognized);
    scorer.add(ok);
    if (!ok) setField(lastError := ExpectedAndActual(actual, recognized));
  }
  
  void reconstruct {
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, LineWithColor(actual.a, actual.b, Color.black, 1)));
    isReconstructed.setImage(img);
  }
  
  void status {
    setFields(
      status := joinNemptiesWithComma(
        "Image size: " + toMillion(sqr(imageSize), 1) + " MP, conversion time: " + n2_iround(conversionTime!) + " ns",
        "recognition time: " + n2_iround(recTime!) + " ns",
        scorer),
      errorStatus := lastError == null ?: "last error: " + lastError);
  }
}

Author comment

Began life as a copy of #1019834

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030512
Snippet name: Microsecond Line Finder v1 [OK, works 99.9% of the time]
Eternal ID of this version: #1030512/38
Text MD5: 347377cf5bb27556226fc51e4d1b41c3
Transpilation MD5: 0574d444aa6277b805052be0e9b8db68
Author: stefan
Category: javax / image recognition
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-05 15:55:16
Source code size: 2892 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 223 / 1200
Version history: 37 change(s)
Referenced in: #1030524 - Microsecond Line Finder v1, JDK 16 version with vector API