Exemplo n.º 1
0
def removeChildren( src, view) :
    contours = []
    children = view.mChildren
		# black out
    mask = np.ones(src.shape, np.int8)
    thisContours = []

    if (view.contour != None) :

			# draw this contour
        contour = RectUtil.convertToParentCorrdinate(view, view.contour)
        thisContours.append(contour);
#        Imgproc.drawContours(mask, contours, -1, new Scalar(0), Core.FILLED);
    cv2.polylines(mask, thisContours, True, ColorUtil.getScalar(0),-1)


		# Extra and update x, y of children's contours and rects
    rects = []
    for child in children:
        contour = child.contour
        if (contour == None) :
            rect = RectUtil.convertToParentCorrdinate(view,child)
            rects.append(rect)
        else :
            contour = RectUtil.convertToParentCorrdinateContour(view, contour)
            contours.append(contour)
			
		
    cv2.polylines(mask, contours, True, ColorUtil.getScalar(255),cv2.FILLED)

		# Fill children's inner contours with white and outer contours with
		# black
#    Imgproc.drawContours(mask, contours, -1, new Scalar(255), Core.FILLED);
		# Fill inner rects with white and outer rects with black
    for rect in rects:
        cv2.rectangle(mask, rect.tl(), rect.br(), ColorUtil.getScalar(255), cv2.FILLED)
		

		# inverse: black -> white and white -> black
    newMask = np.zeros(src.shape,np.int8)
    cv2.bitwise_not(mask, newMask)

		# let's create a new image now
#    crop = new (src.rows(), src.cols(), CvType.CV_8UC3);

		# set background to dominate color
    width = 0 
    height = 0
    
    if len(src.shape) == 2 :
        height, width = src.shape
    else:
        height, width,channels = src.shape
    crop = np.zeros(src.shape, src.dtype)
    crop[:] = ColorUtil.getScalar(ColorUtil.findDominateColor(Rect(0, 0, width, height), src))
    np.copyto(crop, src, 'unsafe', newMask.astype(bool))
    return crop
Exemplo n.º 2
0
    def logHierarchy(self, rootView, colListmap):
        if (rootView.mType == RectViewTypes.VIEW_TYPE_TEXT):
            colListmap[rootView] = ColorUtil.cColortoInt(CColor.Red)
        elif (rootView.mType == RectViewTypes.VIEW_TYPE_IMAGE):
            colListmap[rootView] = ColorUtil.cColortoInt(CColor.Blue)
        else:
            colListmap[rootView] = ColorUtil.cColortoInt(CColor.Black)

        for child in rootView.mChildren:
            self.logHierarchy(child, colListmap)
Exemplo n.º 3
0
    def updateColorBackgroundInternal(self, rectViewParent):

        if (rectViewParent.mType == RectViewTypes.VIEW_TYPE_TEXT):
            color = ColorUtil.findDominateColorForTextView(
                rectViewParent, self.mImage)
            rectViewParent.mColor = color[0]
            rectViewParent.textColor = color[1]
        else:
            color = ColorUtil.findDominateColor(rectViewParent, self.mImage)
            rectViewParent.mColor = color
        children = rectViewParent.mChildren
        for rectView in children:
            #            if rectView.mType != RectViewTypes.VIEW_TYPE_IMAGE:
            self.updateColorBackgroundInternal(rectView)
Exemplo n.º 4
0
def removeChildrenAndCreateTransparentBackground( src, view) :
    contours = []
    children = view.mChildren
		# black out
    mask = np.ones(src.shape,np.int8)
    alpha = np.zeros(src.shape,np.int8)
    if (view.contour != None) :
			# draw this contour
            thisContours = []
            contour = RectUtil.convertToParentCorrdinate(view, view.contour)
            thisContours.append(contour)

    cv2.polylines(mask, thisContours, True, ColorUtil.getScalar(0),cv2.FILLED)

#            Imgproc.drawContours(mask, contours, -1, new Scalar(0), Core.FILLED);
		

		# Extra and update x, y of children's contours and rects
    rects = []
    for child in children:
        contour = child.contour
        if (contour == None) :
                bound = RectUtil.convertToParentCorrdinate(view, child)
                (x1,y1,width,height)= cv2.boundingRect(bound)
                rect = Rect(x1,y1,width,height)
                rects.append(rect)
        else :
            contour = RectUtil.convertToParentCorrdinate(view, contour);
            contours.append(contour)
			
		

		# Fill children's inner contours with white and outer contours with
		# black
