Beispiel #1
0
    def createListItemForView(self, rectView, baseLists,
                              defaultThresholdAlignment):
        # Check to see if there is a list of drawable belong to one view
        # then we remove this list
        # We only find one for now
        matchedBaseLists = []
        removedList = []
        for drawableList in baseLists:
            if (RectUtil.containAll(rectView, drawableList)):
                matchedBaseLists.append(ListWrapper(drawableList))
                removedList.append(ListWrapper(drawableList))

        # First remove approval list
        baseLists = [x for x in baseLists if x not in removedList]
        removedList = []
        if len(matchedBaseLists) > 0:
            for matchedBaseList in matchedBaseLists:
                # System.out.prln("Found a list of: " + rectView.bound()
                # + ", list: " + matchedBaseList.size() + ", "
                # + matchedBaseList)
                # First make sure these are repeated icons of list items
                # Make sure they align either LEFT-RIGHT or TOP-BOTTOM
                checkValidList = False
                alignmentType = RectUtil.getAlignmentType(
                    matchedBaseList._list, defaultThresholdAlignment)
                matchedBaseList.alignmentType = alignmentType
                if (alignmentType == RectUtil.ALIGNMENT_RIGHT):
                    # System.out.prln("Align LEFT and RIGHT")
                    # We sort them vertically
                    matchedBaseList._list.sort(
                        key=cmp_to_key(RectUtil.getTopBottomComparator()))
                    if len(matchedBaseList
                           ) >= Constants.LAYOUT_MIN_ACCEPTABLE_LIST_SIZE:
                        distances = set()
                        for i in range(len(matchedBaseList)):
                            distances.add(
                                RectUtil.verticalDistance(
                                    matchedBaseList._list[i + 1],
                                    matchedBaseList._list[i]))

                            if len(distances) == 1:
                                # Perfect
                                checkValidList = True
                            elif len(distances) >= 2:
                                # it is okay to have more than 2 distance, but the
                                # different cannot too much
                                # we set here is <= half of the biggest distance
                                satisfy = True
                                lDistances = []
                                for i in range(len(lDistances) - 1):
                                    first = lDistances[i]
                                    second = lDistances[i + 1]
                                    difference = abs(first - second)
                                    if (difference >= max(first, second) / 2):
                                        satisfy = False
                                        break
                                checkValidList = satisfy

                    else:
                        checkValidList = False

                if (alignmentType == RectUtil.ALIGNMENT_BOTTOM):
                    # System.out.prln("Align TOP and BOTTOM")
                    # We sort them horizontally
                    matchedBaseList._list.sort(
                        key=cmp_to_key(RectUtil.getLeftRightComparator()))
                    if len(matchedBaseList
                           ) >= Constants.LAYOUT_MIN_ACCEPTABLE_LIST_SIZE:
                        distances = set()
                        for i in range(len(matchedBaseList) - 1):
                            distances.add(
                                RectUtil.horizontalDistance(
                                    matchedBaseList._list[i + 1],
                                    matchedBaseList._list[i]))

                        if len(distances) == 1:
                            # Perfect
                            checkValidList = True
                        elif len(distances) >= 2:
                            # it is okay to have more than 2 distance, but the
                            # different cannot too much
                            # we set here is <= half of the biggest distance
                            satisfy = True
                            lDistances = []
                            for i in range(lDistances) - 1:
                                first = lDistances[i]
                                second = lDistances[i + 1]
                                difference = abs(first - second)
                                if (difference >= max(first, second) / 2):
                                    satisfy = False
                                    break

                        checkValidList = satisfy

                    else:
                        checkValidList = False

                # Now put them in a groups
                if (not checkValidList):
                    removedList.append(matchedBaseList)

            # We only support one list for now, and only allow has the same
            # size
            #   maxSize = RectUtil
            # .maxSizeListWrapper(matchedBaseLists)
            #
            # for ( ListWrapper list : matchedBaseLists) :
            # if (list.size() != maxSize) :
            # removedList.add(list)
            #
            #
        matchedBaseLists = [
            x for x in matchedBaseLists if x not in removedList
        ]

        # Split list to multiple with different alignment type
        mapTypeList = {}
        for listWrapper in matchedBaseLists:
            ls = mapTypeList[listWrapper.alignmentType]
            if (ls == None):
                ls = []
                mapTypeList[listWrapper.alignmentType] = ls
            ls.append(listWrapper)

#            We have enough base item here already
# Now we try to get as much as ImageView base, as possible

        for entry in mapTypeList:
            entryKey = [x for x in entry]
            entryValue = [entry(x) for x in entry]
            maxSize = RectUtil.maxSizeListWrapper(mapTypeList[entry])
            newLists = []
            for listWrapper in entryValue:
                if len(listWrapper) == maxSize:
                    newLists.append(listWrapper)

            if len(newLists) <= 1:
                # System.out
                # .prln("*We need more list here let get the list = size - 1")
                # We need more list here let get the list = size - 1
                for listWrapper in entryValue:
                    if len(listWrapper) == maxSize - 1:
                        newLists.append(listWrapper)

            self.prepareCreateListView(rectView, newLists, entryKey)
Beispiel #2
0
    def removeInvalidTextBaseOnNeighbours(self, acceptedOcrTextWrappers,
                                          invalidTexts):
        validTexts = []
        validTexts.extend(acceptedOcrTextWrappers)

        for invText in invalidTexts:
            if invText in validTexts:
                validTexts.remove(invText)

        moreInvalidText = []

        groups = GroupUtil.group(self.mViews, self.sameGroup)
        for group in groups:
            if len(
                    group
            ) >= Constants.TEXT_TO_BECOME_IMAGE_MIN_ACCEPTABLE_LIST_SIZE:
                alignmentType = RectUtil.getAlignmentType(
                    group, self.mDefaultAlignThreshold)
                if alignmentType != RectUtil.ALIGNMENT_UNKNOWN:
                    skipGroup = False
                    # if all text exactly the same and have invalid text, we
                    # kill them all.
                    sameText = ""
                    inValidTextsInGroup = set()
                    for i in range(len(group)):
                        rectView = group[i]
                        texts = RectUtil.contain(rectView, validTexts)
                        # We only test views have one text
                        # And not "intersect not include" with other valid text
                        if len(
                                texts
                        ) == 1 and RectUtil.countIntersectNotIncludeRect(
                                rectView, validTexts) == 0:
                            # If the confident is acceptable but boundary is
                            # wrong, only one character is accept here
                            text = texts[0].text
                            # //TODO                            if self.mTesseractOCR.validWord(text) or len(text) > 1:
                            if len(text) > 1:
                                skipGroup = True
                                break

                            if i == 0:
                                sameText = text
                            elif not sameText == text:
                                skipGroup = True
                                break

                            # if not add to group
                            inValidTextsInGroup.add(texts[0])
                            # System.out.println("Added: " + texts);
                        else:
                            skipGroup = True
                            break

                    if not skipGroup:
                        if len(inValidTextsInGroup) == len(group):
                            stillInvalid = False
                            for textWrapper in inValidTextsInGroup:
                                if textWrapper.confidence < Constants.TEXT_TO_BECOME_IMAGE_IN_LIST_THRESHOLD or not self.mTesseractOCR.validWord(
                                        textWrapper.getText()):
                                    stillInvalid = True
                                    break

                            if stillInvalid:
                                moreInvalidText.extend(inValidTextsInGroup)
                                continue

                        # We will check other rules after this
                        skipGroup = True

                    mapCountTexts = {}

                    if skipGroup:
                        for rectView in group:
                            texts = RectUtil.contain(rectView, validTexts)
                            # We only test views have one text
                            # And not "intersect not include" with other valid
                            # text
                            if len(
                                    texts
                            ) >= 2 or RectUtil.countIntersectNotIncludeRect(
                                    rectView, validTexts) > 0:
                                skipGroup = True
                                break
                            else:
                                mapCountTexts[rectView] = texts

                    if skipGroup:
                        continue

                    else:
                        countEmptyView = 0
                        uniqueInvalidRectView = None
                        for rectView in group:
                            if len(mapCountTexts.get(rectView)) == 0:
                                countEmptyView = countEmptyView + 1
                            else:
                                uniqueInvalidRectView = rectView
                        if len(
                                group
                        ) == countEmptyView + 1 and uniqueInvalidRectView != None:
                            moreInvalidText.extend(
                                mapCountTexts.get(uniqueInvalidRectView))

        return moreInvalidText