def groupViewsForList(self, parentView, newChildren, viewTypeRect, minChidren): if len(newChildren) >= minChidren: bound = RectUtil.findBoundRectangle(newChildren) newParent = RectView(bound, None) newParent.mType = viewTypeRect # replace the parent at the location of the first child indexOf = parentView.mChildren.index(newChildren[0]) if indexOf > 0 and indexOf < len(parentView.mChildren): parentView.mChildren[indexOf] = newParent else: parentView.mChildren.append(newParent) # Now remove the rest parentView.mChildren = [ x for x in parentView.mChildren if x not in newChildren ] # Make sure there is no view is hidden under the new parent insideViews = RectUtil.contain(newParent, parentView.mChildren) parentView.mChildren = [ x for x in parentView.mChildren if x not in insideViews ] indexOfNewParent = parentView.mChildren.index(newParent) if (indexOfNewParent == len(parentView.mChildren) - 1): parentView.mChildren.extend(insideViews) else: parentView.mChildren.extend(indexOfNewParent + 1, insideViews) return newParent return None
def findExternalContours(self, imageAfterClearProcessedView, contours, hierarchy, index, level, parent, sibling): i = index if (i < 0): return while (i >= 0): buff = hierarchy[0][i] contour = contours[i] i = buff[0] #// Get the next id contour j = buff[2] # // child index children = [] if (j > 0): while (j >= 0): internalContoursBuff = hierarchy[0][j] self.findExternalContours(imageAfterClearProcessedView, contours, hierarchy, internalContoursBuff[2], level + 1, contour, children) j = internalContoursBuff[0] currentView = None (x1, y1, width, height) = cv2.boundingRect(contour) currentView = RectView(Rect(x1, y1, width, height)) # currentView = self.processRectangle(imageAfterClearProcessedView, contour) #print(len(sibling)) sibling.append(currentView) if (currentView != None): currentView.addAllChild(children)
def analyze(self, imgData, foundContours): imageWithContour = np.empty_like(imgData) imageWithContour[:] = imgData imageAfterClearProcessedView = np.empty_like(imgData) imageAfterClearProcessedView[:] = imgData height, width = imgData.shape rect = Rect(0, 0, width, height) rootView = RectView(rect, None) children = [] self.findExternalContours(imageAfterClearProcessedView, foundContours['contours'], foundContours['hierarchy'], 0, 0, None, children) for rawView in children: rootView.addChild(rawView) contourInfo = ContourInfo() contourInfo.imageAfterClearProcessedView = imageAfterClearProcessedView contourInfo.rootView = rootView map_data = {} contourInfo.rects = self.creatRects(rootView, map_data) contourInfo.map_data = map_data return contourInfo
def createListLayoutCode(self, listMetadata, index, listView): baseListItemMetadata = listMetadata.mListItemMetadatas[0] # document = XmlUtil.createDocument() rootView = RectView(baseListItemMetadata.bound, None) for rectView in baseListItemMetadata.baseViews: rootView.addChild(rectView) for rectView in baseListItemMetadata.additionalViews: rootView.addChild(rectView) rootView.mChildren.sort( key=cmp_to_key(RectUtil.getTopBottomComparator())) rootView.mChildren.sort( key=cmp_to_key(RectUtil.getLeftRightComparator())) rootElement = XmlUtil.createRoot(self.mDipCalculator, FRAMELAYOUT_ELEMENT, rootView, self.mColorWriter) _map = {} self.addChildrenLayout(rootElement, rootView, listView.x, listView.y, _map) rectViews = RectUtil.toRects(rootView) rectViews.remove(rootView) XmlUtil.writeDocument( rootView, self.mOutProjectFolder + Constants.DEFAULT_LAYOUT_PATH + "/" + Constants.DEFAULT_LAYOUT_LIST_PREFIX + index + ".xml")
def createListLayoutCode(self, listView, listMetadata, aditionalListViewItemData, index): if len(listMetadata.getListItemMetadatas()) == 0: return baseListItemMetadata = listMetadata.getListItemMetadatas()[0] # Document document = XmlUtil.createDocument() rootView = RectView(baseListItemMetadata.bound, None) for rectView in baseListItemMetadata.baseViews: rootView.addChild(rectView) for rectView in baseListItemMetadata.additionalViews: rootView.addChild(rectView) rootView.mChildren.sort( key=cmp_to_key(RectUtil.getTopBottomComparator())) rootView.mChildren.sort( key=cmp_to_key(RectUtil.getLeftRightComparator())) rootElement = XmlUtil.createRoot(self.mDipCalculator, self.FRAMELAYOUT_ELEMENT, rootView, self.mColorWriter) _map = {} self.addChildrenLayout(rootElement, rootView, listView.getX(), listView.getY(), _map) rectViews = RectUtil.toRects(rootView) rectViews.remove(rootView) viewIdMap = {} # For baseview for rectView in baseListItemMetadata.baseViews: elementInfo = _map[rectView] viewIdMap[rectView] = elementInfo._id # For additionview for rectViewWrapper in aditionalListViewItemData: elementInfo = _map[rectViewWrapper.view] viewIdMap[rectViewWrapper.view] = elementInfo.id # TODO: since we checked already (#getAditionalListViewItemData), all # base view should only image views resourceInfoMap = {} listItemMetadatas = listMetadata.getListItemMetadatas() for listItemMetadata in listItemMetadatas: for baseView in listItemMetadata.baseViews: resourceInfoMap[baseView] = self.getInfoResource(baseView) for rectViewWrapper in aditionalListViewItemData: for viewWrapper in rectViewWrapper.relativeViews: resourceInfoMap[viewWrapper.view] = self.getInfoResource( viewWrapper.view) listMetadata.generateCode(index, listView.getListInfo().xmlId, self.mOutProjectFolder, aditionalListViewItemData, viewIdMap, resourceInfoMap) # We need to store current x, y, w, h of element before we loss # them anotateMap = LayoutFilter.anotate(rootView) layoutFilter = RelativeLayoutFilter() layoutFilter.doFilter(rootView, anotateMap) XmlUtil.writeDocument( rootView, self.mOutProjectFolder + Constants.DEFAULT_LAYOUT_PATH + "/" + Constants.DEFAULT_LAYOUT_LIST_PREFIX + index + ".xml")
def delOverlapViews(self, view): overlapIndexes = [] children = view.mChildren for rawView in children: self.delOverlapViews(rawView) removingChildrenIndexes = [False] * len(children) for i in range(len(children)): rectView = children[i] for j in range(i + 1, len(children)): otherRectView = children[j] if rectView != otherRectView: #Check if Intersects only if (not RectUtil.contains(rectView.bound(), otherRectView.bound()) and not RectUtil.contains(otherRectView.bound(), rectView.bound()) and RectUtil.intersects(rectView.bound(), otherRectView.bound())): added = False #Check if already in the overlapIndexes for overlapIndex in overlapIndexes: if (i in overlapIndex and j in overlapIndex): added = True break elif i in overlapIndex: overlapIndex.append(j) added = True break elif j in overlapIndex: overlapIndex.append(i) added = True break # did not contain non of them them add two of them as a list within overlapIndexes if (not added): rectViews = [] rectViews.append(i) rectViews.append(j) overlapIndexes.append(rectViews) removingChildrenIndexes[i] = True removingChildrenIndexes[j] = True newRectViews = [] # process overlap rect for rectViewIndexes in overlapIndexes: #find bound rectView = children[rectViewIndexes[0]] unionRect = rectView.bound() for i in range(1, len(rectViewIndexes)): r = children[rectViewIndexes[i]] unionRect = RectUtil.union(unionRect, r.bound()) newRectViewParent = RectView( Rect(unionRect.x, unionRect.y, unionRect.width, unionRect.height), None) for index in rectViewIndexes: newRectViewParent.addChild(children[index]) removingChildrenIndexes[rectViewIndexes[0]] = False view.mChildren[rectViewIndexes[0]] = newRectViewParent newRectViews.append(newRectViewParent) # now we make sure all other children of the parent inside union rect # go under this view too. for rectView in newRectViews: for i in range(len(removingChildrenIndexes)): rawView = children[i] if rawView != rectView and not removingChildrenIndexes[i]: bound = children[i].bound() if (RectUtil.contains(rectView.bound(), bound)): rectView.addChild(children[i]) removingChildrenIndexes[i] = True # now update the children rawViews = [] for i in range(len(removingChildrenIndexes)): if (not removingChildrenIndexes[i]): rawViews.append(children[i]) view.mChildren = [x for x in children if x in rawViews]
class HierarchyInfo: rootView = RectView() biMapViewRect = {}
class ContourInfo: map_data = {} imageAfterClearProcessedViewArr = [] imageAfterClearProcessedView = np.array(imageAfterClearProcessedViewArr) rootView = RectView() rects = []
def processRectangle(self, imageAfterClearProcessedView, contour): return RectView()