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

1  
!7
2  
3  
set flag BWIntegralImage_brightnessCheat.
4  
//set flag BWIntegralImage_useVectorAPI.
5  
6  
cmodule2 IITriangleFinder > DynImageSurfaceAndEnabled {
7  
  switchable int imageSize = 512;
8  
  switchable int lineWidth = 5;
9  
  switchable int warmups = 10; // per round
10  
  switchable int initialWarmups = 100000;
11  
  switchable int stripes = 2; // how many threads to use for making integral image
12  
  ExpectedAndActual<L<Pt>> lastError;
13  
  
14  
  transient L<Pt> actual, recognized;
15  
  transient IBWIntegralImage ii;
16  
  transient ImageSurface isReconstructed;
17  
  transient S status, errorStatus;
18  
  transient new Scorer scorer;
19  
  transient new Average conversionTime, recTime;
20  
  transient NotTooOften clearRecTime = nto_everyTenSeconds();
21  
  transient long rounds;
22  
  transient autoDispose ThreadPoolExecutor executor = defaultThreadPoolExecutor();
23  
  
24  
  visualize {
25  
    JComponent c = centerAndSouthWithMargin(
26  
      jhgrid(jCenteredSection_fontSizePlus(10, "Input", super.visualize()),
27  
        jCenteredSection_fontSizePlus(10, "Recognition", jscroll_centered(isReconstructed = doubleBufferedImageSurface()))),
28  
      jvstack(fontSizePlus(10, dm_centeredLabel status()), fontSizePlus(5, dm_centeredLabel errorStatus())));
29  
    enableDoubleBuffering();
30  
    ret c;
31  
  }
32  
33  
  start {
34  
    thread "Calc" enter {
35  
      while (true) {
36  
        if (deleted()) ret;
37  
        if (!enabled) continue with sleepSeconds(1);
38  
        pcall {
39  
          if (clearRecTime!) {
40  
            recTime.clear();
41  
            conversionTime.clear();
42  
          }
43  
          makeImage();
44  
          recognize();
45  
          score();
46  
          reconstruct();
47  
          status();
48  
        }
49  
      }
50  
    }
51  
  }
52  
  
53  
  void makeImage {
54  
    L<Pt> points = repF(3, () -> randomPt(shrinkRect(0, 0, imageSize, imageSize, 3)));
55  
    L<LineWithColor> lines = mapPairsToList(overlappingPairs_cyclic(points),
56  
      (a, b) -> LineWithColor(a, b, Color.black, lineWidth));
57  
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, lines));
58  
    setImage(img);
59  
    long time = nanoTime();
60  
    ii = stripedBWIntegralImage(executor, stripes, img);
61  
    conversionTime.add(nanoTime()-time);
62  
    actual = points;
63  
  }
64  
  
65  
  void recognize {
66  
    int w = rounds == 0 ? initialWarmups : warmups;
67  
    ++rounds;
68  
    //repeat w { bwii_detectSingleLine(ii); }
69  
    long time = nanoTime();
70  
    //Pair<Pt> output = bwii_detectSingleLine(ii);
71  
    recTime.add(nanoTime()-time);
72  
    //recognized = sortedPair(output);
73  
  }
74  
  
75  
  void score {
76  
    bool ok = eq(actual, recognized);
77  
    scorer.add(ok);
78  
    if (!ok) setField(lastError := ExpectedAndActual(actual, recognized));
79  
  }
80  
  
81  
  void reconstruct {
82  
    L<LineWithColor> lines = mapPairsToList(overlappingPairs_cyclic(actual),
83  
      (a, b) -> LineWithColor(a, b, Color.black, lineWidth));
84  
    BufferedImage img = renderBackgroundPlusLines(imageSize, imageSize, new BackgroundPlus(Color.white, lines));
85  
    isReconstructed.setImage(img);
86  
  }
87  
  
88  
  void status {
89  
    setFields(
90  
      status := joinNemptiesWithComma(
91  
        "Image size: " + toMillion(sqr(imageSize), 1) + " MP, conversion time: " + n2_iround(conversionTime!) + " ns",
92  
        "recognition time: " + n2_iround(recTime!) + " ns",
93  
        scorer),
94  
      errorStatus := lastError == null ?: "last error: " + lastError);
95  
  }
96  
}

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: 185 / 930
Version history: 4 change(s)
Referenced in: [show references]