How to use _defaultOcrEngine method in fMBT

Best Python code snippet using fMBT_python

fmbtgti.py

Source:fmbtgti.py Github

copy

Full Screen

...482 for ppfilter in preprocess:483 pp = ppfilter % { "zoom": "-resize %sx" % (self._ss[ssId].screenSize[0] * 2) }484 eyenfinger.iRead(source=self._ss[ssId].filename, ocr=True, preprocess=pp, ocrArea=area, ocrPageSegModes=pagesegmodes)485 self._ss[ssId].words[ppfilter] = eyenfinger._g_words486def _defaultOcrEngine():487 if _g_defaultOcrEngine:488 return _g_defaultOcrEngine489 else:490 _EyenfingerOcrEngine().register(defaultOcr=True)491 return _g_defaultOcrEngine492class OirEngine(OrEngine):493 """494 This is an abstract interface for OIR (optical image recognition)495 engines that can be plugged into fmbtgti.GUITestInterface496 instances and Screenshots.497 To implement an OIR engine, you need to override _findBitmap() at498 minimum. See _findBitmap documentation in this class for499 requirements.500 This base class provides full set of OIR parameters to501 _findBitmap. The parameters are combined from502 - OirEngine find defaults, specified when OirEngine is503 instantiated.504 - Screenshot instance find defaults.505 - bitmap / bitmap directory find defaults (read from the506 .fmbtoirrc that is in the same directory as the bitmap).507 - ...Bitmap() method parameters.508 The latter in the list override the former.509 For efficient caching of results and freeing cached results, you510 can override _addScreenshot() and _removeScreenshot(). Every511 screenshot is added before findBitmap().512 A typical usage of OirEngine instance:513 - oe.addScreenshot(ss)514 - oe.findBitmap(ss, bmpFilename1, <engine/screenshot/find-specific-args>)515 - oe.findBitmap(ss, bmpFilename2, <engine/screenshot/find-specific-args>)516 - oe.removeScreenshot(ss)517 Note that there may be several screenshots added before they are518 removed. ss is a Screenshot instance. Do not keep references to519 Screenshot intances, otherwise garbage collector will not remove520 them.521 """522 def __init__(self, *args, **kwargs):523 super(OirEngine, self).__init__(*args, **kwargs)524 self._ssFindBitmapDefaults = {}525 self._findBitmapDefaults = {}526 oirArgs, _ = _takeOirArgs(self, kwargs)527 self._setFindBitmapDefaults(oirArgs)528 def addScreenshot(self, screenshot, **findBitmapDefaults):529 """530 Prepare for finding bitmap from the screenshot.531 Parameters:532 screenshot (fmbtgti.Screenshot)533 screenshot object to be searched from.534 other parameters (optional)535 findBitmap defaults for this screenshot.536 Notice that there may be many screenshots simultaneously.537 Do not keep reference to the screenshot object.538 """539 self.setScreenshotFindBitmapDefaults(screenshot, **findBitmapDefaults)540 return self._addScreenshot(screenshot, **findBitmapDefaults)541 def _addScreenshot(self, screenshot, **findBitmapDefaults):542 pass543 def removeScreenshot(self, screenshot):544 """545 OIR queries on the screenshot will not be made anymore.546 """547 self._removeScreenshot(screenshot)548 try:549 del self._ssFindBitmapDefaults[id(screenshot)]550 except KeyError:551 raise KeyError('screenshot "%s" does not have findBitmapDefaults. '552 'If OirEngine.addScreenshot() is overridden, it '553 '*must* call parent\'s addScreenshot.' % (screenshot.filename(),))554 def _removeScreenshot(self, screenshot):555 pass556 def setFindBitmapDefaults(self, **findBitmapDefaults):557 return self._setFindBitmapDefaults(findBitmapDefaults, screenshot=None)558 def setScreenshotFindBitmapDefaults(self, screenshot, **findBitmapDefaults):559 return self._setFindBitmapDefaults(findBitmapDefaults, screenshot=screenshot)560 def _setFindBitmapDefaults(self, defaults, screenshot=None):561 """562 Set default values for optional arguments for findBitmap().563 Parameters:564 defaults (dictionary)565 Default keyword arguments and their values.566 screenshot (optional, fmbtgti.Screenshot instance)567 Use the defaults for findBitmap on this screenshot. If568 the defaults are None, make them default for all569 screenshots. Screenshot-specific defaults override570 engine default.571 """572 if screenshot == None:573 self._findBitmapDefaults.update(defaults)574 else:575 ssid = id(screenshot)576 if not ssid in self._ssFindBitmapDefaults:577 self._ssFindBitmapDefaults[ssid] = self._findBitmapDefaults.copy()578 self._ssFindBitmapDefaults[ssid].update(defaults)579 def findBitmapDefaults(self, screenshot=None):580 if screenshot == None:581 return self._findBitmapDefaults582 elif id(screenshot) in self._ssFindBitmapDefaults:583 return self._ssFindBitmapDefaults[id(screenshot)]584 else:585 return None586 def _findBitmapArgNames(self):587 """588 Returns names of optional findBitmap arguments.589 """590 return inspect.getargspec(self._findBitmap).args[3:]591 def __oirArgs(self, screenshot, bitmap, **priorityArgs):592 oirArgs = {}593 oirArgs.update(self._findBitmapDefaults)594 ssId = id(screenshot)595 if ssId in self._ssFindBitmapDefaults:596 oirArgs.update(self._ssFindBitmapDefaults[ssId])597 oirArgs.update(priorityArgs)598 return oirArgs599 def findBitmap(self, screenshot, bitmap, **kwargs):600 """601 Return list of fmbtgti.GUIItems that match to bitmap.602 """603 oirArgs = self.__oirArgs(screenshot, bitmap, **kwargs)604 return self._findBitmap(screenshot, bitmap, **oirArgs)605 def _findBitmap(self, screenshot, bitmap, **kwargs):606 """607 Find appearances of bitmap from the screenshot.608 Parameters:609 screenshot (fmbtgti.Screenshot)610 Screenshot from which bitmap is to be searched611 for. Use Screenshot.filename() to get the filename.612 bitmap (string)613 bitmap to be searched for.614 other arguments (engine specific)615 kwargs contain keyword arguments given to616 findBitmap(screenshot, bitmap, ...), already extended617 first with screenshot-specific findBitmapDefaults, then618 with engine-specific findBitmapDefaults.619 _findBitmap *must* define all engine parameters as620 explicit keyword arguments:621 def _findBitmap(self, screenshot, bitmap, engArg1=42):622 ...623 Returns list of fmbtgti.GUIItems.624 """625 raise NotImplementedError("_findBitmap needed but not implemented.")626class _Eye4GraphicsOirEngine(OirEngine):627 """OIR engine parameters that can be used in all628 ...Bitmap() methods (swipeBitmap, tapBitmap, findItemsByBitmap, ...):629 colorMatch (float, optional):630 required color matching accuracy. The default is 1.0631 (exact match). For instance, 0.75 requires that every632 pixel's every RGB component value on the bitmap is at633 least 75 % match with the value of corresponding pixel's634 RGB component in the screenshot.635 opacityLimit (float, optional):636 threshold for comparing pixels with non-zero alpha637 channel. Pixels less opaque than the given threshold are638 skipped in match comparison. The default is 0, that is,639 alpha channel is ignored.640 area ((left, top, right, bottom), optional):641 search bitmap from the given area only. Left, top right642 and bottom are either absolute coordinates (integers) or643 floats in range [0.0, 1.0]. In the latter case they are644 scaled to screenshot dimensions. The default is (0.0,645 0.0, 1.0, 1.0), that is, search everywhere in the646 screenshot.647 limit (integer, optional):648 number of returned matches is limited to the limit. The649 default is -1: all matches are returned. Applicable in650 findItemsByBitmap.651 allowOverlap (boolean, optional):652 allow returned icons to overlap. If False, returned list653 contains only non-overlapping bounding boxes. The654 default is False.655 scale (float or pair of floats, optional):656 scale to be applied to the bitmap before657 matching. Single float is a factor for both X and Y658 axis, pair of floats is (xScale, yScale). The default is659 1.0.660 bitmapPixelSize (integer, optional):661 size of pixel rectangle on bitmap for which there must662 be same color on corresponding screenshot rectangle. If663 scale is 1.0, default is 1 (rectangle is 1x1). If scale664 != 1.0, the default is 2 (rectangle is 2x2).665 screenshotPixelSize (integer, optional):666 size of pixel rectangle on screenshot in which there667 must be a same color pixel as in the corresponding668 rectangle on bitmap. The default is scale *669 bitmapPixelSize.670 If unsure about parameters, but you have a bitmap that should be671 detected in a screenshot, try obj.oirEngine().adjustParameters().672 Example:673 d.enableVisualLog("params.html")674 screenshot = d.refreshScreenshot()675 results = d.oirEngine().adjustParameters(screenshot, "mybitmap.png")676 if results:677 item, params = results[0]678 print "found %s with parameters:" % (item,)679 print "\n".join(sorted([" %s = %s" % (k, params[k]) for k in params]))680 print "verify:", d.verifyBitmap("mybitmap.png", **params)681 Notice, that you can force refreshScreenshot to load old screenshot:682 d.refreshScreenshot("old.png")683 """684 def __init__(self, *args, **engineDefaults):685 engineDefaults["colorMatch"] = engineDefaults.get("colorMatch", 1.0)686 engineDefaults["opacityLimit"] = engineDefaults.get("opacityLimit", 0.0)687 engineDefaults["area"] = engineDefaults.get("area", (0.0, 0.0, 1.0, 1.0))688 engineDefaults["limit"] = engineDefaults.get("limit", -1)689 engineDefaults["allowOverlap"] = engineDefaults.get("allowOverlap", False)690 engineDefaults["scale"] = engineDefaults.get("scale", 1.0)691 engineDefaults["bitmapPixelSize"] = engineDefaults.get("bitmapPixelSize", 0)692 engineDefaults["screenshotPixelSize"] = engineDefaults.get("screenshotPixelSize", 0)693 OirEngine.__init__(self, *args, **engineDefaults)694 self._openedImages = {}695 self._findBitmapCache = {}696 def _addScreenshot(self, screenshot, **findBitmapDefaults):697 filename = screenshot.filename()698 self._openedImages[filename] = eye4graphics.openImage(filename)699 # make sure size() is available, this can save an extra700 # opening of the screenshot file.701 if screenshot.size(allowReadingFile=False) == None:702 screenshot.setSize(_e4gImageDimensions(self._openedImages[filename]))703 self._findBitmapCache[filename] = {}704 def _removeScreenshot(self, screenshot):705 filename = screenshot.filename()706 eye4graphics.closeImage(self._openedImages[filename])707 del self._openedImages[filename]708 del self._findBitmapCache[filename]709 def adjustParameters(self, screenshot, bitmap,710 scaleRange = [p/100.0 for p in range(110,210,10)],711 colorMatchRange = [p/100.0 for p in range(100,60,-10)],712 pixelSizeRange = range(2,5),713 resultCount = 1,714 **oirArgs):715 """716 Search for scale, colorMatch, bitmapPixelSize and717 screenshotPixelSize parameters that find the bitmap in the718 screenshot.719 Parameters:720 screenshot (Screenshot instance):721 screenshot that contains the bitmap.722 bitmap (string):723 bitmap filename.724 scaleRange (list of floats, optional):725 scales to go through.726 The default is: 1.1, 1.2, ... 2.0.727 colorMatchRange (list of floats, optional):728 colorMatches to try out.729 The default is: 1.0, 0.9, ... 0.7.730 pixelSizeRange (list of integers, optional):731 values for bitmapPixelSize and screenshotPixelSize.732 The default is: [2, 3]733 resultCount (integer, optional):734 number of parameter combinations to be found.735 The default is 1. 0 is unlimited.736 other OIR parameters: as usual, refer to engine documentation.737 Returns list of pairs: (GUIItem, findParams), where738 GUIItem is the detected item (GUIItem.bbox() is the box around it),739 and findParams is a dictionary containing the parameters.740 """741 if not screenshot.filename() in self._findBitmapCache:742 self.addScreenshot(screenshot)743 ssAdded = True744 else:745 ssAdded = False746 retval = []747 for colorMatch in colorMatchRange:748 for pixelSize in pixelSizeRange:749 for scale in scaleRange:750 findParams = oirArgs.copy()751 findParams.update({"colorMatch": colorMatch,752 "limit": 1,753 "scale": scale,754 "bitmapPixelSize": pixelSize,755 "screenshotPixelSize": pixelSize})756 results = self.findBitmap(screenshot, bitmap,757 **findParams)758 if results:759 retval.append((results[0], findParams))760 if len(retval) == resultCount:761 return retval762 if ssAdded:763 self.removeScreenshot(screenshot)764 return retval765 def _findBitmap(self, screenshot, bitmap, colorMatch=None,766 opacityLimit=None, area=None, limit=None,767 allowOverlap=None, scale=None,768 bitmapPixelSize=None, screenshotPixelSize=None):769 """770 Find items on the screenshot that match to bitmap.771 """772 ssFilename = screenshot.filename()773 ssSize = screenshot.size()774 cacheKey = (bitmap, colorMatch, opacityLimit, area, limit,775 scale, bitmapPixelSize, screenshotPixelSize)776 if cacheKey in self._findBitmapCache[ssFilename]:777 return self._findBitmapCache[ssFilename][cacheKey]778 self._findBitmapCache[ssFilename][cacheKey] = []779 e4gIcon = eye4graphics.openImage(bitmap)780 if e4gIcon == 0:781 raise IOError('Cannot open bitmap "%s".' % (bitmap,))782 matchCount = 0783 leftTopRightBottomZero = (_intCoords((area[0], area[1]), ssSize) +784 _intCoords((area[2], area[3]), ssSize) +785 (0,))786 struct_area_bbox = _Bbox(*leftTopRightBottomZero)787 struct_bbox = _Bbox(0, 0, 0, 0, 0)788 contOpts = 0 # search for the first hit789 try:790 xscale, yscale = scale791 except TypeError:792 xscale = yscale = float(scale)793 while True:794 if matchCount == limit: break795 result = eye4graphics.findNextIcon(796 ctypes.byref(struct_bbox),797 ctypes.c_void_p(self._openedImages[ssFilename]),798 ctypes.c_void_p(e4gIcon),799 0, # no fuzzy matching800 ctypes.c_double(colorMatch),801 ctypes.c_double(opacityLimit),802 ctypes.byref(struct_area_bbox),803 ctypes.c_int(contOpts),804 ctypes.c_float(xscale),805 ctypes.c_float(yscale),806 ctypes.c_int(bitmapPixelSize),807 ctypes.c_int(screenshotPixelSize))808 contOpts = 1 # search for the next hit809 if result < 0: break810 bbox = (int(struct_bbox.left), int(struct_bbox.top),811 int(struct_bbox.right), int(struct_bbox.bottom))812 addToFoundItems = True813 if allowOverlap == False:814 for guiItem in self._findBitmapCache[ssFilename][cacheKey]:815 itemLeft, itemTop, itemRight, itemBottom = guiItem.bbox()816 if ((itemLeft <= bbox[0] <= itemRight or itemLeft <= bbox[2] <= itemRight) and817 (itemTop <= bbox[1] <= itemBottom or itemTop <= bbox[3] <= itemBottom)):818 if ((itemLeft < bbox[0] < itemRight or itemLeft < bbox[2] < itemRight) or819 (itemTop < bbox[1] < itemBottom or itemTop < bbox[3] < itemBottom)):820 addToFoundItems = False821 break822 if addToFoundItems:823 self._findBitmapCache[ssFilename][cacheKey].append(824 GUIItem("bitmap", bbox, ssFilename, bitmap=bitmap))825 matchCount += 1826 eye4graphics.closeImage(e4gIcon)827 return self._findBitmapCache[ssFilename][cacheKey]828def _defaultOirEngine():829 if _g_defaultOirEngine:830 return _g_defaultOirEngine831 else:832 _Eye4GraphicsOirEngine().register(defaultOir=True)833 return _g_defaultOirEngine834class _OirRc(object):835 """Optical image recognition settings for a directory.836 Currently loaded from file .fmbtoirc in the directory.837 There is once _OirRc instance per directory.838 """839 _filename = ".fmbtoirrc"840 _cache = {}841 @classmethod842 def load(cls, directory):843 if directory in cls._cache:844 pass845 elif os.access(os.path.join(directory, cls._filename), os.R_OK):846 cls._cache[directory] = cls(directory)847 else:848 cls._cache[directory] = None849 return cls._cache[directory]850 def __init__(self, directory):851 self._key2value = {}852 curdir = "."853 self._dir2oirArgsList = {curdir: [{}]}854 for line in file(os.path.join(directory, _OirRc._filename)):855 line = line.strip()856 if line == "" or line[0] in "#;":857 continue858 elif line == "alternative":859 self._dir2oirArgsList[curdir].append({}) # new set of args860 self._key2value = {}861 elif "=" in line:862 key, value_str = line.split("=", 1)863 value_str = value_str.strip()864 if key.strip().lower() == "includedir":865 curdir = value_str866 self._dir2oirArgsList[curdir] = [{}]867 else:868 try: value = int(value_str)869 except ValueError:870 try: value = float(value_str)871 except ValueError:872 if value_str[0] in "([\"'": # tuple, list, string873 value = eval(value_str)874 else:875 value = value_str876 self._dir2oirArgsList[curdir][-1][key.strip()] = value877 def searchDirs(self):878 return self._dir2oirArgsList.keys()879 def oirArgsList(self, searchDir):880 return self._dir2oirArgsList[searchDir]881class _Paths(object):882 def __init__(self, bitmapPath, relativeRoot):883 self.bitmapPath = bitmapPath884 self.relativeRoot = relativeRoot885 self._oirAL = {} # OIR parameters for bitmaps886 self._abspath = {} # bitmap to abspath887 def abspath(self, bitmap, checkReadable=True):888 if bitmap in self._abspath:889 return self._abspath[bitmap]890 if bitmap.startswith("/"):891 path = [os.path.dirname(bitmap)]892 bitmap = os.path.basename(bitmap)893 else:894 path = []895 for singleDir in self.bitmapPath.split(":"):896 if not singleDir.startswith("/"):897 path.append(os.path.join(self.relativeRoot, singleDir))898 else:899 path.append(singleDir)900 for singleDir in path:901 retval = os.path.join(singleDir, bitmap)902 if not checkReadable or os.access(retval, os.R_OK):903 oirRc = _OirRc.load(singleDir)904 if oirRc:905 self._oirAL[retval] = oirRc.oirArgsList(".")906 else:907 self._oirAL[retval] = [{}]908 self._oirAL[bitmap] = self._oirAL[retval]909 break910 else:911 # bitmap is not in singleDir, but there might be .fmbtoirrc912 oirRc = _OirRc.load(singleDir)913 if oirRc:914 for d in oirRc.searchDirs():915 if d.startswith("/"):916 candidate = os.path.join(d, bitmap)917 else:918 candidate = os.path.join(singleDir, d, bitmap)919 if os.access(candidate, os.R_OK):920 self._oirAL[candidate] = oirRc.oirArgsList(d)921 self._oirAL[bitmap] = self._oirAL[candidate]922 retval = candidate923 break924 if checkReadable and not os.access(retval, os.R_OK):925 raise ValueError('Bitmap "%s" not readable in bitmapPath %s' % (bitmap, ':'.join(path)))926 self._abspath[bitmap] = retval927 return retval928 def oirArgsList(self, bitmap):929 """Returns list of alternative OIR parameters associated to the bitmap930 by appropriate .fmbtoirrc file931 """932 if bitmap in self._oirAL:933 return self._oirAL[bitmap]934 else:935 absBitmap = self.abspath(bitmap)936 if absBitmap in self._oirAL:937 return self._oirAL[absBitmap]938 else:939 return None940class GUITestInterface(object):941 def __init__(self, ocrEngine=None, oirEngine=None, rotateScreenshot=None):942 self._paths = _Paths("", "")943 self._conn = None944 self._lastScreenshot = None945 self._longPressHoldTime = 2.0946 self._longTapHoldTime = 2.0947 self._ocrEngine = None948 self._oirEngine = None949 self._rotateScreenshot = rotateScreenshot950 self._screenshotLimit = None951 self._screenshotRefCount = {} # filename -> Screenshot object ref count952 self._screenshotArchiveMethod = "resize"953 if ocrEngine == None:954 self.setOcrEngine(_defaultOcrEngine())955 else:956 if type(ocrEngine) == int:957 self.setOcrEngine(_g_ocrEngines[ocrEngine])958 else:959 self.setOcrEngine(ocrEngine)960 if oirEngine == None:961 self.setOirEngine(_defaultOirEngine())962 else:963 if type(oirEngine) == int:964 self.setOirEngine(_g_oirEngines[oirEngine])965 else:966 self.setOirEngine(oirEngine)967 self._screenshotDir = None968 self._screenshotDirDefault = "screenshots"...

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