예제 #1
0
 def Detect(Feature_Manager):
     global make_dir_root, timestr, DManager
     DManager = Feature_Manager
     make_dir_root = Feature_Manager._RootDirectory
     image = Feature_Manager._ImageCleaned.copy()
     
     LineSegmentsFeature.Initialize(image)
     
     cornerPts, cornerImageOutput, extractedLinesP, houghlineImageOutput = LineSegmentsFeature.EntityDetection()
     
     uniqueLines = LineSegmentsFeature.SegmentCreator(extractedLinesP)
     
     segLines = LineSegmentsFeature.SeggeratedCreator(uniqueLines)
     
     LineSegmentsFeature.cornerOnSegLine(cornerPts, segLines)
     
     detectedLineSegments = LineSegmentsFeature.DetectLineSegments(segLines)
     
     Segments = Cognition.MergeLineSegments(detectedLineSegments)
     
     LineImg = LineSegmentsFeature.DrawLines(Segments)
     
     LineSegmentsFeature.DisplayOutputs()
     
     
     return Segments, LineImg
예제 #2
0
    def Extract_Text(img, cnts):
        global DimensioanlLines
        i = 0
        ExtractedTextArea = []
        RotatedText = []
        for c in cnts:
            (x, y, w, h) = cv2.boundingRect(c)
            ar = w / float(h)
            crWidth = w / float(img.shape[1])
            area = cv2.contourArea(c)
            if h > 5 and w > 4 and ar > 0.4 and 0.5 > crWidth > 0.001 and area > 20:
                roi = img[y:y + h + 1, x:x + w + 1].copy()
                Rotate_roi = img[y:y + h + 5, x:x + w + 5].copy()
                cv2.imwrite(make_dir_roi + "/segmentedtext" + str(i) + ".png",
                            roi)
                BB = [x, y, w, h]
                new_img = TextsFeature.Paste(img, roi)
                OrientationAngle = ((Cognition.GetOrientation(
                    DimensioanlLines, BB)))
                output_img_path, detected_text = TextsFeature.RotateByAngle(
                    new_img, OrientationAngle, make_dir_rotate, i)
                p1 = Point2(x - 2, y - 2)
                p2 = Point2(x + w + 2, y + h + 2)
                DimText = DimensionalTexts()
                DimText.ExtractDimensionalText(DimText, detected_text, p1, p2,
                                               OrientationAngle)
                ExtractedTextArea.append(DimText)
                i += 1

        return ExtractedTextArea, RotatedText
예제 #3
0
 def CheckArrowOverlapBetweenTwo(a1, a2):
     if a1._ArrowCenter.x != a2._ArrowCenter.x or a1._ArrowCenter.y != a2._ArrowCenter.y:
         overlap = Cognition.CheckOverlapByLineSegment(
             a1._BoundingBoxP1, a1._BoundingBoxP2, a2._BoundingBoxP1,
             a2._BoundingBoxP2)
         if overlap == True:
             return True
     return False
예제 #4
0
    def CheckArrowOverlap(arrowHead):
        global ArrowHeadsList

        for a in ArrowHeadsList:
            if fabs(arrowHead._ArrowCenter.x - a._ArrowCenter.x) > 1 or fabs(
                    arrowHead._ArrowCenter.y != a._ArrowCenter.y) > 1:
                overlap = Cognition.CheckOverlapByLineSegment(
                    arrowHead._BoundingBoxP1, arrowHead._BoundingBoxP2,
                    a._BoundingBoxP1, a._BoundingBoxP2)
                if overlap == True:
                    return True
        return False