#		Imgproc.drawContours(mask, contours, -1, new Scalar(255), Core.FILLED);
    cv2.polylines(mask, contours, True, ColorUtil.getScalar(255),cv2.FILLED)

		# Fill inner rects with white and outer rects with black
    for rect in rects:
        cv2.rectangle(mask, rect.tl(), rect.br(), ColorUtil.getScalar(255), cv2.FILLED)
		

		# inverse: black -> white and white -> black
    newMask = np.zeros(src.shape, np.int8)
    cv2.bitwise_not(mask, newMask)
    retval, alpha =   cv2.threshold(newMask,100,255,cv2.THRESH_BINARY)
    b,g,r = cv2.split(src)
    merge = cv2.merge((b,g,r,alpha))
    return merge
Exemplo n.º 5
0
    def createDocument(self):
        _map = {}
        # create root element
        rootElement = XmlUtil.createSketchRoot(
            self.mDipCalculator, LayoutHelper.FRAMELAYOUT_ELEMENT,
            self.mRootView, self.mColorWriter)
        #  To fix the style color
        color = ColorUtil.cColortoInt(CColor.Black)
        self.mColorWriter.addResource(selectColorMode(color))
        color = ColorUtil.cColortoInt(CColor.Cyan)
        self.mColorWriter.addResource(selectColorMode(color))

        # add childeren to the layout
        self.addChildrenLayout(rootElement, self.mRootView, 0, 0, _map)

        return rootElement
Exemplo n.º 6
0
    def logList(self, _id):

        # http:#www.color-hex.com/color/472300
        colorWrapper = ColorWrapper(ColorUtil.to(71, 35, 0, 0), 1)
        additionColor = ColorWrapper(ColorUtil.to(CColor.Blue), 1)
        iRectBaseViews = []
        iRectAddtionalViews = []
        for listMetadata in self.mListViews:
            for itemMetadata in listMetadata.mListItemMetadatas:
                iRectBaseViews.extend(itemMetadata.baseViews)
                iRectAddtionalViews.extend(itemMetadata.additionalViews)

        _map = {}
        _map[colorWrapper] = iRectBaseViews
        ImageUtil.log(_map, _id + "_base", self.mImage, True)
        _map[additionColor] = iRectAddtionalViews
        ImageUtil.log(_map, _id, self.mImage, True)
Exemplo n.º 7
0
    def getRandomColor(self, view):
        color = ()
        if view in self.mRectColorMapLog:
            color = self.mRectColorMapLog[view]
        else:
            color = ColorUtil.randomColorInt()
            self.mRectColorMapLog[view] = color

        return color
Exemplo n.º 8
0
def getColorWrapperBaseOnType(_type):
    color = ColorWrapper()
    if _type == RectView.VIEW_TYPE_TEXT:
        color.color = ColorUtil.cColortoInt(CColor.Red)

    elif _type == RectView.VIEW_TYPE_IMAGE:
        color.color = ColorUtil.cColortoInt(CColor.Green)

    elif _type == RectView.VIEW_TYPE_LIST_ITEM:
        color.color = ColorUtil.cColortoInt(CColor.Blue)
        color.thicknessType = 4
    elif _type == RectView.VIEW_TYPE_LIST:
        color.color = ColorUtil.cColortoInt(CColor.Orange)
        color.thicknessType = 0
    else:
        color.color = ColorUtil.cColortoInt(CColor.Black)
        color.thicknessType = 3

    return color
Exemplo n.º 9
0
    def isFullImage(self, view):
        if view.hasTextRecusive():
            return False
        if len(view.mChildren) == 0:
            return True

        if (ColorUtil.isAContainer(view, self.mImage)):
            return False
        else:
            return True
Exemplo n.º 10
0
def logDraw(_id, image, contours,randomColor= False,clearImageContent=False) :
    if not randomColor:
        logDrawColor(_id, image,contours, clearImageContent, (0,0,255))
        return
    copyImage = copy.deepcopy(image)
    width = 0 
    height = 0
    
    if len(image.shape) == 2 :
        height, width = image.shape
    else:
        height, width,channels = image.shape
    
    if (clearImageContent) :
        fillRect(copyImage, Rect(0, 0, width, height),ColorUtil.toInt(255, 255, 255, 255))
    
    for contour in contours:
        matOfPoints = []
        matOfPoints.append(contour)		
        cv2.polylines(copyImage, matOfPoints, True, ColorUtil.randomColor())
    
    drawWindow(_id,copyImage)
