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

74
LINES

< > BotCompany Repo | #1019834 // Box Finder v1 [Dyn Module]

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

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

!7

replace Submission with BackgroundPlus<BoxWithColor>.

// TODO: print log problem
cmodule2 BoxFinder > DynImageSurface {
  int nBoxes = 1;
  bool keepGoing;
  new Best<Submission> best;
  
  transient RGBImage original;
  transient Thread recognizerThread;
  transient ImageSurface isBoxes;
  transient Submission rendered;
  
  S switchableFields() { ret "nBoxes keepGoing"; }
  
  visualize {
    JSpinner spinner = liveValueSpinner(dm_fieldLiveValue('nBoxes), 1, 50);
    main.onChange(spinner, r startRecognition );
    ret jvsplit(
      vgrid(super.visualize(), jscroll_centered(isBoxes = imageSurface())),
      northAndCenter(
        withSideMargin(rightAlignedLine(
          dm_fieldCheckBox('keepGoing),
          jbutton("Start", r startRecognition),
          jbutton("Stop", r { cancelThread(recognizerThread) }),
          withLabel("Boxes:", spinner))),
        dm_printLogComponent()));
  }

  start {
    if (!hasImage()) setImage(whiteImage(50, 50)); // so we can paste...
    onNewImage = r startRecognition;
    doEvery(1.0, r renderBoxes);
  }
  
  double scoreImage(RGBImage image) {
    ret 100*(1.0-rgbDistance(image, original));
  }
  
  void startRecognition enter {
    cancelThread(recognizerThread);
    recognizerThread = startThread(r {
      BufferedImage image = getImage();
      final int w = image.getWidth(), h = image.getHeight();
      print("Starting recognition on " + w + "*" + h + " image");
      best = new Best;
      best.stringifier = f sfu;
      original = RGBImage(image);
      //new AIStrategy_RandomWithVariation<Submission> strategy;
      new AIStrategy_Racer_RandomWithVary<Submission> strategy;
      strategy.verbose = true;
      strategy.best = best;
      strategy.submit = func(Submission s) -> double { scoreImage(rgbRenderRenderable(w, h, s)) };
      strategy.random = func -> Submission { randomBackgroundPlusBoxes(w, h, nBoxes) };
      strategy.vary = func(Submission s) -> Submission { varyBackgroundPlusBoxes(s, w, h) };
  
      if (keepGoing)
        runStrategyForever(best, strategy);
      else
        runStrategyWhileImprovement(best, strategy);
      print("Done");
      persistMe();
    });
  }
  
  void renderBoxes enter {
    if (original != null && best! != rendered) {
      rendered = best!;
      isBoxes.setImage(renderRenderable(original.w(), original.h(), rendered));
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1019834
Snippet name: Box Finder v1 [Dyn Module]
Eternal ID of this version: #1019834/35
Text MD5: 193e0d3e3a6268c943214c274aca107c
Transpilation MD5: 44ae641db66427507c1f1df291b2052e
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-04 00:29:08
Source code size: 2449 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 339 / 975
Version history: 34 change(s)
Referenced in: [show references]