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

64
LINES

< > BotCompany Repo | #1031966 // OneColorTheoryChecker [dev.]

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

Libraryless. Click here for Pure Java version (8317L/48K).

1  
sclass OneColorTheoryChecker {
2  
  replace Cell with URecognizer.Cell.
3  
  replace Theory with BodyOfEvidence<S>.
4  
  
5  
  new ProbabilisticScheduler scheduler;
6  
  
7  
  record OneColorTheory(S text, Cell cell, RGB color) > Theory {
8  
    toString {
9  
      ret renderProbabilityGuess() + ": " + dollarVarsMeanFields(text, this);
10  
    }
11  
    
12  
    selfType branch(O... _) { this; }
13  
  }
14  
  
15  
  // create the theory object and initiate the reasoning
16  
  // you have to step the scheduler afterwards to get results
17  
  OneColorTheory makeTheory(Cell cell) {
18  
    var color = cell.averageColor();
19  
20  
    var theory = new OneColorTheory("Color of every pixel in $cell is $color.", cell, color);
21  
  
22  
    testOneColorTheory(cell, theory);
23  
    ret theory;
24  
  }
25  
26  
  void noteCellColor(Cell cell, OneColorTheory theory) {
27  
    var desc = "Pixel check at " + cell;
28  
    if (theory.hasEvidenceFromSource(desc)) return;
29  
    var strength = doubleRatio(cell.area(), theory.cell.area()); // how much do we have to say about the cell in the theory?
30  
    var p = colorDistanceToProbability(cell.averageColor(), theory.color);
31  
    addExampleToTheory(theory, desc, p, strength);
32  
  }
33  
  
34  
  double colorDistanceToProbability(RGB col1, RGB col2) {
35  
    double sim = colorSimilarity(col1, col2), sqr = sqr(sim);
36  
    printFunctionCall colorDistanceToProbability(+col1, +col2, +sim, +sqr);
37  
    ret sqr;
38  
  }
39  
  
40  
  void testOneColorTheory(Cell cell, OneColorTheory theory) {  
41  
    noteCellColor(cell, theory);
42  
  
43  
    scheduler.atRelative(dontZoomTooFar(cell), r {
44  
      for (var split : usefulSplits(cell))
45  
        scheduler.atRelative(split.probability(), r {
46  
          for (var cell : split!)
47  
            testOneColorTheory(cell, theory.branch(description := "recursion to " + cell));
48  
        });
49  
    });
50  
  }
51  
  
52  
  L<WithProbability<Cell[]>> usefulSplits(Cell cell) {
53  
    ret map withProbability1(llNonNulls(cell.split(0, 2), cell.split(1, 2)));
54  
  }
55  
  
56  
  // probability penalty .25 for zooming in 3 steps
57  
  double dontZoomTooFar(Cell cell) {
58  
    ret 0.75;
59  
  }
60  
  
61  
  void addExampleToTheory(Theory theory, S desc, double probabilityOfTheory, double strengthOfEvidence) {
62  
    theory.addEvidence(desc, probabilityOfTheory, /* scheduler.currentProbability()* */ strengthOfEvidence);
63  
  }
64  
}

Author comment

Began life as a copy of #1031965

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031966
Snippet name: OneColorTheoryChecker [dev.]
Eternal ID of this version: #1031966/31
Text MD5: b44d29d36111a5d00d169fe230c47076
Transpilation MD5: 6d7864f2b1f103202b1d7b96f782d9f1
Author: stefan
Category: javax / image recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-11 12:13:01
Source code size: 2282 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 222 / 446
Version history: 30 change(s)
Referenced in: [show references]