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

100
LINES

< > BotCompany Repo | #1015422 // Movie Review Cat!

JavaX source code (desktop) [tags: archive use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (11768L/87K).

1  
!7
2  
3  
static double zoom = 1.5;
4  
static volatile BufferedImage img, recognizedImage;
5  
static L<RecognizedText> texts;
6  
static ImageSurface is, isWord;
7  
static volatile int score;
8  
static volatile L<RecognizedText> posTexts, negTexts;
9  
static Set<S> dontPrint;
10  
11  
//static int coresToUse() { ret numberOfCores()/2; }
12  
13  
p-subst-autorestart {
14  
  ocr_recognizeMultiLine_scored_maxPixels = 20000;
15  
  hideConsoleOnFirstFrame();
16  
  //mL_setCacheTime(60.0);
17  
  dontPrint = mechSetCI("Movie Review Cat | Negative/positive words don't print list");
18  
  
19  
  thread "The Loop" { theLoop(); }
20  
  noMainDone("Cat!");
21  
}
22  
  
23  
svoid theLoop {
24  
  repeat with sleep 0 {
25  
    //print("Shooting");
26  
    //img = shootScreenHidingConsole();
27  
    img = shootScreen2();
28  
    //print("Recognizing");
29  
    
30  
    long recTime = sysNow();
31  
    texts = ocr_recognizeMultiLine_scored(img);
32  
    print(filledSimpleRecognizer_cached().cacheStats());
33  
    recTime = sysNow()-recTime;
34  
      
35  
    time "Post" {
36  
      L<S> lines = mapMethod('text, texts);
37  
      MultiSet<S> multiSet = asCIMultiSet(onlyStringsLongerThan(1, words(lines(lines))));
38  
      //pnl(multiSetAsMap_popularFirst(multiSet));
39  
      Set<S> positiveWords = mechSetCI("Movie Review Cat | Positive words");
40  
      Set<S> negativeWords = mechSetCI("Movie Review Cat | Negative words");
41  
      Set<S> set = asCISet(keys(multiSet));
42  
      final MultiSet<S> plus = multiSetSetIntersection(multiSet, positiveWords);
43  
      final MultiSet<S> minus = multiSetSetIntersection(multiSet, negativeWords);
44  
      score = l(plus)-l(minus);
45  
      
46  
      posTexts = [RecognizedText r : texts | containsOneOfTheWords(r.text(), setMinusSet(keys(plus), dontPrint))];
47  
      negTexts = [RecognizedText r : texts | containsOneOfTheWords(r.text(), setMinusSet(keys(minus), dontPrint))];
48  
        
49  
      L<S> popPlus = mostPopularEntriesFromMultiMap(3, plus);
50  
      L<S> popMinus = mostPopularEntriesFromMultiMap(3, minus);
51  
      
52  
      S text = "Score: " + score;
53  
      if (score >= 0 && nempty(popPlus))
54  
        text += "\n" + joinWithComma(popPlus);
55  
      if (score <= 0 && nempty(popMinus))
56  
        text += "\n" + joinWithComma(popMinus);
57  
      //infoBox(text);
58  
      flatInfoBox(recTime + " ms | " + escapeNewLines(text));
59  
      S imageID = score == 0 ? #1101256
60  
        : score > 0 ? #1101255
61  
        : #1101254;
62  
      recognizedImage = img;
63  
    }
64  
65  
    bool first = is == null;
66  
    is = showImage_hcentered(is, loadImage2_cached(imageID));
67  
    if (first) {
68  
      alwaysOnTopFrame(is);
69  
      addToWindowWithMargin(is, jfullcenter(isWord = jImageSurface()));
70  
      enlargeFrameLeftAndBottom(is, 50);
71  
      isWord.setZoom(2);
72  
      
73  
      awtEvery(isWord, 333, f cycleWord);
74  
    }
75  
    //packFrame/*InTopRightCorner*/(is);
76  
  }
77  
}
78  
79  
svoid cycleWord {
80  
  if (recognizedImage == null) ret;
81  
  RecognizedText hilite = null;
82  
  if (score > 0) hilite = random(posTexts);
83  
  else if (score < 0) hilite = random(negTexts);
84  
  if (hilite != null) {
85  
    final RecognizedText _hilite = hilite;
86  
    final BufferedImage clip = clipBufferedImage(recognizedImage, hilite.r);
87  
    isWord.setImage(clip);
88  
    swing {
89  
      BufferedImage scaledClip = scaleBufferedImage(clip, zoom);
90  
      JWindow win = makeWindow(jImageLabel(scaledClip));
91  
      Pt p = centerOfRect(_hilite.r);
92  
      int w = scaledClip.getWidth(), h = scaledClip.getHeight();
93  
      win.setBounds(p.x-w/2, p.y-h/2, w, h);
94  
      win.setAlwaysOnTop(true);
95  
      win.setVisible(true);
96  
      disposeWindowAfter(win, 0.5);
97  
    }
98  
  } else
99  
    isWord.setImage(newBufferedImage(1, 1, Color.white));
100  
}

Author comment

Began life as a copy of #1015266

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1015422
Snippet name: Movie Review Cat!
Eternal ID of this version: #1015422/64
Text MD5: 42cc758a2a783f9df13ae39967c0f4fe
Transpilation MD5: 21b870cf3eacc6ccc40209a10e7e154c
Author: stefan
Category: javax / ocr
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2018-06-16 13:31:19
Source code size: 3604 bytes / 100 lines
Pitched / IR pitched: No / No
Views / Downloads: 633 / 1886
Version history: 63 change(s)
Referenced in: [show references]