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

101
LINES

< > BotCompany Repo | #1006341 // Find snippet source on screen, recognize, step 2 (dev.)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (10176L/72K/244K).

!7

static double spaceWidth = 8;

// typical line dists: 6, 21
static float lineDist1 = 6, lineDist2 = 22;
static float lineHeight = lineDist2-lineDist1;

p-type {
  RGBImage img = rgbShootScreenHidingConsole(;
  Rect r = ocr_findSnippetSource(img;
  
  if (r == null) {
    print("No snippet source found.";
    ret;
  }
  print("Scanning: " + r;

  BWImage bwImage = new BWImage(img.clip(r;
  L<Rect> clips = autoSegment(bwImage;
  
  L<Rect> r2 = ocr_mergeOverlappingY(clips;
  //L<Rect> r2 = ocr_mergeOverlappingY_plusPercent(clips, 7;
  r2 = sortByField(r2, "y");
  
  /*for (Rect rx : r2) {
    print("h=" + rx.h + ". " + struct(bwImageRowBrightnesses(bwImage.clip(rx;
  }*/
  
  // Split rows
  for i over r2: {
    Rect rx = r2.get(i;
    BWImage cImg = bwImage.clip(rx);
    int h = rx.h/2;
    //float[] rows = bwImageRowBrightnesses(bwImage.clip(rx));
    //float b = rows[l(rows)/2];
    float b = (float) cImg.clip(0, h, rx.w, 1).averageBrightness();
    if (b > 0.93f) {
      Rect upper = new Rect(rx.x, rx.y, rx.w, h);
      Rect lower = new Rect(rx.x, rx.y+h, rx.w, rx.h-h);
      r2.set(i, lower);
      r2.add(i++, upper);
    }
  }
  
  // Merge rows
  for (int i = 0; i < l(r2)-1; i++) {
    Rect a = r2.get(i), b = r2.get(i+1);
    if (b.h < a.h/2 && (b.y-a.y2()) < 2) { // fix the dangling underscore
      r2.set(i, mergeRects(a, b));
      r2.remove(i+1);
    }
  }
  
  r2 = bwAutoCropClips(bwImage, r2);
  
  showImageWithSelections(bwImage.getBufferedImage(), r2;
  focusConsole();
  growToTop(consoleFrame());
  enableFullSearchInSimpleRecognizer(filledSimpleRecognizer_cached());
  L<S> lines = ocr_recognizeTextClips(bwImage, r2;
  
  lines = map(f ocr_unescape, lines);
  
  addIndents(lines, r2);
  addEmptyRows(lines, r2);
  
  printNumbered_flushed(lines;
}

svoid addIndents(L<S> lines, L<Rect> rects) {
  int n = l(lines);
  assertEquals(n, l(rects));
  int leftBorder = toInt(collectMinInt(rects, "x"));
  for i to n: {
    int dist = rects.get(i).x-leftBorder;
    print("dist=" + dist);
    int spaces = iround(dist/spaceWidth;
    if (spaces > 0)
      lines.set(i, rep(spaces, ' ') + lines.get(i));
  }
}

svoid addEmptyRows(L<S> lines, L<Rect> rects) {
  int n = l(lines);
  assertEquals(n, l(rects));
  for (int i = 0; i+1 < n; i++) {
    Rect a = rects.get(i), b = rects.get(i+1);
    if (a != null && b != null) {
      int dist = b.y-a.y2();
      float frows = (dist-lineDist1)/lineHeight;
      int rows = iround(frows);
      print("dist=" + dist + ", rows=" + frows);
      if (rows > 0) {
        lines.addAll(i+1, rep("", rows));
        rects.addAll(i+1, rep((Rect) null, rows));
        i += rows-1;
      }
    }
  }
}

Author comment

Began life as a copy of #1006334

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006341
Snippet name: Find snippet source on screen, recognize, step 2 (dev.)
Eternal ID of this version: #1006341/3
Text MD5: 96f857cc4ae2d4b320302059f395b932
Transpilation MD5: 2577054484bd49ae0de392a5aaded4bb
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-11 00:59:02
Source code size: 2775 bytes / 101 lines
Pitched / IR pitched: No / No
Views / Downloads: 630 / 1032
Version history: 2 change(s)
Referenced in: [show references]