Exemplo n.º 11
0
    def validateWordWithAllViews(self, tv, ocrTextWrapper):
        ocrBound = ocrTextWrapper.bound()
        for view in self.mViews:
            # woa this word is big and have a lot of children, not good
            # this may okay with url or special texts
            if (RectUtil.contains(ocrBound, view.bound())
                    and len(view.getChildren()) > 0):
                tv.scalar = ColorUtil.getScalar(CColor.Cyan)
                tv.valid = False
                tv.log = "This word is big and have a lot of children"
                return

            # the box may has the word is too small compare with the word
            # itself.
            # If the word a children view which only have on child, we need
            # to verify if:
            # (1) This child view did not intersect with any other views
            # (2) This child view really small compare to the word bound
            if RectUtil.contains(ocrBound, view.bound()) and len(
                    view.getChildren()) == 0:
                # make sure this view did not intersect with other view,
                # include is accepted in this case
                hasIntersection = False
                for otherView in self.mViews:
                    if otherView != view and RectUtil.intersectsNotInclude(
                            ocrBound, otherView.bound()):
                        hasIntersection = True
                        break

                if (not hasIntersection):
                    if (RectUtil.dimesionSmallerThan(view, ocrTextWrapper,
                                                     0.8)):
                        # this is wrong, ignore this word
                        tv.scalar = CColor.Black
                        tv.valid = False
                        tv.log = "The box may has the word is too small compare with the word itself"
                        return

            # same here: the box may has the word is too big compare to the
            # word itself we also make sure that this view may also have
            # other view but it so tiny will be ignore when layout
            # itself and there is only one word in here
            if (self.areChildrenIsTooSmall(self.mDipCalculator, view)
                    and RectUtil.contains(view.bound(), ocrBound)):
                if (RectUtil.dimesionSmallerThan(ocrTextWrapper, view, 0.8)):
                    # this is wrong, ignore this word
                    tv.scalar = CColor.Pink
                    tv.valid = False
                    tv.log = "The box may has the word is too big compare to the word, and there is only one word in here. This view may also have other view but it so tiny"
                    return
Exemplo n.º 12
0
def createTransparentBackground( src, contour) :
		# black out
        mask = np.zeros(src.shape, np.int8);
        alpha = np.zeros(src.shape, np.int8);
        OfPos = []
        OfPos.add(contour);
		# Fill inner contours with white and outer contours with black
        cv2.polylines(mask, OfPos, True , ColorUtil.getScalar(255),cv2.FILLED)
		# Make the outer contour transparent
        retval, alpha =   cv2.threshold(newMask,100,255,cv2.THRESH_BINARY)
#		Imgproc.threshold(mask, alpha, 100, 255, Imgproc.THRESH_BINARY);
        b,g,r = cv2.split(src)
        merge = cv2.merge((b,g,r,alpha))
        return merge
Exemplo n.º 13
0
def logDrawColor(_id, image, contours,clearImageContent,color) :
    width = 0 
    height = 0
    
    if len(image.shape) == 2 :
        height, width = image.shape
    else:
        height, width, channels = image.shape
    
    if (clearImageContent) :
        fillRect(image, Rect(0, 0, width, height),ColorUtil.toInt(255, 255, 255, 255))
    
    
    cv2.polylines(image, contours, True, color, 2)	
    
    drawWindow(_id,image)
Exemplo n.º 14
0
def logDrawRects(rects,color, _id, image, clearImageContent = False):
    copyImage = copy.deepcopy(image)
    width = 0 
    height = 0
    
    if len(image.shape) == 2 :
        height, width = image.shape
    else:
        height, width, channels = image.shape
	 
    if (clearImageContent):
        fillRect(copyImage, Rect(0, 0, width,height), ColorUtil.toInt(255, 255, 255, 255))
    
    for entry in rects:
#        for rect in colorRectMap[entry]:
            drawRect(copyImage, entry, color)
    drawWindow(_id, copyImage)
Exemplo n.º 15
0
    def addTextToHierarchy(self, textInfo):

        #

        colListmap = {}
        colListmap[ColorWrapper(ColorUtil.cColortoInt(CColor.Red),
                                1)] = textInfo.blocksInALine
        #        if(isDebugMode)
        #        ImageUtil.logDrawMap(colListmap, "addTextToHierarchy_before_blocks", self.mImage, True)

        blocks = []
        blocks.extend(textInfo.blocksInALine)

        self.addTextToHierarchyInternal(self.hierarchyInfo.rootView, blocks)

        acceptedBlocks = []
        acceptedBlocks.extend(textInfo.blocksInALine)

        acceptedBlocks = [x for x in acceptedBlocks if x not in blocks]
        #        print(len(acceptedBlocks))
        colListmap = {}