예제 #5
0
    def Detect(Feature_Manager):

        lines = Feature_Manager._DetectedLine
        dimension = Feature_Manager._DetectedDimension
        supportLinesegments = []
        entityLinesegments = []
        dist_Img = Cognition.DistanceTransform(Feature_Manager._ImageCleaned)
        minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dist_Img)
        for l in lines:
            for seg in l:
                PointsBetweenLine = SpecialLineSegments.PixelScanner(
                    seg.startPoint, seg.endPoint, dist_Img)
                sLinesPoints = []
                eLinePoints = []
                for i in PointsBetweenLine:
                    t = Cognition.CheckThicknessInVicinity(
                        i[0], i[1], dist_Img)
                    pt = Point2(i[0], i[1])
                    if t is None:
                        continue
                    if t <= 0.6 * maxVal:
                        sLinesPoints.append(pt)
                    else:
                        eLinePoints.append(pt)
                sortSuppPts = sLinesPoints
                sortEntPts = eLinePoints
                if (len(sortSuppPts) > 1):
                    startsupportPt = sortSuppPts[0]
                    for i in range(0, len(sortSuppPts) - 1):
                        p1 = sortSuppPts[i]
                        p2 = sortSuppPts[i + 1]
                        dist = p1.DistanceTo(p2)
                        if dist > 30:
                            Supportlinesegment = Line2(startsupportPt, p1)
                            supportLinesegments.append(Supportlinesegment)
                            startsupportPt = p2
                    Supportlinesegment = Line2(startsupportPt, p2)
                    supportLinesegments.append(Supportlinesegment)
                if (len(sortEntPts) > 1):
                    startentityPt = sortEntPts[0]
                    elines = []
                    for i in range(0, len(sortEntPts) - 1):
                        eline = []
                        p1 = sortEntPts[i]
                        p2 = sortEntPts[i + 1]
                        dist = p1.DistanceTo(p2)
                        if dist > 2:
                            entitylinesegment = Line2(startentityPt, p1)
                            eline.append(entitylinesegment)
                            entityLinesegments.append(eline)
                            startentityPt = p2
                    entitylinesegment = Line2(startentityPt, p2)
                    elines.append(entitylinesegment)
                    entityLinesegments.append(elines)
        for d in dimension:
            SupportL = []
            for a in d._DimensionalLines._ArrowHeads:
                BB = Cognition.SortCoordinates(
                    [a._BoundingBoxP1, a._BoundingBoxP2])
                bbMin = BB[0]
                bbMax = BB[1]
                direction = a._Direction
                if direction == "West":
                    shortListedLines = []
                    SupportLSW = []
                    for ls in supportLinesegments:
                        if bbMin.x + 3 > ls.startPoint.x and fabs(
                                ls.endPoint.x - ls.startPoint.x) < 3 and fabs(
                                    ls.endPoint.y - ls.startPoint.y) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            SupportLSW.append(ls)
                    SupportL.append(SupportLSW)
                elif direction == "East":
                    shortListedLines = []
                    SupportLSE = []
                    for ls in supportLinesegments:
                        if bbMax.x - 3 < ls.startPoint.x and fabs(
                                ls.endPoint.x - ls.startPoint.x) < 3 and fabs(
                                    ls.endPoint.y - ls.startPoint.y) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSE.append(ls)
                    SupportL.append(SupportLSE)
                elif direction == "North":
                    shortListedLines = []
                    SupportLSN = []
                    for ls in supportLinesegments:
                        if bbMin.y + 3 > ls.startPoint.y and fabs(
                                ls.endPoint.y - ls.startPoint.y) < 3 and fabs(
                                    ls.endPoint.x - ls.startPoint.x) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSN.append(ls)
                    SupportL.append(SupportLSN)
                elif direction == "South":
                    SupportLSS = []
                    shortListedLines = []
                    for ls in supportLinesegments:
                        if bbMax.y - 3 < ls.startPoint.y and fabs(
                                ls.endPoint.y - ls.startPoint.y) < 3 and fabs(
                                    ls.endPoint.x - ls.startPoint.x) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSS.append(ls)
                    SupportL.append(SupportLSS)
            d._SupportLines = SupportL
        Feature_Manager._DetectedLine = entityLinesegments
