class CompareInfo: rectFirst = Rect() rectSecond = Rect() delta=0.0 def __init__(self,rectFirst, rectSecond, delta): self.rectFirst = rectFirst self.rectSecond = rectSecond self.delta = delta
def getSmallestBoundRect(mRectBoundOfTexts, rect): smalledRect = Rect() # check if there is any rectangle contains with this rect for erRect in mRectBoundOfTexts: if (RectUtil.contains(erRect, rect)): if (smalledRect.area() == 0): smalledRect = erRect elif (RectUtil.contains(smalledRect, erRect)): smalledRect = erRect return smalledRect
def compressDataForFullUI(content): parsed_json = json.loads(content) strokes = [] strokeSize = [] xLines = [] yLines = [] if len(parsed_json) == 0: rect = Rect(0, 0, 0, 0) return (strokes, rect) for singleStroke in parsed_json: lines = singleStroke["lines"] strokeSize.append(singleStroke["size"]) stroke = [] xLine = [] yLine = [] for line in lines: startPoint = line['start'] endPoint = line['end'] xLine.append(int(startPoint['x'])) xLine.append(int(endPoint['x'])) yLine.append(int(startPoint['y'])) yLine.append(int(endPoint['y'])) stroke.append(xLine) stroke.append(yLine) strokes.append(stroke) xLines.extend(xLine) yLines.extend(yLine) xLines.sort() yLines.sort() x = xLines[0] y = yLines[0] width = xLines[-1] - xLines[0] height = yLines[-1] - yLines[0] rect = Rect(x, y, width, height) # strokes.append(strokeSize) return (strokes, rect) #if __name__ == "__main__": # with open('starButton_1' ) as f: # elmtList = f.read() # strokes = compressData(elmtList) # print(strokes) # img = strokeToSquareImage(strokes) # showStrokeImage(img) #A = [{"stroke":[{"color":"#000000","size":5,"lines":[{"start":{"x":47.625,"y":47.2734375},"end":{"x":53.625,"y":47.2734375}},{"start":{"x":53.625,"y":47.2734375},"end":{"x":56.625,"y":47.2734375}},{"start":{"x":56.625,"y":47.2734375},"end":{"x":57.625,"y":47.2734375}},{"start":{"x":57.625,"y":47.2734375},"end":{"x":65.625,"y":47.2734375}},{"start":{"x":65.625,"y":47.2734375},"end":{"x":73.625,"y":47.2734375}},{"start":{"x":73.625,"y":47.2734375},"end":{"x":76.625,"y":47.2734375}},{"start":{"x":76.625,"y":47.2734375},"end":{"x":85.625,"y":47.2734375}},{"start":{"x":85.625,"y":47.2734375},"end":{"x":91.625,"y":47.2734375}},{"start":{"x":91.625,"y":47.2734375},"end":{"x":100.625,"y":48.2734375}},{"start":{"x":100.625,"y":48.2734375},"end":{"x":105.625,"y":48.2734375}},{"start":{"x":105.625,"y":48.2734375},"end":{"x":125.625,"y":48.2734375}},{"start":{"x":125.625,"y":48.2734375},"end":{"x":127.625,"y":48.2734375}},{"start":{"x":127.625,"y":48.2734375},"end":{"x":131.625,"y":48.2734375}}]},{"color":"#000000","size":5,"lines":[{"start":{"x":40.625,"y":82.2734375},"end":{"x":40.625,"y":83.2734375}},{"start":{"x":40.625,"y":83.2734375},"end":{"x":45.625,"y":83.2734375}},{"start":{"x":45.625,"y":83.2734375},"end":{"x":53.625,"y":83.2734375}},{"start":{"x":53.625,"y":83.2734375},"end":{"x":62.625,"y":83.2734375}},{"start":{"x":62.625,"y":83.2734375},"end":{"x":73.625,"y":83.2734375}},{"start":{"x":73.625,"y":83.2734375},"end":{"x":77.625,"y":83.2734375}},{"start":{"x":77.625,"y":83.2734375},"end":{"x":87.625,"y":83.2734375}},{"start":{"x":87.625,"y":83.2734375},"end":{"x":97.625,"y":83.2734375}},{"start":{"x":97.625,"y":83.2734375},"end":{"x":100.625,"y":83.2734375}},{"start":{"x":100.625,"y":83.2734375},"end":{"x":111.625,"y":83.2734375}},{"start":{"x":111.625,"y":83.2734375},"end":{"x":120.625,"y":83.2734375}},{"start":{"x":120.625,"y":83.2734375},"end":{"x":135.625,"y":83.2734375}}]},{"color":"#000000","size":5,"lines":[{"start":{"x":27.625,"y":130.2734375},"end":{"x":36.625,"y":131.2734375}},{"start":{"x":36.625,"y":131.2734375},"end":{"x":57.625,"y":131.2734375}},{"start":{"x":57.625,"y":131.2734375},"end":{"x":77.625,"y":131.2734375}},{"start":{"x":77.625,"y":131.2734375},"end":{"x":107.625,"y":131.2734375}},{"start":{"x":107.625,"y":131.2734375},"end":{"x":137.625,"y":131.2734375}},{"start":{"x":137.625,"y":131.2734375},"end":{"x":170.625,"y":131.2734375}},{"start":{"x":170.625,"y":131.2734375},"end":{"x":207.625,"y":131.2734375}},{"start":{"x":207.625,"y":131.2734375},"end":{"x":245.625,"y":131.2734375}},{"start":{"x":245.625,"y":131.2734375},"end":{"x":277.625,"y":131.2734375}},{"start":{"x":277.625,"y":131.2734375},"end":{"x":310.625,"y":131.2734375}},{"start":{"x":310.625,"y":131.2734375},"end":{"x":332.625,"y":131.2734375}},{"start":{"x":332.625,"y":131.2734375},"end":{"x":340.625,"y":131.2734375}}]}]},{"stroke":[{"color":"#000000","size":5,"lines":[{"start":{"x":182.625,"y":411.2734375},"end":{"x":188.625,"y":411.2734375}},{"start":{"x":188.625,"y":411.2734375},"end":{"x":197.625,"y":411.2734375}},{"start":{"x":197.625,"y":411.2734375},"end":{"x":210.625,"y":411.2734375}},{"start":{"x":210.625,"y":411.2734375},"end":{"x":217.625,"y":411.2734375}},{"start":{"x":217.625,"y":411.2734375},"end":{"x":232.625,"y":411.2734375}},{"start":{"x":232.625,"y":411.2734375},"end":{"x":250.625,"y":410.2734375}},{"start":{"x":250.625,"y":410.2734375},"end":{"x":267.625,"y":410.2734375}},{"start":{"x":267.625,"y":410.2734375},"end":{"x":281.625,"y":408.2734375}},{"start":{"x":281.625,"y":408.2734375},"end":{"x":290.625,"y":408.2734375}},{"start":{"x":290.625,"y":408.2734375},"end":{"x":302.625,"y":407.2734375}},{"start":{"x":302.625,"y":407.2734375},"end":{"x":310.625,"y":407.2734375}},{"start":{"x":310.625,"y":407.2734375},"end":{"x":312.625,"y":407.2734375}}]},{"color":"#000000","size":5,"lines":[{"start":{"x":247.625,"y":361.2734375},"end":{"x":247.625,"y":366.2734375}},{"start":{"x":247.625,"y":366.2734375},"end":{"x":247.625,"y":377.2734375}},{"start":{"x":247.625,"y":377.2734375},"end":{"x":247.625,"y":385.2734375}},{"start":{"x":247.625,"y":385.2734375},"end":{"x":247.625,"y":396.2734375}},{"start":{"x":247.625,"y":396.2734375},"end":{"x":247.625,"y":408.2734375}},{"start":{"x":247.625,"y":408.2734375},"end":{"x":247.625,"y":418.2734375}},{"start":{"x":247.625,"y":418.2734375},"end":{"x":247.625,"y":431.2734375}},{"start":{"x":247.625,"y":431.2734375},"end":{"x":247.625,"y":440.2734375}},{"start":{"x":247.625,"y":440.2734375},"end":{"x":250.625,"y":447.2734375}},{"start":{"x":250.625,"y":447.2734375},"end":{"x":252.625,"y":458.2734375}},{"start":{"x":252.625,"y":458.2734375},"end":{"x":255.625,"y":461.2734375}},{"start":{"x":255.625,"y":461.2734375},"end":{"x":255.625,"y":463.2734375}},{"start":{"x":255.625,"y":463.2734375},"end":{"x":263.625,"y":470.2734375}},{"start":{"x":263.625,"y":470.2734375},"end":{"x":265.625,"y":476.2734375}},{"start":{"x":265.625,"y":476.2734375},"end":{"x":270.625,"y":477.2734375}}]},{"color":"#000000","size":5,"lines":[{"start":{"x":302.625,"y":372.2734375},"end":{"x":301.625,"y":371.2734375}}]},{"color":"#000000","size":5,"lines":[{"start":{"x":295.625,"y":365.2734375},"end":{"x":290.625,"y":360.2734375}},{"start":{"x":290.625,"y":360.2734375},"end":{"x":285.625,"y":356.2734375}},{"start":{"x":285.625,"y":356.2734375},"end":{"x":282.625,"y":353.2734375}},{"start":{"x":282.625,"y":353.2734375},"end":{"x":282.625,"y":351.2734375}},{"start":{"x":282.625,"y":351.2734375},"end":{"x":281.625,"y":351.2734375}},{"start":{"x":281.625,"y":351.2734375},"end":{"x":277.625,"y":351.2734375}},{"start":{"x":277.625,"y":351.2734375},"end":{"x":275.625,"y":351.2734375}},{"start":{"x":275.625,"y":351.2734375},"end":{"x":275.625,"y":350.2734375}},{"start":{"x":275.625,"y":350.2734375},"end":{"x":267.625,"y":350.2734375}},{"start":{"x":267.625,"y":350.2734375},"end":{"x":263.625,"y":347.2734375}},{"start":{"x":263.625,"y":347.2734375},"end":{"x":261.625,"y":347.2734375}},{"start":{"x":261.625,"y":347.2734375},"end":{"x":252.625,"y":347.2734375}},{"start":{"x":252.625,"y":347.2734375},"end":{"x":247.625,"y":347.2734375}},{"start":{"x":247.625,"y":347.2734375},"end":{"x":237.625,"y":347.2734375}},{"start":{"x":237.625,"y":347.2734375},"end":{"x":206.625,"y":353.2734375}},{"start":{"x":206.625,"y":353.2734375},"end":{"x":195.625,"y":358.2734375}},{"start":{"x":195.625,"y":358.2734375},"end":{"x":180.625,"y":367.2734375}},{"start":{"x":180.625,"y":367.2734375},"end":{"x":172.625,"y":371.2734375}},{"start":{"x":172.625,"y":371.2734375},"end":{"x":168.625,"y":372.2734375}},{"start":{"x":168.625,"y":372.2734375},"end":{"x":157.625,"y":380.2734375}},{"start":{"x":157.625,"y":380.2734375},"end":{"x":157.625,"y":381.2734375}},{"start":{"x":157.625,"y":381.2734375},"end":{"x":157.625,"y":388.2734375}},{"start":{"x":157.625,"y":388.2734375},"end":{"x":157.625,"y":401.2734375}},{"start":{"x":157.625,"y":401.2734375},"end":{"x":157.625,"y":407.2734375}},{"start":{"x":157.625,"y":407.2734375},"end":{"x":160.625,"y":410.2734375}},{"start":{"x":160.625,"y":410.2734375},"end":{"x":165.625,"y":413.2734375}},{"start":{"x":165.625,"y":413.2734375},"end":{"x":170.625,"y":427.2734375}},{"start":{"x":170.625,"y":427.2734375},"end":{"x":182.625,"y":452.2734375}},{"start":{"x":182.625,"y":452.2734375},"end":{"x":192.625,"y":457.2734375}},{"start":{"x":192.625,"y":457.2734375},"end":{"x":192.625,"y":460.2734375}},{"start":{"x":192.625,"y":460.2734375},"end":{"x":195.625,"y":460.2734375}},{"start":{"x":195.625,"y":460.2734375},"end":{"x":206.625,"y":466.2734375}},{"start":{"x":206.625,"y":466.2734375},"end":{"x":217.625,"y":475.2734375}},{"start":{"x":217.625,"y":475.2734375},"end":{"x":226.625,"y":478.2734375}},{"start":{"x":226.625,"y":478.2734375},"end":{"x":232.625,"y":480.2734375}},{"start":{"x":232.625,"y":480.2734375},"end":{"x":235.625,"y":480.2734375}},{"start":{"x":235.625,"y":480.2734375},"end":{"x":240.625,"y":481.2734375}},{"start":{"x":240.625,"y":481.2734375},"end":{"x":243.625,"y":481.2734375}},{"start":{"x":243.625,"y":481.2734375},"end":{"x":247.625,"y":481.2734375}},{"start":{"x":247.625,"y":481.2734375},"end":{"x":250.625,"y":481.2734375}},{"start":{"x":250.625,"y":481.2734375},"end":{"x":255.625,"y":481.2734375}},{"start":{"x":255.625,"y":481.2734375},"end":{"x":257.625,"y":480.2734375}},{"start":{"x":257.625,"y":480.2734375},"end":{"x":261.625,"y":480.2734375}},{"start":{"x":261.625,"y":480.2734375},"end":{"x":267.625,"y":476.2734375}},{"start":{"x":267.625,"y":476.2734375},"end":{"x":270.625,"y":475.2734375}},{"start":{"x":270.625,"y":475.2734375},"end":{"x":275.625,"y":472.2734375}},{"start":{"x":275.625,"y":472.2734375},"end":{"x":281.625,"y":468.2734375}},{"start":{"x":281.625,"y":468.2734375},"end":{"x":292.625,"y":455.2734375}},{"start":{"x":292.625,"y":455.2734375},"end":{"x":295.625,"y":450.2734375}},{"start":{"x":295.625,"y":450.2734375},"end":{"x":302.625,"y":446.2734375}},{"start":{"x":302.625,"y":446.2734375},"end":{"x":305.625,"y":441.2734375}},{"start":{"x":305.625,"y":441.2734375},"end":{"x":307.625,"y":435.2734375}},{"start":{"x":307.625,"y":435.2734375},"end":{"x":320.625,"y":412.2734375}},{"start":{"x":320.625,"y":412.2734375},"end":{"x":322.625,"y":407.2734375}},{"start":{"x":322.625,"y":407.2734375},"end":{"x":322.625,"y":405.2734375}},{"start":{"x":322.625,"y":405.2734375},"end":{"x":322.625,"y":402.2734375}},{"start":{"x":322.625,"y":402.2734375},"end":{"x":322.625,"y":396.2734375}},{"start":{"x":322.625,"y":396.2734375},"end":{"x":322.625,"y":385.2734375}},{"start":{"x":322.625,"y":385.2734375},"end":{"x":322.625,"y":380.2734375}},{"start":{"x":322.625,"y":380.2734375},"end":{"x":315.625,"y":371.2734375}},{"start":{"x":315.625,"y":371.2734375},"end":{"x":312.625,"y":367.2734375}},{"start":{"x":312.625,"y":367.2734375},"end":{"x":307.625,"y":365.2734375}},{"start":{"x":307.625,"y":365.2734375},"end":{"x":292.625,"y":362.2734375}},{"start":{"x":292.625,"y":362.2734375},"end":{"x":287.625,"y":362.2734375}},{"start":{"x":287.625,"y":362.2734375},"end":{"x":282.625,"y":361.2734375}},{"start":{"x":282.625,"y":361.2734375},"end":{"x":277.625,"y":360.2734375}},{"start":{"x":277.625,"y":360.2734375},"end":{"x":275.625,"y":360.2734375}},{"start":{"x":275.625,"y":360.2734375},"end":{"x":267.625,"y":357.2734375}},{"start":{"x":267.625,"y":357.2734375},"end":{"x":265.625,"y":357.2734375}}]}]}] ##json.loads(str(A)) #print(compressAllData(A))
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
def union(t, r): tx2 = t.width ty2 = t.height if ((tx2 | ty2) < 0): # This Rect has negative dimensions... # If r has non-negative dimensions then it is the answer. # If r is non-existant (has a negative dimension), then both # are non-existant and we can return any non-existant Rect # as an answer. Thus, returning r meets that criterion. # Either way, r is our answer. return Rect(r.x, r.y, r.width, r.height) rx2 = r.width ry2 = r.height if (rx2 < 0 or ry2 < 0): return Rect(t.x, t.y, t.width, t.height) tx1 = t.x ty1 = t.y tx2 += tx1 ty2 += ty1 rx1 = r.x ry1 = r.y rx2 += rx1 ry2 += ry1 if (tx1 > rx1): tx1 = rx1 if (ty1 > ry1): ty1 = ry1 if (tx2 < rx2): tx2 = rx2 if (ty2 < ry2): ty2 = ry2 tx2 -= tx1 ty2 -= ty1 # tx2,ty2 will never underflow since both original Rects # were already proven to be non-empty # they might overflow, though... if (tx2 > sys.maxsize): tx2 = sys.maxsize if (ty2 > sys.maxsize): ty2 = sys.maxsize return Rect(tx1, ty1, tx2, ty2)
class ElementData: def __init__(self, top, left, width, height): self.x = top self.y = left self.width = width self.height = height self.rect = Rect(top,left,width,height) # @Override def bound(self): return self.rect # @Override def toString(self): return "(" + self.x + ", " + self.y + ", " + self.width + ", " + self.height + ")" def __eq__(self, other): if other is None: return self.rect.area() == 0 elif type(other) != type(self): return False else: return (self.rect == other.rect) def __ne__(self, other): return not(self.__eq__(other))
def createHierachy(rects, width, height): rootObj = RectObj(Rect(0, 0, width, height)) sortedRectObjs = sorted(rects, key=lambda x: x.rectArea) elementLength = len(sortedRectObjs) if (elementLength == 1): rootObj.mChildren.append(sortedRectObjs[0]) return rootObj for i in range(elementLength - 1): item = sortedRectObjs[i] validElement = True isChild = False # print(item.rectArea) for j in range(i + 1, elementLength): parItem = sortedRectObjs[j] if parItem != item: item, validElement, isChild = RectUtil.fixHierarchy( parItem, item, width, height) if isChild: parItem.mChildren.append(item) break if not validElement: break if validElement and not isChild: rootObj.mChildren.append(item) rootObj.mChildren.append(sortedRectObjs[elementLength - 1]) searchForTextButton(rootObj) return rootObj
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 __init__(self, another=None): self.x = 0 self.y = 0 self.width = 0 self.height = 0 # self.bottom = 0 # self.right = 0 self.text = "" self.fontName = "" self.bold = False self.italic = False self.underlined = False self.monospace = False self.serif = False self.smallcaps = False self.fontSize = -1 self.fontId = 0 self.confidence = 0.0 self.words = [] self.blocks = [] self.rect = Rect(self.x, self.y, self.width, self.height) self.boundRectView = RectView.RectView(self.rect, None) if another != None: self.text = another.text self.confidence = another.confidence self.fontId = another.fontId self.fontName = another.fontName self.fontSize = another.fontSize self.y = another.y self.x = another.x self.width = another.width # self.bottom = another.bottom # self.right = another.right self.height = another.height self.italic = another.italic self.bold = another.bold self.underlined = another.underlined self.monospace = another.monospace self.serif = another.serif self.smallcaps = another.smallcaps self.words = [] self.blocks = [] self.rect = Rect(self.x, self.y, self.width, self.height) self.boundRectView = RectView.RectView(another.rect, None)
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
def jsonToRect(jsonRects): rectObjs = [] for item in jsonRects: # print(item) rectObj = RectObj( Rect(int(item['x']), int(item['y']), int(item['width']), int(item['height'])), int(item['iconID']), int(item['elementId'])) rectObjs.append(rectObj) return rectObjs
def jsonToRect(jsonRects,dipCalulator): rectObjs=[] for item in jsonRects: rectObj = RectObj(Rect(int(item['x']),int(item['y']),int(item['width']),int(item['height'])),int(item['iconID']),int(item['elementId'])) if rectObj.isRating(): rectObj.width = dipCalulator.dipToWidthPx(50) if rectObj.isSearchBar(): rectObj.width = dipCalulator.dipToWidthPx(50) rectObj.height = dipCalulator.dipToHeightPx(50) rectObjs.append(rectObj) return rectObjs
def getAllLeaveViewRecusively(self, additionalViewRecusive, rectView, x, y): if len(rectView.mChildren) == 0: b = rectView.bound() additionalViewRecusive.append( RectViewWrapper(rectView, Rect(b.x - x, b.y - y, b.width, b.height))) else: for child in rectView.mChildren: self.getAllLeaveViewRecusively(additionalViewRecusive, child, x, y)
def baseInit(self, iteratorLevel): width = 0 height = 0 channels = 1 if len(self.mRgbaImage.shape) == 2: height, width = self.mRgbaImage.shape else: height, width, channels = self.mRgbaImage.shape return self.baseInitIter(self.mRgbaImage, Rect(0, 0, width, height), channels, iteratorLevel)
def baseInitIter(self, imageMat, rect, channels, iteratorLevel): listdata = [] parentX = rect.x parentY = rect.y # subMat = imageMat[rect.y:rect.y+rect.height, rect.x:rect.width+rect.x] # # if(channels != 1): # subMat = imageMat[rect.y:rect.y+rect.height, rect.x:rect.width+rect.x, 0:channels] #tessAPI = PyTessBaseAPI() #Convert to PIL image imgPIL = Image.fromarray(imageMat) self.mHandle.SetImage(imgPIL) boxes = self.mHandle.GetComponentImages(iteratorLevel, True) for i, (im, box, _, _) in enumerate(boxes): wrapper = OCRTextWrapper.OCRTextWrapper() self.mHandle.SetRectangle(box['x'], box['y'], box['w'], box['h']) ocrResult = self.mHandle.GetUTF8Text() wrapper.text = ocrResult conf = self.mHandle.MeanTextConf() wrapper.confidence = conf self.mHandle.Recognize() iterator = self.mHandle.GetIterator() fontAttribute = iterator.WordFontAttributes() # fontAttribute = None wrapper.x = box['x'] + parentX wrapper.y = box['y'] + parentY wrapper.width = box['w'] wrapper.height = box['h'] wrapper.rect = Rect(wrapper.x, wrapper.y, wrapper.width, wrapper.height) # print(box) # if (fontAttribute != None): wrapper.fontName = fontAttribute['font_name'] wrapper.bold = fontAttribute['bold'] wrapper.italic = fontAttribute['italic'] wrapper.underlined = fontAttribute['underlined'] wrapper.monospace = fontAttribute['monospace'] wrapper.serif = fontAttribute['serif'] wrapper.smallcaps = fontAttribute['smallcaps'] wrapper.fontSize = fontAttribute['pointsize'] wrapper.fontId = fontAttribute['font_id'] listdata.append(wrapper) return listdata
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)
def convertToParentCorrdinate(rectView, childBound): rect = Rect() rect.x = childBound.x - rectView.x rect.y = childBound.y - rectView.y rect.height = childBound.height rect.width = childBound.width # Point[] ap = new Point[listPoints.size()]; # listPoints.toArray(ap); # contour = new MatOfPoint(ap); return rect
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)
def __init__(self, rect=Rect(), contour=None): self.rect = rect self.contour = contour self.mChildren = [] #mTextWithLocations = new ArrayList<OCRTextWrapper>(); self.mImageInfo = ImageInfo() self.mListInfo = ListInfo() self.mTextInfo = TextInfo() self.mType = RecViewUtil.VIEW_TYPE_DEFAULT self.x = rect.x self.y = rect.y self.width = rect.width self.height = rect.height self.mColor = None self.mAlpha = 0.0 self.mTextChildren = [] self.mTextWithLocations = [] self.mImageInfo = ImageInfo() self.mListItemInfo = ListItemInfo() self.textColor = 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)
def interection(t, r): tx1 = t.x ty1 = t.y rx1 = r.x ry1 = r.y tx2 = tx1 tx2 += t.width ty2 = ty1 ty2 += t.height rx2 = rx1 rx2 += r.width ry2 = ry1 ry2 += r.height if (tx1 < rx1): tx1 = rx1 if (ty1 < ry1): ty1 = ry1 if (tx2 > rx2): tx2 = rx2 if (ty2 > ry2): ty2 = ry2 tx2 -= tx1 ty2 -= ty1 # tx2,ty2 will never overflow (they will never be # larger than the smallest of the two source w,h) # they might underflow, though... if (tx2 < -sys.maxsize): tx2 = -sys.maxsize if (ty2 < -sys.maxsize): ty2 = -sys.maxsize return Rect(tx1, ty1, tx2, ty2)
def findBoundOfRects(iRects): rect = Rect() if (len(iRects) == 0): return rect top = sys.maxsize left = sys.maxsize right = -sys.maxsize bottom = -sys.maxsize for e in iRects: left = min(left, e.x) top = min(top, e.y) right = max(right, e.x + e.width) bottom = max(bottom, e.y + e.height) rect.x = left rect.y = top rect.width = right - left rect.height = bottom - top return rect
class ListItemMetadata: rect = Rect() baseViews = [] # Array of rectview additionalViews = [] # Array of rectview
def expand1Px(rect): return Rect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2)
def __init__(self, top, left, width, height): self.x = top self.y = left self.width = width self.height = height self.rect = Rect(top,left,width,height)
def getImageFromContour( original, contour) : rect = Rect(cv2.boundingRect(contour)) return getImageFromRect(original, rect);
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)
class OCRTextWrapper: def __init__(self, another=None): self.x = 0 self.y = 0 self.width = 0 self.height = 0 # self.bottom = 0 # self.right = 0 self.text = "" self.fontName = "" self.bold = False self.italic = False self.underlined = False self.monospace = False self.serif = False self.smallcaps = False self.fontSize = -1 self.fontId = 0 self.confidence = 0.0 self.words = [] self.blocks = [] self.rect = Rect(self.x, self.y, self.width, self.height) self.boundRectView = RectView.RectView(self.rect, None) if another != None: self.text = another.text self.confidence = another.confidence self.fontId = another.fontId self.fontName = another.fontName self.fontSize = another.fontSize self.y = another.y self.x = another.x self.width = another.width # self.bottom = another.bottom # self.right = another.right self.height = another.height self.italic = another.italic self.bold = another.bold self.underlined = another.underlined self.monospace = another.monospace self.serif = another.serif self.smallcaps = another.smallcaps self.words = [] self.blocks = [] self.rect = Rect(self.x, self.y, self.width, self.height) self.boundRectView = RectView.RectView(another.rect, None) def __hash__(self): return hash(self.rect) def __eq__(self, other): if other is None: return self.rect.area() == 0 elif type(other) != type(self): return False else: return (self.rect == other.rect and self.text == other.text) def __ne__(self, other): return not (self.__eq__(other)) def getTextAttributes(self, tesseractOCR, height): properties = [] wrapper = self #TODO properties.append( (Constants.ATTRIBUTE_TEXT_SIZE, str(tesseractOCR.getPreferenceFontSize(wrapper, height)) + Constants.UNIT_DIP)) # properties.append((Constants.ATTRIBUTE_TEXT_SIZE, str(self.fontSize)+ Constants.UNIT_DIP)) buffer = "normal" if self.bold: buffer += "|bold" if (self.italic): buffer += "|italic" properties.append((Constants.ATTRIBUTE_TEXT_STYLE, buffer)) buffer = "" if self.serif: buffer += "serif" elif self.monospace: buffer += "monospace" else: buffer += "normal" properties.append((Constants.ATTRIBUTE_TYPEFACE, buffer)) return properties def getWidth(self): return self.width def isSameTextInfoAndSameLine(self, other): if other == None: return False if self.fontSize != other.fontSize: return False if self.fontName == None: if other.fontName != None: return False elif not self.fontName == other.fontName: return False # same y location # overlap y location # once contain other # if (!(bottom == o.bottom && top == o.top || o.bottom <= bottom # && o.top <= top || bottom <= o.bottom && top <= o.top # || o.bottom <= bottom && top <= o.top || bottom <= o.bottom # && o.top <= top)) { # return false; # } if (not (self.height == other.height and self.top == other.top or other.bottom <= self.bottom and other.top <= self.top or self.bottom <= other.bottom and self.top <= other.top or other.bottom <= self.bottom and self.top <= other.top or self.bottom <= other.bottom and other.top <= self.top)): return False spaceBetweenWord = int(Constants.SPACE_BETWEEN_WORD_RATIO * self.fontSize) #not to far from each other if (not (self.right + spaceBetweenWord > other.left and self.right < other.left or other.right + spaceBetweenWord > self.left and other.right < self.left)): return False return True def toString(self): return self.text + "[" + self.rect + ", " + self.confidence + ", " + self.fontName + ", " + self.fontId + ", " + self.fontSize + ", " + self.serif + ", " + self.bold + ", " + len( self.text) + "]" def reCalculateBoundBaseOnWordList(self): if (len(self.words) == 0): return None firstWord = self.words[0] unionRect = firstWord.rect for i in range(1, len(self.words)): word = self.words[i] unionRect = RectUtil.union(unionRect, word.rect) self.x = unionRect.x self.y = unionRect.y self.width = unionRect.width self.height = unionRect.height return unionRect def bound(self): return self.rect def setBound(self, rect): self.left = rect.x self.top = rect.y self.width = rect.width self.height = rect.height
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]