Exemplo n.º 16
0
    def addChildrenLayout(self, element, rectView, parentLeft, parentTop,
                          rectViewElementInfoMap):
        # Setting background

        for childRectView in rectView.mChildren:
            _id = ""
            # list view has it own index

            _id = self.getId(LayoutHelper.FRAMELAYOUT_ELEMENT)
            marginLeft = childRectView.x
            marginTop = childRectView.y
            childElement = None
            childElement = XmlUtil.addElement(
                self.mDipCalculator, element,
                self.getElementTypeForRect(childRectView), childRectView,
                marginLeft, marginTop, _id, self.mColorWriter)

            rectViewElementInfoMap[childRectView] = ElementInfo(
                childElement, _id)
            self.addChildrenLayout(childElement, childRectView,
                                   childRectView.x, childRectView.y,
                                   rectViewElementInfoMap)

        XmlUtil.addBackgroundColor(element, rectView.mColor, self.mColorWriter)

        if (rectView == self.mRootView):
            pass

        # if children is icon add this attributes for code generation
        elif (rectView.isIconButton()):
            iconButtonName = rectView.getIconName()
            elementID = rectView.getElementID()
            element.tag = Constants.ELEMENT_IMAGE_BUTTON
            XmlUtil.addImageDrawable(element, iconButtonName)
            _id = self.getId(Constants.ELEMENT_IMAGE_BUTTON)

            # fit it to center
            XmlUtil.addAdditionalAttribute(element, "android:scaleType",
                                           "fitCenter")

            # add press animation
            XmlUtil.addAdditionalAttribute(element, "android:background",
                                           "@drawable/oniconpress")

            rectViewElementInfoMap[rectView] = ElementInfo(element, elementID)

        # if children is text add this attributes for code generation
        elif (rectView.isText()):
            # default text hello text
            helloText = "Hello Text"
            stringId = self.mWriter.addResource(helloText)
            element.tag = Constants.ELEMENT_TEXT_VIEW

            XmlUtil.addSize(self.mDipCalculator, element, rectView.width,
                            rectView.height)

            XmlUtil.addBackgroundColor(element, rectView.mColor,
                                       self.mColorWriter)
            element.set(Constants.ATTRIBUTE_TEXT,
                        XmlUtil.getReferenceResourceId(stringId))
            _id = self.getId(Constants.ELEMENT_TEXT_VIEW)
            XmlUtil.addId(element, _id)
            color = ColorUtil.cColortoInt(CColor.Black)
            XmlUtil.addTextColor(element, color, self.mColorWriter)
            # Set the auto text size property
            element.set(Constants.ATTRIBUTE_AUTOSIZE_TEXT_TYPE, "uniform")
            rectViewElementInfoMap[rectView] = ElementInfo(element, _id)

        # for all other UI elements
        else:
            # for checkbox
            if (rectView.isCheckbox()):
                _id = self.getId(Constants.ELEMENT_CHECK_BOX)
                element.tag = Constants.ELEMENT_CHECK_BOX
                # set checkbox default to uncheck
                XmlUtil.addAdditionalAttribute(element, "android:button",
                                               "@null")
                XmlUtil.addAdditionalAttribute(element, "app:theme",
                                               "@style/CheckboxStyle")
                XmlUtil.addAdditionalAttribute(
                    element, "android:background",
                    "?android:attr/listChoiceIndicatorMultiple")

                # for Toggle
            elif (rectView.isToogle()):
                element.tag = Constants.ELEMENT_SWITCH
                _id = self.getId(Constants.ELEMENT_SWITCH)
                minWidthDp = str(
                    self.mDipCalculator.pxToWidthDip(rectView.width) -
                    12) + Constants.UNIT_DIP
                XmlUtil.addAttribute(element, "android:switchMinWidth",
                                     minWidthDp)

            # for Slider
            elif (rectView.isSlider()):
                element.tag = Constants.ELEMENT_SEEK_BAR
                color = ColorUtil.cColortoInt(CColor.Black)
                XmlUtil.addAttributeColor(element, "android:progressTint",
                                          color, self.mColorWriter)
                XmlUtil.addAttributeColor(element, "android:thumbTint", color,
                                          self.mColorWriter)
                _id = self.getId(Constants.ELEMENT_SEEK_BAR)
                XmlUtil.addBackgroundColor(element, rectView.mColor,
                                           self.mColorWriter)

            # for star convert it to raing
            elif (rectView.isRating()):

                element.tag = Constants.ELEMENT_RATING_BAR
                XmlUtil.addAttribute(element, "android:layout_width",
                                     "wrap_content")
                XmlUtil.addAttribute(element, "android:layout_height",
                                     "wrap_content")
                XmlUtil.addAttribute(element, "android:theme",
                                     "@style/RatingBar")
                # based on the width of the element set number of star in rating
                widthOfStar = int(
                    self.mDipCalculator.pxToWidthDip(rectView.width) / 50)
                _id = self.getId(Constants.ELEMENT_RATING_BAR)
                XmlUtil.addAttribute(element, "android:numStars",
                                     str(widthOfStar))

            # for Searchbar
            elif (rectView.isSearchBar()):
                element.tag = Constants.ELEMENT_SEARCH_BAR
                _id = self.getId(Constants.ELEMENT_SEARCH_BAR)
                XmlUtil.addBackgroundColor(element, rectView.mColor,
                                           self.mColorWriter)

            # for Searchbar
            elif (rectView.isUserImage()):
                element.tag = Constants.ELEMENT_IMAGE_VIEW
                iconButtonName = "userimage"
                XmlUtil.addImageDrawable(element, iconButtonName)
                _id = self.getId(Constants.ELEMENT_IMAGE_VIEW)

                # for Container
            elif (rectView.isContainer()):
                XmlUtil.addAttribute(element, "android:background",
                                     "@drawable/border")
                _id = self.getId(LayoutHelper.FRAMELAYOUT_ELEMENT)

                # for Dropdown
            elif (rectView.isDropDown()):
                element.tag = Constants.ELEMENT_SPINNER
                XmlUtil.addAdditionalAttribute(element,
                                               "android:drawSelectorOnTop",
                                               "true")
                arrayId = self.mWriter.addArrayResource("default")
                # create a dummy array for the dropdown
                element.set(Constants.ATTRIBUTE_DROPDOWN_ENTRIES,
                            XmlUtil.getArrayReferenceResourceId(arrayId))
                _id = self.getId(Constants.ELEMENT_SPINNER)
                element.attrib.pop("android:background", None)

                # for Text Button
            elif (rectView.isButtonText()):
                element.tag = Constants.ELEMENT_BUTTON
                XmlUtil.addAdditionalAttribute(element, "android:text",
                                               "Button")
                _id = self.getId(Constants.ELEMENT_BUTTON)
                XmlUtil.addAdditionalAttribute(element, "android:background",
                                               "@drawable/oniconpress")

            XmlUtil.addId(element, _id)

            rectViewElementInfoMap[rectView] = ElementInfo(element, _id)