예제 #6
0
def main(argv1):

    img_path = argv1
    #img_path = "..\\TestData\\1.png"
    img_path = os.path.abspath(img_path)

    dir = os.path.dirname(img_path)
    os.chdir(dir)
    currentDir = os.getcwd()

    from Core.Features.Texts.TextsFeature import TextsFeature
    from Core.Features.Arrowheads.ArrowHeadsFeature import ArrowHeadsFeature
    from Core.Features.Circles.CirclesFeature import CirclesFeature
    from Core.Features.LineSegments.LineSegmentsFeature import LineSegmentsFeature
    from Core.Features.Cognition.DimensionalLinesFeature import DimensionalLinesFeature
    from Core.Features.Cognition.Cognition import Cognition
    from Core.Features.Cognition.SupportLinesFeature import SupportLinesFeature
    from Core.Features.FeatureManager import FeatureManager
    from Core.Utils.Eraser import Eraser
    from Core.Utils.I2CWriter import I2CWriter
    from Core.Utils.DXFWriter import DXFWriter

    timestr = time.strftime("%Y%m%d-%H%M%S")
    Start_time = time.strftime("%H hr - %M min - %S sec")
    print("Image2CAD Script Started at " + Start_time + "...")
    base = os.path.basename(img_path)
    folder_name = os.path.splitext(base)[0]
    print("Image Loaded: " + img_path + "...")
    print("Making Required Directory...")
    make_dir_Output = r"./Output"
    if not os.path.exists(make_dir_Output):
        os.mkdir(make_dir_Output)
    make_dir_folder = r"./Output/" + folder_name
    make_dir_root = r"./Output/" + folder_name + r"/" + timestr
    if not os.path.exists(make_dir_folder):
        os.mkdir(make_dir_folder)
    os.mkdir(make_dir_root)

    print("Initializing Feature Manager...")
    FM = FeatureManager()
    FM._ImagePath = img_path
    FM._RootDirectory = make_dir_root
    img = cv2.imread(FM._ImagePath)
    FM._ImageOriginal = img
    FM._ImageCleaned = img.copy()
    FM._ImageDetectedDimensionalText = img.copy()
    FM._ImageDetectedCircle = img.copy()
    Erased_Img = img.copy()

    AD_Time = time.strftime("%H hr - %M min - %S sec")
    print("Arrow Detection Started at " + AD_Time + "...")
    BB_Arrows, Arrow_Img = ArrowHeadsFeature.Detect(FM)
    FM._DetectedArrowHead = BB_Arrows
    FM._ImageDetectedArrow = Arrow_Img
    print("Arrow Detection Complete...")
    cv2.imshow("Detected Arrows", FM._ImageDetectedArrow)
    cv2.waitKey(0)

    for i in BB_Arrows:
        P1 = i._BoundingBoxP1
        P2 = i._BoundingBoxP2
        Erased_Img = Eraser.EraseBox(FM._ImageCleaned, P1, P2)
    FM._ImageCleaned = Erased_Img

    DL_Time = time.strftime("%H hr - %M min - %S sec")
    print("Dimensional Line Detection Started at " + DL_Time + "...")
    segments, DimensionalLine_Img = DimensionalLinesFeature.Detect(FM)
    FM._ImageDetectedDimensionalLine = DimensionalLine_Img
    FM._DetectedDimensionalLine = segments
    print("Dimensional Line Detection Complete...")
    cv2.imshow("Detected Dimensional Lines", FM._ImageDetectedDimensionalLine)
    cv2.waitKey(0)

    for j in segments:
        for i in j._Leaders:
            P1 = i.startPoint
            P2 = i.endPoint
            Erased_Img = Eraser.EraseLine(FM._ImageCleaned, P1, P2)
    FM._ImageCleaned = Erased_Img

    print("Correlating ArrowHead Direction...")
    Cognition.ArrowHeadDirection(FM)
    print("Correlating ArrowHead Direction Complete...")

    TE_Time = time.strftime("%H hr - %M min - %S sec")
    print("Text Area Extraction Started at " + TE_Time + "...")
    ExtractedTextArea, TextArea_Img = TextsFeature.Detect(FM)
    FM._ImageDetectedDimensionalText = TextArea_Img
    FM._DetectedDimensionalText = ExtractedTextArea
    print("Text Area Extraction Complete...")
    cv2.imshow("Detected Text Area", FM._ImageDetectedDimensionalText)
    cv2.waitKey(0)

    for i in ExtractedTextArea:
        P1 = i._TextBoxP1
        P2 = i._TextBoxP2
        Erased_Img = Eraser.EraseBox(FM._ImageCleaned, P1, P2)
    FM._ImageCleaned = Erased_Img

    DC_Time = time.strftime("%H hr - %M min - %S sec")
    print("Correlation of Dimensions Started at " + DC_Time + "...")
    Dimension_correlate = Cognition.ProximityCorrelation(FM)
    FM._DetectedDimension = Dimension_correlate
    print("Correlation of Dimensions complete...")

    LD_Time = time.strftime("%H hr - %M min - %S sec")
    print("Line Detection Started at " + LD_Time + "...")
    segments, DetectedLine_Img = LineSegmentsFeature.Detect(FM)
    FM._DetectedLine = segments
    FM._ImageDetectedLine = DetectedLine_Img
    print("Line Detection Complete...")
    cv2.imshow("Detected Lines", FM._ImageDetectedLine)
    cv2.waitKey(0)

    print("Correlation of Support Lines Started...")
    SupportLinesFeature.Detect(FM)
    print("Correlation of Support Lines Complete...")

    print("Correction of Broken Ends Stage 1 Started...")
    Cognition.CorrectEnds(FM)
    print("Correction of Broken Ends Stage 1 Complete...")

    print("Correction of Broken Ends Stage 2 Started...")
    Cognition.JoinLineSegmentsWithinProximityTolerance(FM)
    print("Correction of Broken Ends Stage 2 Complete...")

    for i in segments:
        for ls in i:
            P1 = ls.startPoint
            P2 = ls.endPoint
            Erased_Img = Eraser.EraseLine(FM._ImageCleaned, P1, P2)
    FM._ImageCleaned = Erased_Img

    print("Entity Correlation Started...")
    Cognition.EntityCorrelation(FM)
    print("Entity Correlation Complete...")

    CD_Time = time.strftime("%H hr - %M min - %S sec")
    print("Circle Detection Started at " + CD_Time + "...")
    detectedcircle, DetectedCircle_Img = CirclesFeature.Detect(FM)
    FM._ImageDetectedCircle = DetectedCircle_Img
    FM._DetectedCircle = detectedcircle
    print("Circle Detection Complete...")
    cv2.imshow("Detected circles", FM._ImageDetectedCircle)
    cv2.waitKey(0)

    for i in detectedcircle:
        center = i._centre
        radius = i._radius
        Erased_Img = Eraser.EraseCircle(FM._ImageCleaned, center, radius)
    FM._ImageCleaned = Erased_Img

    print("Exporting Extracted Data to I2C File...")
    I2CWriter.Write(FM)
    print("Exporting Complete...")

    print("Exporting Extracted Data to DXF File...")
    DXFWriter.Write(FM)
    print("Exporting Complete...")

    print("Image2CAD Script Execution Complete...")