Beispiel #1
0
    def _find_recognition(self, image, template, limit=None, tolerance=None):
        """Find template using recognition module."""
        if tolerance is None:
            tolerance = self._tolerance

        confidence = tolerance * 100.0
        return templates.find(image, template, limit=limit, confidence=confidence)
Beispiel #2
0
    def _find_templates(self,
                        locator: ImageTemplate) -> List[Union[Point, Region]]:
        """Internal helper method for getting image template matches in all displays
        and returning them as Regions, scaled to accomodate macOS HiDPI.
        """
        confidence = locator.confidence or self.confidence
        self.logger.info("Matching with confidence of %.1f", confidence)

        regions: List[Region] = []
        for display in all_displays():
            screenshot = take_screenshot(display)

            try:
                matches: List[Region] = templates.find(
                    image=screenshot_to_image(screenshot),
                    template=locator.path,
                    confidence=confidence,
                )
            except templates.ImageNotFoundError:
                continue

            # Calculate scaling factor for macOS, which uses virtual pixels for HiDPI.
            # Should always be 1.0 on all other platforms
            scale_factor = float(screenshot.height) / float(display.height)

            for region in matches:
                # Scale by reverse of scale factor
                region.scale(1 / scale_factor)
                # Virtual screen top-left might not be (0,0)
                region.move(display.left, display.top)

            regions.extend(matches)

        return regions
Beispiel #3
0
 def finder(image: Image.Image) -> List[Region]:
     try:
         return templates.find(
             image=image,
             template=locator.path,
             confidence=confidence,
         )
     except templates.ImageNotFoundError:
         return []
def test_find_template(region_and_template):
    region, template = region_and_template
    region = Region(*region)

    matches = templates.find(image=IMAGES / "source.png",
                             template=IMAGES / template)

    assert len(matches) == 1
    match = matches[0]
    assert match.center == region.center
Beispiel #5
0
    def find(self, locator: str) -> List[Union[Point, Region]]:
        """Internal method for resolving and searching locators."""
        if isinstance(locator, (Region, Point)):
            return [locator]

        locator = parse_locator(locator)
        self.logger.info("Using locator: %s", locator)

        if isinstance(locator, Coordinates):
            position = Point(locator.x, locator.y)
            return [position]
        elif isinstance(locator, Offset):
            position = self.ctx.get_mouse_position()
            position.offset(locator.x, locator.y)
            return [position]
        elif isinstance(locator, ImageTemplate):
            if not HAS_RECOGNITION:
                raise ValueError(
                    "Image templates not supported, please install "
                    "rpaframework-recognition module")
            # TODO: Add built-in offset support
            confidence = locator.confidence or self.confidence
            self.logger.info("Matching with confidence of %.1f", confidence)

            try:
                regions = templates.find(
                    image=self.ctx.take_screenshot(),
                    template=locator.path,
                    confidence=confidence,
                )

                # Virtual screen top-left might not be (0,0)
                left, top, _, _ = self.ctx.get_display_dimensions()
                for region in regions:
                    region.move(left, top)
            except templates.ImageNotFoundError:
                return []

            return regions
        else:
            raise NotImplementedError(f"Unsupported locator: {locator}")