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

96
LINES

< > BotCompany Repo | #1030567 // Triangle Finder v1 [dev.]

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

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

!7

set flag BWIntegralImage_brightnessCheat.
//set flag BWIntegralImage_useVectorAPI.

cmodule2 IITriangleFinder > DynImageSurfaceAndEnabled {
  switchable int imageSize = 512;
  switchable int lineWidth = 5;
  switchable int warmups = 10; // per round
  switchable int initialWarmups = 100000;
  switchable int stripes = 2; // how many threads to use for making integral image
  ExpectedAndActual<L<Pt>> lastError;
  
  transient L<Pt> actual, recognized;
  transient IBWIntegralImage 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;
  transient autoDispose ThreadPoolExecutor executor = defaultThreadPoolExecutor();
  
  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 {
    L<Pt> points = repF(3, () -> randomPt(shrinkRect(0, 0, imageSize, imageSize, 3)));
    L<LineWithColor> lines = mapPairsToList(overlappingPairs_cyclic(points),
      (a, b) -> LineWithColor(a, b, Color.black, lineWidth));
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, lines));
    setImage(img);
    long time = nanoTime();
    ii = stripedBWIntegralImage(executor, stripes, img);
    conversionTime.add(nanoTime()-time);
    actual = points;
  }
  
  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 {
    L<LineWithColor> lines = mapPairsToList(overlappingPairs_cyclic(actual),
      (a, b) -> LineWithColor(a, b, Color.black, lineWidth));
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, lines));
    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 #1030524

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

-
Snippet ID: #1030567
Snippet name: Triangle Finder v1 [dev.]
Eternal ID of this version: #1030567/5
Text MD5: cbcda5ed3ca33b0314c07fd498623a3e
Transpilation MD5: 13e05139db4777d8647b581a85d6fdb5
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-07 19:46:31
Source code size: 3367 bytes / 96 lines
Pitched / IR pitched: No / No
Views / Downloads: 184 / 929
Version history: 4 change(s)
Referenced in: