/// <summary> /// try to give resources free /// TODO: most likely, this method is not used /// </summary> /// <param name="ce"></param> private static void DisposeData(ContextEntry ce) { if (ce == null || ce.Screenshot == null) return; if (ce.Screenshot.Image != null) ce.Screenshot.Image.Dispose(); if (ce.Screenshot != null) ce.Screenshot.Dispose(); ce = null; }
/// <summary> /// Gets a screenshot of the active window (already optimised for OCR), /// calculates the thresholded image, /// runs the OCR, /// and returns the result as a context entry. /// </summary> /// <param name="ce"></param> /// <returns></returns> public ContextEntry RunOcr(ContextEntry ce) { if (_tEngine == null) return null; try { // run OCR preprocessing RunOcrPreProcessing(ce.Screenshot); // processed screenshot var processedScreenshot = ce.Screenshot.Image; // threshold //screenshot = _tEngine.GetThresholdedImage(screenshot, Rectangle.Empty); //TODO: enable or disable? //Screenshot.SaveImage(screenshot, "thresholded"); //TEMP _tEngine.OcrDone += OcrFinished; //var result = _tEngine.DoOCR(screenshot, Rectangle.Empty); // used for single-threading OCR processing _tEngine.DoOCR(processedScreenshot, Rectangle.Empty); processedScreenshot.Dispose(); _mEvent = new ManualResetEvent(false); _mEvent.WaitOne(); // wait here until it's finished // add ocr'd text to context entry ce.OcrText = _temporaryOcrText; ce.Confidence = _tempConfidence; // reset temp values _temporaryOcrText = string.Empty; _tempConfidence = Settings.OcrConfidenceAcceptanceThreshold; // release sources ce.Screenshot.Dispose(); ce.Screenshot = null; } catch (Exception e) { Logger.WriteToLogFile(e); } return ce; }
private static void SaveContextEntryToDatabase(ContextEntry contextEntry) { if (contextEntry == null || contextEntry.OcrText == string.Empty || // no text recognized contextEntry.Confidence < 0.5) // makes no sense to save if recognition is too in-accurate return; var query = "INSERT INTO " + Settings.DbTable + " (time, timestamp, ocrText, confidentality, windowName, processName) VALUES (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime'), " + Database.GetInstance().QTime(contextEntry.Timestamp) + ", " + Database.GetInstance().Q(contextEntry.OcrText) + ", " + Database.GetInstance().Q(contextEntry.Confidence.ToString()) + ", " + Database.GetInstance().Q(contextEntry.WindowName) + ", " + Database.GetInstance().Q(contextEntry.ProcessName) + ");"; Database.GetInstance().ExecuteDefaultQuery(query); }
/// <summary> /// Every x seconds the Context Recognition algorithm is run if the window has changed. /// Some info about the context is added (currently: process + windows information) /// Then, the background worker is started for OCR recognition /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RunContextRecognition(object sender, EventArgs e) { // determine whether store or not if (_screenshotChangedTracker == null || !_screenshotChangedTracker.WindowChanged()) return; // capture the screenshot var sc = ScreenCapture.CaptureActiveWindowHq(); if (sc == null || sc.Image == null) return; // screenshot capture unsuccessful // get window & process names (additional contextual information) var currentWindowName = _screenshotChangedTracker.GetWindowText(); var currentProcessName = _screenshotChangedTracker.GetProcessName(); var ce = new ContextEntry { Timestamp = DateTime.Now, WindowName = currentWindowName, ProcessName = currentProcessName, Confidence = OcrLibrary.Settings.OcrConfidenceAcceptanceThreshold, // so it won't be saved if it isn't overwritten OcrText = string.Empty, Screenshot = sc }; // runs the OCR process and get's the result as a context entry ce = OcrEngine.GetInstance().RunOcr(ce); // only save in database if useful accuracy & enough content if (ce != null && ce.Confidence < OcrLibrary.Settings.OcrConfidenceAcceptanceThreshold && ce.OcrText.Length > OcrLibrary.Settings.OcrTextLengthAcceptanceThreshold) { SaveContextEntryToDatabase(ce); } // dispose data (to free up RAM) DisposeData(ce); }