Exemplo n.º 17
0
def getRandomColor():
    color = ColorUtil.alphaColortoInt( ColorUtil.randomColor())
    return toHtmlColor(color)
Exemplo n.º 18
0
    def processText(self, color):
        ocrTextWrappers = self.mOcr.mOcrTextWrappers
        width = 0
        height = 0
        copyImage = copy.deepcopy(self.mRgbaImage)

        if len(copyImage.shape) == 2:
            height, width = copyImage.shape
        else:
            height, width, channels = copyImage.shape

#        ocrOnlyProcessingStepImage = copy.deepcopy(self.mRgbaImage)

        acceptedOcrTextWrappers = []
        ruleManager = FilterRuleManager(self.mDipCalculator, self.mOcr,
                                        self.mRgbaImage, ocrTextWrappers,
                                        self.mViews)
        invalidTexts = {}

        for ocrTextWrapper in ocrTextWrappers:

            textValidator = ruleManager.acceptOCRRules(ocrTextWrapper)
            if textValidator != None and not textValidator.valid:
                invalidTexts[ocrTextWrapper] = textValidator

#                if(self.isDebugMode) :
#                    cv2.rectangle(ocrOnlyProcessingStepImage, ocrTextWrapper.bound().tl(), ocrTextWrapper.bound().br(),   CColor.Red, 2)
            else:
                acceptedOcrTextWrappers.append(ocrTextWrapper)
