How to use _dumpOcr method in fMBT

Best Python code snippet using fMBT_python

fmbtgti.py

Source:fmbtgti.py Github

copy

Full Screen

...267 into fmbtgti.GUITestInterface instances and Screenshots.268 To implement an OCR engine, you need to override _findText() at269 minimum. See _findText documentation in this class for270 requirements.271 If possible in your OCR engine, you can provide _dumpOcr() to272 reveal what is recognized in screenshots.273 For efficient caching of results and freeing cached results, you274 can override _addScreenshot() and _removeScreenshot(). Every275 screenshot is added before findText() or dumpOcr().276 A typical usage of OcrEngine instance:277 - oe.addScreenshot(ss)278 - oe.findText(ss, text1, <engine/screenshot/find-specific-args>)279 - oe.findText(ss, text2, <engine/screenshot/find-specific-args>)280 - oe.removeScreenshot(ss)281 Note that there may be several screenshots added before they are282 removed.283 """284 def __init__(self, *args, **kwargs):285 super(OcrEngine, self).__init__(*args, **kwargs)286 self._ssFindTextDefaults = {}287 self._findTextDefaults = {}288 ocrFindArgs, _ = _takeOcrArgs(self, kwargs)289 self._setFindTextDefaults(ocrFindArgs)290 def dumpOcr(self, screenshot, **kwargs):291 """292 Returns what is recognized in the screenshot. For debugging293 purposes.294 """295 ocrArgs = self.__ocrArgs(screenshot, **kwargs)296 return self._dumpOcr(screenshot, **ocrArgs)297 def _dumpOcr(self, screenshot, **kwargs):298 return None299 def addScreenshot(self, screenshot, **findTextDefaults):300 """301 Prepare for finding text from the screenshot.302 Parameters:303 screenshot (fmbtgti.Screenshot)304 screenshot object to be searched from.305 other parameters (optional)306 findText defaults for this screenshot.307 Notice that there may be many screenshots simultaneously.308 Do not keep reference to the screenshot object.309 """310 self.setScreenshotFindTextDefaults(screenshot, **findTextDefaults)311 return self._addScreenshot(screenshot, **findTextDefaults)312 def _addScreenshot(self, screenshot, **findTextDefaults):313 pass314 def removeScreenshot(self, screenshot):315 """316 OCR queries on the screenshot will not be made anymore.317 """318 self._removeScreenshot(screenshot)319 try:320 del self._ssFindTextDefaults[id(screenshot)]321 except KeyError:322 raise KeyError('screenshot "%s" does not have findTextDefaults. '323 'If OcrEngine.addScreenshot() is overridden, it '324 '*must* call parent\'s addScreenshot.' % (screenshot.filename(),))325 def _removeScreenshot(self, screenshot):326 pass327 def setFindTextDefaults(self, **findTextDefaults):328 return self._setFindTextDefaults(findTextDefaults, screenshot=None)329 def setScreenshotFindTextDefaults(self, screenshot, **findTextDefaults):330 return self._setFindTextDefaults(findTextDefaults, screenshot=screenshot)331 def _setFindTextDefaults(self, defaults, screenshot=None):332 """333 Set default values for optional arguments for findText().334 Parameters:335 defaults (dictionary)336 Default keyword arguments and their values.337 screenshot (optional, fmbtgti.Screenshot instance)338 Use the defaults for findText on this screenshot. If339 the defaults are None, make them default for all340 screenshots. Screenshot-specific defaults override341 engine default.342 """343 if screenshot == None:344 self._findTextDefaults.update(defaults)345 else:346 ssid = id(screenshot)347 if not ssid in self._ssFindTextDefaults:348 self._ssFindTextDefaults[ssid] = self._findTextDefaults.copy()349 self._ssFindTextDefaults[ssid].update(defaults)350 def findTextDefaults(self, screenshot=None):351 if screenshot == None:352 return self._findTextDefaults353 elif id(screenshot) in self._ssFindTextDefaults:354 return self._ssFindTextDefaults[id(screenshot)]355 else:356 return None357 def _findTextArgNames(self):358 """359 Returns names of optional findText arguments.360 """361 return inspect.getargspec(self._findText).args[3:]362 def __ocrArgs(self, screenshot, **priorityArgs):363 ocrArgs = {}364 ocrArgs.update(self._findTextDefaults)365 ssId = id(screenshot)366 if ssId in self._ssFindTextDefaults:367 ocrArgs.update(self._ssFindTextDefaults[ssId])368 ocrArgs.update(priorityArgs)369 return ocrArgs370 def findText(self, screenshot, text, **kwargs):371 """372 Return list of fmbtgti.GUIItems that match to text.373 """374 ocrArgs = self.__ocrArgs(screenshot, **kwargs)375 return self._findText(screenshot, text, **ocrArgs)376 def _findText(self, screenshot, text, **kwargs):377 """378 Find appearances of text from the screenshot.379 Parameters:380 screenshot (fmbtgti.Screenshot)381 Screenshot from which text is to be searched382 for. Use Screenshot.filename() to get the filename.383 text (string)384 text to be searched for.385 other arguments (engine specific)386 kwargs contain keyword arguments given to387 findText(screenshot, text, ...), already extended388 first with screenshot-specific findTextDefaults, then389 with engine-specific findTextDefaults.390 _findText *must* define all engine parameters as391 explicit keyword arguments:392 def _findText(self, screenshot, text, engArg1=42):393 ...394 Return list of fmbtgti.GUIItems.395 """396 raise NotImplementedError("_findText needed but not implemented.")397class _EyenfingerOcrEngine(OcrEngine):398 """399 OCR engine parameters that can be used in all400 ...OcrText() methods (swipeOcrText, tapOcrText, findItemsByOcrText, ...):401 match (float, optional):402 minimum match score in range [0.0, 1.0]. The default is403 1.0 (exact match).404 area ((left, top, right, bottom), optional):405 search from the given area only. Left, top, right and406 bottom are either absolute coordinates (integers) or407 floats in range [0.0, 1.0]. In the latter case they are408 scaled to screenshot dimensions. The default is (0.0,409 0.0, 1.0, 1.0), that is, search everywhere in the410 screenshot.411 pagesegmodes (list of integers, optional):412 try all integers as tesseract -pagesegmode413 arguments. The default is [3], another good option could414 be [3, 6].415 preprocess (string, optional):416 preprocess filter to be used in OCR for better417 result. Refer to eyenfinger.autoconfigure to search for418 a good one.419 """420 class _OcrResults(object):421 __slots__ = ("filename", "screenSize", "pagesegmodes", "preprocess", "area", "words")422 def __init__(self, filename, screenSize):423 self.filename = filename424 self.screenSize = screenSize425 self.pagesegmodes = None426 self.preprocess = None427 self.area = None428 self.words = None429 def __init__(self, *args, **engineDefaults):430 engineDefaults["area"] = engineDefaults.get("area", (0.0, 0.0, 1.0, 1.0))431 engineDefaults["match"] = engineDefaults.get("match", 1.0)432 engineDefaults["pagesegmodes"] = engineDefaults.get("pagesegmodes", _OCRPAGESEGMODES)433 engineDefaults["preprocess"] = engineDefaults.get("preprocess", _OCRPREPROCESS)434 super(_EyenfingerOcrEngine, self).__init__(*args, **engineDefaults)435 self._ss = {} # OCR results for screenshots436 def _addScreenshot(self, screenshot, **findTextDefaults):437 ssId = id(screenshot)438 self._ss[ssId] = _EyenfingerOcrEngine._OcrResults(screenshot.filename(), screenshot.size())439 def _removeScreenshot(self, screenshot):440 ssId = id(screenshot)441 if ssId in self._ss:442 del self._ss[ssId]443 def _findText(self, screenshot, text, match=None, preprocess=None, area=None, pagesegmodes=None):444 ssId = id(screenshot)445 self._assumeOcrResults(screenshot, preprocess, area, pagesegmodes)446 for ppfilter in self._ss[ssId].words.keys():447 try:448 score_text_bbox_list = eyenfinger.findText(449 text, self._ss[ssId].words[ppfilter], match=match)450 if not score_text_bbox_list:451 continue452 else:453 break454 except eyenfinger.BadMatch:455 continue456 else:457 return []458 retval = [GUIItem("OCR text (match %.2f)" % (score,),459 bbox, self._ss[ssId].filename,460 ocrFind=text, ocrFound=matching_text)461 for score, matching_text, bbox in score_text_bbox_list]462 return retval463 def _dumpOcr(self, screenshot, match=None, preprocess=None, area=None, pagesegmodes=None):464 ssId = id(screenshot)465 if self._ss[ssId].words == None:466 self._assumeOcrResults(screenshot, preprocess, area, pagesegmodes)467 w = []468 for ppfilter in self._ss[ssId].preprocess:469 for word in self._ss[ssId].words[ppfilter]:470 for appearance, (wid, middle, bbox) in enumerate(self._ss[ssId].words[ppfilter][word]):471 (x1, y1, x2, y2) = bbox472 w.append((word, (x1, y1, x2, y2)))473 return sorted(set(w), key=lambda i:(i[1][1]/8, i[1][0]))474 def _assumeOcrResults(self, screenshot, preprocess, area, pagesegmodes):475 ssId = id(screenshot)476 if not type(preprocess) in (list, tuple):477 preprocess = [preprocess]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful