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

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

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: 151 / 1109
Version history: 37 change(s)
Referenced in: [show references]