#                if(self.isDebugMode) :
#                    cv2.rectangle(ocrOnlyProcessingStepImage, ocrTextWrapper.bound().tl(), ocrTextWrapper.bound().br(), CColor.Blue, 2)

        ruleManager.acceptVisionRules(invalidTexts, acceptedOcrTextWrappers)

        validTexts = []
        validTexts.extend(acceptedOcrTextWrappers)
        validTexts = [x for x in validTexts if x not in invalidTexts]

        #        ImageUtil.drawWindow( "basic Text",ocrOnlyProcessingStepImage)

        ocrLineWrappers = self.mOcr.mOcrLineWrappers
        # sort top bottom
        copyLines = []
        copyLines.extend(ocrLineWrappers)

        copyLines.sort(key=cmp_to_key(RectUtil.getTopBottomComparator))

        validLines = []
        addedWords = []
        for ocrLineWrapper in copyLines:
            words = []
            line = OCRTextWrapper.OCRTextWrapper(ocrLineWrapper)
            for ocrWordWrapper in validTexts:
                if (ocrWordWrapper not in addedWords) and RectUtil.contains(
                        ocrLineWrapper.bound(), ocrWordWrapper.bound()):
                    words.append(ocrWordWrapper)
                    addedWords.append(ocrWordWrapper)
            # Some line contain 2 words which are vertically alignment
            if len(words) > 0:
                notHorizontalAlignmentWords = self.getNotHorizontalAlignmentWords(
                    words)
                if len(notHorizontalAlignmentWords) == 0:
                    validLines.append(line)

                    words.sort(key=cmp_to_key(RectUtil.getLeftRightComparator))
                    line.words = words
                else:
                    # Take it from addedWords. This will help these words be
                    # added to other lines, since this line is invalid
                    addedWords = [
                        x for x in addedWords
                        if x not in notHorizontalAlignmentWords
                    ]
                    # remove bad guy
                    words = [
                        x for x in words
                        if x not in notHorizontalAlignmentWords
                    ]
                    validLines.append(line)
                    words.sort(key=cmp_to_key(RectUtil.getLeftRightComparator))
                    line.words = words
        # We still want to add word as line when it did not get add to any
        # lines

        remainWords = []
        remainWords.extend(validTexts)
        remainWords = [x for x in remainWords if x not in addedWords]

        for word in remainWords:
            # System.out.println("Remain words: " + word);
            if (word.confidence < 90
                    and not self.mOcr.isValidTextUsingBoundaryCheck(word)):
                continue

            line = OCRTextWrapper.OCRTextWrapper(word)
            words = []
            words.append(word)
            line.words = words
            validLines.append(line)

        validLines.sort(key=cmp_to_key(RectUtil.getTopBottomComparator))
        #        self.log("ValidLines", validLines, CColor.Red)

        for ocrLineWrapper in validLines:
            rect = ocrLineWrapper.reCalculateBoundBaseOnWordList()
            if (rect == None):
                print("Error with line, there is no more text: " +
                      ocrLineWrapper.text)
                #System.out.println("Error with line, there is no more text: "+ ocrLineWrapper.getText());
            else:
                text = self.mOcr.getText(rect)
                ocrLineWrapper.text = text
                ocrLineWrapper.rect = rect

        # word is sort from left to right

        for ocrLineWrapper in validLines:
            blocks = [[]]
            words = ocrLineWrapper.words
            currentBlock = []
            if len(words) > 0:
                currentBlock.append(words[0])
                for i in range(len(words) - 1):
                    nextWord = words[i + 1]
                    currentWord = words[i]
                    xDistance = nextWord.x - (currentWord.x +
                                              currentWord.width)
                    xDistanceThreshold = int(
                        Constants.WORD_SPACE_THRESHOLD_BASE_ON_HEIGHT * float(
                            min(currentWord.bound().height,
                                nextWord.bound().height)))
                    fontDiff = abs(currentWord.fontSize - nextWord.fontSize)
                    #                    if (xDistance <= xDistanceThreshold and fontDiff <= 1) :
                    if (xDistance <= xDistanceThreshold):
                        currentBlock.append(nextWord)
                    else:
                        blocks.append(currentBlock)
                        currentBlock = []
                        currentBlock.append(nextWord)

                if currentBlock not in blocks:
                    blocks.append(currentBlock)

            ocrLineWrapper.blocks = blocks

#        logImageWithValidTextBox = copy.deepcopy(self.mRgbaImage)
#        ImageUtil.fillRect(logImageWithValidTextBox, Rect(0, 0, width,height), ColorUtil.toInt(255, 255, 255, 255))

        blocksInline = []

        for lineOCR in validLines:
            blocks = lineOCR.blocks
            for listWord in blocks:
                if len(listWord) > 0:
                    firstWord = listWord[0]
                    rect = RectUtil.findBoundRectangle(listWord)
                    #                    cv2.rectangle(logImageWithValidTextBox, rect.tl(),
                    #							rect.br(), CColor.Red, 2);
                    block = OCRTextWrapper.OCRTextWrapper(firstWord)
                    block.words = listWord
                    block.width = rect.width
                    block.height = rect.height
                    block.rect = rect
                    lineText = ""
                    #                    rect = Rect(rect.x -2, rect.y-2, rect.width +2, rect.height +2)
                    #                    if len(listWord) == 1 :
                    #                        lineText = listWord[0].text
                    #                    else :
                    #                        # override text
                    lineText = self.mOcr.getLineText(rect)
                    block.text = lineText
                    # will ignore this block if it contains only invisible
                    # chars
                    #                    blocksInline.append(block)
                    if (RuleAllSpace.containAllSpacesOrInvalidChars(lineText)):
                        blocksInline.append(block)
        colListmap = {}
        for blocks in blocksInline:
            colListmap[ColorWrapper(ColorUtil.cColortoInt(CColor.Red),
                                    1)] = blocks

