Exemplo n.º 1
0
    def __screendetection_get_type_internal(
        self, image, identifier
    ) -> Optional[Tuple[ScreenType, Optional[dict], int, int, int]]:
        returntype: ScreenType = ScreenType.UNDEFINED
        globaldict: Optional[dict] = {}
        diff: int = 1
        logger.debug(
            "__screendetection_get_type_internal: Detecting screen type - identifier {}",
            identifier)

        texts = []
        try:
            with Image.open(image) as frame_org:
                width, height = frame_org.size

                logger.debug("Screensize of origin {}: W:{} x H:{}".format(
                    str(identifier), str(width), str(height)))

                if width < 1080:
                    logger.info('Resize screen ...')
                    frame_org = frame_org.resize(
                        [int(2 * s) for s in frame_org.size], Image.ANTIALIAS)
                    diff: int = 2

                frame = frame_org.convert('LA')
                texts = [frame, frame_org]
                for text in texts:
                    try:
                        globaldict = pytesseract.image_to_data(
                            text,
                            output_type=Output.DICT,
                            timeout=40,
                            config='--dpi 70')
                    except Exception as e:
                        logger.error(
                            "Tesseract Error for device {}: {}. Exception: {}".
                            format(str(identifier), str(globaldict), e))
                        globaldict = None
                    logger.debug("Screentext: {}".format(str(globaldict)))
                    if globaldict is None or 'text' not in globaldict:
                        continue
                    n_boxes = len(globaldict['level'])
                    for i in range(n_boxes):
                        if returntype != ScreenType.UNDEFINED:
                            break
                        if len(globaldict['text'][i]) > 3:
                            for z in self._ScreenType:
                                if globaldict['top'][i] > height / 4 and globaldict['text'][i] in \
                                        self._ScreenType[z]:
                                    returntype = ScreenType(z)
                    if returntype != ScreenType.UNDEFINED:
                        break

                del texts
                frame.close()
        except (FileNotFoundError, ValueError) as e:
            logger.error("Failed opening image {} with exception {}", image, e)
            return None

        return returntype, globaldict, width, height, diff
Exemplo n.º 2
0
    def detect_screentype(self) -> ScreenType:
        topmostapp = self._communicator.topmostApp()
        if not topmostapp:
            return ScreenType.ERROR

        screentype, global_dict, diff = self.__evaluate_topmost_app(topmost_app=topmostapp)
        logger.info("Processing Screen: {}", str(ScreenType(screentype)))
        return self.__handle_screentype(screentype=screentype, global_dict=global_dict, diff=diff)
Exemplo n.º 3
0
    def __screendetection_get_type_internal(
        self, image, identifier
    ) -> Optional[Tuple[ScreenType, Optional[dict], int, int, int]]:
        origin_logger = get_origin_logger(logger, origin=identifier)
        returntype: ScreenType = ScreenType.UNDEFINED
        globaldict: Optional[dict] = {}
        diff: int = 1
        origin_logger.debug(
            "__screendetection_get_type_internal: Detecting screen type")

        texts = []
        try:
            with Image.open(image) as frame_org:
                width, height = frame_org.size

                origin_logger.debug("Screensize: W:{} x H:{}", width, height)

                if width < 1080:
                    origin_logger.info('Resize screen ...')
                    frame_org = frame_org.resize(
                        [int(2 * s) for s in frame_org.size], Image.ANTIALIAS)
                    diff: int = 2

                texts = [frame_org]
                for thresh in [200, 175, 150]:
                    fn = lambda x: 255 if x > thresh else 0  # noqa: E731
                    frame = frame_org.convert('L').point(fn, mode='1')
                    texts.append(frame)
                for text in texts:
                    try:
                        globaldict = pytesseract.image_to_data(
                            text,
                            output_type=Output.DICT,
                            timeout=40,
                            config='--dpi 70')
                    except Exception as e:
                        origin_logger.error(
                            "Tesseract Error: {}. Exception: {}", globaldict,
                            e)
                        globaldict = None
                    origin_logger.debug("Screentext: {}", globaldict)
                    if globaldict is None or 'text' not in globaldict:
                        continue
                    n_boxes = len(globaldict['text'])
                    for index in range(n_boxes):
                        if returntype != ScreenType.UNDEFINED:
                            break
                        if len(globaldict['text'][index]) > 3:
                            for screen_elem in self._ScreenType:
                                heightlimit = 0 if screen_elem == 21 else height / 4
                                if globaldict['top'][index] > heightlimit and globaldict['text'][index] in \
                                        self._ScreenType[screen_elem]:
                                    returntype = ScreenType(screen_elem)
                    if returntype != ScreenType.UNDEFINED:
                        break

                del texts
                frame.close()
        except (FileNotFoundError, ValueError) as e:
            origin_logger.error("Failed opening image {} with exception {}",
                                image, e)
            return None

        return returntype, globaldict, width, height, diff