#        ImageUtil.logDrawMap(colListmap, "Text Block", self.mRgbaImage)

        textInfo = TextInfo()
        textInfo.lines = validLines
        textInfo.blocksInALine = blocksInline
        textInfo.blocksInALine.sort(
            key=cmp_to_key(RectUtil.getTopBottomComparator))

        #mSreenshotProcessor.getTimerManager().log(Constants.TIMER_ID_SPLIT_LINE_INTO_TEXT_BOXES);
        return textInfo
Exemplo n.º 19
0
    def addChildrenLayout(self, element, rectView, parentLeft, parentTop,
                          rectViewElementInfoMap):
        # Setting background
        if (self.useTransparentBackground(rectView)):
            XmlUtil.addBackgroundColor(element,
                                       ColorUtil.toInt(0, 255, 255, 255),
                                       self.mColorWriter)
#        elif (RectViewTypes.isContanerView(rectView)) :
# We always want to genenate background regard less of the respect
# ratioand Environment.get().getValue(Environment.KEY_KEEP_ASPECT_RATIO) == Boolean.TRUE
# We will not do this if ratio is between input image and output is the same
#            bound = ImageUtil.getImageFromRect(self.mImage, rectView)
#            # We remove children here using 4 layer channel So we have to make sure that when we save it we will not Adding anymore layer
#            newImageBackground = ImageUtil.removeChildren(bound, rectView)
#            drawableId = self.mDrawableWriter.addResourceDirectly(newImageBackground, rectView)
#            XmlUtil.addBackroundImage(element, drawableId)

        else:
            XmlUtil.addBackgroundColor(element, rectView.mColor,
                                       self.mColorWriter)

        for childRectView in rectView.mChildren:
            _id = ""
            # list view has it own index
            if (childRectView.mType == RectViewTypes.VIEW_TYPE_LIST):
                _id = childRectView.mListInfo.xmlId
            else:
                _id = self.getId(LayoutHelper.FRAMELAYOUT_ELEMENT,
                                 childRectView)

            marginLeft = childRectView.x - parentLeft
            marginTop = childRectView.y - parentTop
            childElement = None
            if (self.useTransparentBackground(childRectView)):
                childElement = XmlUtil.addElement(
                    self.mDipCalculator, element,
                    self.getElementTypeForRect(childRectView), childRectView,
                    marginLeft, marginTop, _id)
            else:
                childElement = XmlUtil.addElement(
                    self.mDipCalculator, element,
                    self.getElementTypeForRect(childRectView), childRectView,
                    marginLeft, marginTop, _id, self.mColorWriter)

            rectViewElementInfoMap[childRectView] = ElementInfo(
                childElement, _id)
            self.addChildrenLayout(childElement, childRectView,
                                   childRectView.x, childRectView.y,
                                   rectViewElementInfoMap)

        # image view
        if (rectView.mType == RectViewTypes.VIEW_TYPE_IMAGE):
            drawableId = self.interestedIcons.get(rectView.mImageInfo.iconInfo)
            element.tag = Constants.ELEMENT_IMAGE_VIEW
            XmlUtil.addImageDrawable(element, drawableId)
            # override attributes
            XmlUtil.removeAttribute(element, Constants.ATTRIBUTE_BACKGROUND)
            _id = self.getId(Constants.ELEMENT_IMAGE_VIEW, rectView)
            XmlUtil.addId(element, _id)
            XmlUtil.addScaleType(element, "fitXY")
            rectViewElementInfoMap[rectView] = ElementInfo(element, _id)
        elif (rectView.mType == RectViewTypes.VIEW_TYPE_TEXT):

            textWrapper = rectView.mTextInfo.textWrapper
            stringId = self.mWriter.addResource(textWrapper.text)
            rectView.mTextInfo._id = stringId
            element.tag = Constants.ELEMENT_TEXT_VIEW
            if (Environment.getValue(
                    Environment.KEY_TEXT_WIDTH_WRAP_CONTENT) == True):
                XmlUtil.addSize(self.mDipCalculator, element,
                                Constants.ATTRIBUTE_WRAP_CONTENT,
                                rectView.height)
            else:
                XmlUtil.addSize(self.mDipCalculator, element, rectView.width,
                                rectView.height)

            XmlUtil.addBackgroundColor(element, rectView.mColor,
                                       self.mColorWriter)
            element.set(Constants.ATTRIBUTE_TEXT,
                        XmlUtil.getReferenceResourceId(stringId))
            _id = self.getId(Constants.ELEMENT_TEXT_VIEW, rectView)
            XmlUtil.addId(element, _id)
            textAttributes = textWrapper.getTextAttributes(
                self.mOcr, rectView.height)
            XmlUtil.addTextColor(element, rectView.textColor,
                                 self.mColorWriter)
            stypeId = self.mStyleWriter.addResource(textAttributes)
            element.set(Constants.ATTRIBUTE_STYLE,
                        XmlUtil.getReferenceStyleId(stypeId))
            rectViewElementInfoMap[rectView] = ElementInfo(element, _id)
Exemplo n.º 20
0
    def pruneBasicInternal(self, parent, view):
        # TODO: if this view is too small and it has no children,so we don't
        # need them
        if self.mDipCalculator.isViewToBeIgnore(view.width, view.height):
            if (parent != None):
                parent.mChildren.remove(view)
            return
#
#
        allChildrenAreTooSmall = self.isAllChildrenTooSmall(view)

        if not allChildrenAreTooSmall and len(view.mChildren) != 0:
            removedChildren = []
            for childView in view.mChildren:
                if self.mDipCalculator.isViewToBeIgnore(
                        childView.width, childView.height):
                    removedChildren.append(childView)

            view.mChildren = [
                x for x in view.mChildren if x not in removedChildren
            ]
            for childView in view.mChildren:
                self.pruneBasicInternal(view, childView)
#                        # add this drawable if we did not want to show any children
#        # here
#
        isAImageView = self.isFullImage(view)
        if isAImageView:
            #        if not view.hasText() and allChildrenAreTooSmall and len(view.mChildren) == 0 :
            currentMat = ImageUtil.getImageFromRect(self.mImage, view.bound())
            iconInfo = IconInfo(currentMat)
            drawableId = ""
            if iconInfo in self.interestedIcons:
                drawableId = self.interestedIcons[iconInfo]
            viewsSameDrawable = None
            if (TextUtils.isEmpty(drawableId)):
                drawableId = self.mDrawableWriter.addResourceDirectly(
                    currentMat, view)
                self.interestedIcons[iconInfo] = drawableId
                viewsSameDrawable = []
                self.mDrawableMap[drawableId] = viewsSameDrawable
            else:
                viewsSameDrawable = self.mDrawableMap[drawableId]

            view.mType = RectViewTypes.VIEW_TYPE_IMAGE
            view.mImageInfo.iconInfo = iconInfo
            view.mImageInfo.drawableId = drawableId
            view.mChildren = []
            viewsSameDrawable.append(view)
        elif view.hasTextRecusive():
            # process text view
            textWithLocations = view.mTextWithLocations
            view.mColor = ColorUtil.findDominateColor(view, self.mImage)
            for textWrapper in textWithLocations:
                newHeight = TesseractOCR.increaseHeight(textWrapper.height)
                textView = textWrapper.boundRectView
                newY = textView.y - (newHeight - textView.height) / 2.0
                textView.y = newY
                textView.x = textWrapper.x
                textView.width = textWrapper.width
                textView.height = newHeight
                textView.mType = RectViewTypes.VIEW_TYPE_TEXT
                textView.mTextInfo.textWrapper = textWrapper
                textView.rect = Rect(textView.x, textView.y, textWrapper.width,
                                     textView.height)
                color = ColorUtil.findDominateColorForTextView(
                    textView, self.mImage)
                textView.mColor = color[0]
                textView.textColor = color[1]
                #                textView.mColor = ColorUtil.findDominateColor(textView,self.mImage)
                view.addChild(textView)

#                currentMat = ImageUtil.getImageFromRect(self.mImage, textView.bound())
#                iconInfo =  IconInfo(currentMat)
#                drawableId = ""
#                if iconInfo in self.interestedIcons:
#                    drawableId = self.interestedIcons[iconInfo]
#                    viewsSameDrawable = None
#                if (TextUtils.isEmpty(drawableId)) :
#                    drawableId = self.mDrawableWriter.addResourceDirectly(currentMat,view)
#                    self.interestedIcons[iconInfo] = drawableId
#                    viewsSameDrawable = []
#                    self.mDrawableMap[drawableId] =  viewsSameDrawable
#                else :
#                    viewsSameDrawable = self.mDrawableMap[drawableId]
#
#                textView.mType = RectViewTypes.VIEW_TYPE_IMAGE
#                textView.mImageInfo.iconInfo = iconInfo
#                textView.mImageInfo.drawableId = drawableId
#                textView.mChildren = []
#                viewsSameDrawable.append(textView)

# Update Bound of parent text view
            if len(view.mChildren) > 0:
                allViews = []
                allViews.extend(view.mChildren)
                allViews.append(view)
                view.bound = RectUtil.findBoundRectangle(allViews)