Пример #1
0
def inoutlet_detect(input_path, output_path, num_of_vertices):
    epsilon = 0.01
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    im_fn_list = get_images(input_path)
    for im_fn in im_fn_list:
        print('===============')
        print(im_fn)
        im_raw = cv2.imread(im_fn)
        im_gray = cv2.cvtColor(im_raw, cv2.COLOR_BGR2GRAY)
        #kernel =  np.ones((4,4),np.uint8)
        #im_gray = cv2.dilate(im_gray, kernel,iterations = 1)
        #cv2.imwrite(os.path.join(output_path, "dialate_"+os.path.basename(im_fn)),im_gray)

        im_gray = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY)[1]
        im_gray = cv2.subtract(255, im_gray)
        _, contours, _ = cv2.findContours(im_gray, cv2.RETR_TREE,
                                          cv2.CHAIN_APPROX_SIMPLE)
        area = []
        for cnt in contours:

            approx = cv2.approxPolyDP(cnt, epsilon * cv2.arcLength(cnt, True),
                                      True)

            if len(approx) == 7:
                print(cv2.contourArea(cnt))
                cv2.drawContours(im_raw, [approx],
                                 0, (0, 255, 0),
                                 thickness=1,
                                 lineType=8)
        cv2.imwrite(os.path.join(output_path, os.path.basename(im_fn)), im_raw)
Пример #2
0
def square_detect(input_path, output_path, ifmask, maskloc_output_path):
    """
    Identify the square which is typically intrument alarm.
    """
    epsilon = 0.001
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)

        img_raw = cv2.imread(img_fn)

        img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
        img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)[1]

        _, contours, _ = cv2.findContours(img_binary, cv2.RETR_TREE,
                                          cv2.CHAIN_APPROX_SIMPLE)

        for cnt in contours:
            approx = cv2.approxPolyDP(cnt, epsilon * cv2.arcLength(cnt, True),
                                      True)
            #for item in approx:
            #  cv2.drawMarker(img_binary, (item[0][0], item[0][1]),(0,255,0), markerType=cv2.MARKER_STAR,markerSize=4, thickness=1, line_type=cv2.LINE_AA)

            if (len(approx) == 4) and cv2.contourArea(cnt) > 400:

                cv2.drawContours(img_raw, [approx],
                                 0, (0, 255, 0),
                                 thickness=3,
                                 lineType=8)

            # dis = []
            #    A1 = approx[3][0][1] - approx[1][0][1]
            #   B1 = -(approx[3][0][0] - approx[1][0][0])
            #    dis.append(np.sqrt(A1**2+B1**2))
            #    A2 = approx[2][0][1] - approx[0][0][1]
            #   B2 = -(approx[2][0][0] - approx[0][0][0])
            #    # Calculate the distance between two vertex
            #    dis.append(np.sqrt(A2**2+B2**2))
            #    # Calculate the intersection angle between the line across tip and centroid and the line across two symmetric points
            #    theta = abs(np.arccos((A1*A2+B1*B2)/np.sqrt((A1**2+B1**2)*(A2**2+B2**2))))
            #   # Check if the line across vertex perpendicular with each other.
            #   # Check if the distance between two vertex points are the same
            #   if theta > 85/180*np.pi and theta < 95/180*np.pi and np.abs(dis[0] - dis[1]) < 5:
            #        for item in approx:
            #           cv2.drawMarker(img_raw, (item[0][0], item[0][1]),(0,255,0), markerType=cv2.MARKER_STAR,markerSize=4, thickness=1, line_type=cv2.LINE_AA)

        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),
                    img_raw)
Пример #3
0
def main(argv=None):
    ##remove_border(FLAGS.test_data_path,FLAGS.removeborder_output_path)
    ##skeleton(FLAGS.morpho_input_path,FLAGS.morpho_output_path)
    #inoutlet_detect(FLAGS.inoutletdetect_input_path, FLAGS.inoutletdetect_output_path, True, FLAGS.maskloc_output_path)
    #circle_detect(FLAGS.circledetect_input_path, FLAGS.circledetect_output_path, False, FLAGS.maskloc_output_path)
    ##square_detect(FLAGS.squaredetect_input_path, FLAGS.squaredetect_output_path, False,  FLAGS.maskloc_output_path)
    #ctpn_pred(FLAGS.ctpn_input_path, FLAGS.ctpn_output_path,FLAGS.textloc_output_path, FLAGS.checkpoint_path, FLAGS.gpu)
    ##text_read(FLAGS.tessact_input_path,FLAGS.tessact_output_path)
    #compmatch(FLAGS.compmatch_input_path,FLAGS.compmatch_target_path,FLAGS.compmatch_output_path, FLAGS.comploc_output_path)
    #maskall(FLAGS.maskimage_input_path,FLAGS.shapeloc_input_path,FLAGS.txtloc_input_path, FLAGS.comploc_input_path,FLAGS.mask_output_path)

    #houghline_detect(FLAGS.pipedetect_input_path, FLAGS.pipedetect_output_path,  FLAGS.lineloc_output_path)
    img_fn_list = get_images(FLAGS.test_data_path)
    create_graph(img_fn_list, FLAGS.shapeloc_input_path,
                 FLAGS.comploc_output_path, FLAGS.textloc_output_path,
                 FLAGS.lineloc_output_path)
Пример #4
0
def maskall(image_input_path,shapeloc_input_path,txtloc_input_path, comploc_input_path, output_path):
    """
    mask all shapes and text boxes
    """
    print("========== mask shapes and text ==============")
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(image_input_path)
    wl = 4
    for img_fn in img_fn_list:
        print(img_fn)
        img_raw = cv2.imread(img_fn)
        img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
        img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)[1]
        h, w = img_binary.shape[:2]
        img_blank =  np.ones(shape=[h, w], dtype=np.uint8)*255

        with open(os.path.join (shapeloc_input_path, os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt", 'r') as file1:
            for line in file1:
                cnt = [i for i in line.split()]
                loc = [int(i) for i in cnt[1:]]
                cv2.drawContours(img_blank, [np.array(loc).reshape((-1,1,2))], 0, (0), thickness = -1, lineType=8)

        with open(os.path.join (comploc_input_path, os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt", 'r') as file2:
            for line in file2:
                cnt = [i for i in line.split()]
                loc = [int(i) for i in cnt[1:]]
                cv2.drawContours(img_blank, [np.array(loc).reshape((-1,1,2))], 0, (0), thickness = -1, lineType=8)

        with open(os.path.join (txtloc_input_path, os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt", 'r') as file3:
            for line in file3:
                if bool(line and line.strip()):
                    cnt = [i for i in line.split()]     
                    loc = [int(i) for i in cnt[1:]]
                    # crop image with rectangle box and save
                    x0,y0,w0,h0 = cv2.boundingRect(np.array(loc[:8]).astype(np.int32).reshape((-1, 2)))
                    img_crop = img_blank[y0:y0+h0,x0:x0+w0].copy()
                    hc, wc = img_crop.shape[:2]
                    countzero = hc*wc - cv2.countNonZero(img_crop)
                    if countzero *1.0 / (hc*wc) <= 0.25:
                        # if new area is less than 25% of overlap with other proposed masked area
                        cv2.drawContours(img_blank, [np.array(loc[:8]).astype(np.int32).reshape((-1, 1, 2))], 0, (0), thickness=-1, lineType=8)
 
                        #img_mask = cv2.bitwise_and(img_blank, img_binary, mask = None)
        img_mask = cv2.bitwise_or(cv2.bitwise_not(img_blank), img_binary, mask = None)
        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)), img_mask)
Пример #5
0
def houghline_detect(input_path,output_path, lineloc_output_path):
    """
    Houghline detection
    """
    print("============ detect line ================")
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    for img_fn in img_fn_list:
        print(img_fn)
        txtfileloc = os.path.join (lineloc_output_path, os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt"
        if os.path.isfile(txtfileloc):
            f =  open(txtfileloc, 'r+')
            textlines = txtremove(f,['LN','CF','CT'])
            f.seek(0)
            f.write(textlines)
            f.truncate()
            f.close() 
        #edge detection of the line
        img_gray = cv2.imread(img_fn,cv2.IMREAD_GRAYSCALE)
        img_draw = img_gray.copy()
        img_draw2 = img_gray.copy()

        #img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)[1]
        edges = cv2.Canny(img_gray,50,100)

        minLineLength = 4
        maxLineGap = 0
        lines = probabilistic_hough_line(edges, threshold=10, line_length=minLineLength,line_gap=maxLineGap)
        #lines = cv2.HoughLinesP(img_gray,1,1/180,40,minLineLength,maxLineGap)
        # draw direct output from houghlines

        
        # draw lines and vertice points before merge
        for iln in range(len(lines)):
            lines[iln] = [[lines[iln][0][0],lines[iln][0][1],lines[iln][1][0],lines[iln][1][1]]]
            #for x1, y1, x2, y2 in lines[iln]:
                #cv2.drawMarker(img_draw2, (x1, y1),(0,255,0), markerType=cv2.MARKER_STAR, 
                #                markerSize=6, thickness=1, line_type=cv2.LINE_AA)
                #cv2.drawMarker(img_draw2, (x2, y2),(255,0,0), markerType=cv2.MARKER_STAR, 
                #                markerSize=6, thickness=1, line_type=cv2.LINE_AA)
                #cv2.line(img_draw2,(x1,y1),(x2,y2),(0,0,100),3)

        # Merge the hough lines
        hb = HoughBundler()
        lines = hb.process_lines(lines,img_gray)
        lines = line_connect(lines)
        # draw line ID before merge
        for iln in range(len(lines)):
            lines[iln] = [lines[iln][0][0],lines[iln][0][1],lines[iln][1][0],lines[iln][1][1]]
            x1, y1, x2, y2 = lines[iln]
            cv2.putText(img_draw2, str(iln), (int((x1+x2)/2),int((y1+y2)/2)), cv2.FONT_HERSHEY_SIMPLEX ,0.25, (0,255,0), 1, cv2.LINE_AA) 
        cv2.imwrite(os.path.join(output_path, "hough_"+os.path.basename(img_fn)),img_draw2)

        pt_intersect = line_intersect(lines, img_gray)

        for ipt in pt_intersect:
            cv2.drawMarker(img_draw, (ipt[0], ipt[1]),(0,255,0), markerType=cv2.MARKER_STAR, 
                                markerSize=4, thickness=2, line_type=cv2.LINE_AA)
        # draw line ID after merge
        for iln in range(len(lines)):
            p1 = lines[iln][:2]
            p2 = lines[iln][2:]
            cv2.drawMarker(img_draw, (p1[0], p1[1]),(0,255,0), markerType=cv2.MARKER_STAR, 
                                markerSize=5, thickness=1, line_type=cv2.LINE_AA)
            cv2.drawMarker(img_draw, (p2[0], p2[1]),(255,0,0), markerType=cv2.MARKER_STAR, 
                                markerSize=5, thickness=1, line_type=cv2.LINE_AA)
            cv2.line(img_draw,(p1[0],p1[1]),(p2[0],p2[1]),(0,0,255),1)
            with open(txtfileloc, "a") as f1:
                txtline = "LN"
                txtline += "\t" + str(p1[0]) + "\t" + str(p1[1]) + "\t" + str(p2[0]) + "\t" + str(p2[1])
                txtline += "\n"
                f1.writelines(txtline)
                f1.close()  
        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),img_draw)
        # save the line intersection result
        with open(txtfileloc, "a") as f1:
            txtline=""
            for ipt in range (len(pt_intersect)):
                txtline =  str(pt_intersect[ipt][4]) 
                txtline += "\t" + str(pt_intersect[ipt][0])
                txtline += "\t" + str(pt_intersect[ipt][1]) 
                txtline += "\t" + str(pt_intersect[ipt][2]) 
                txtline += "\t" + str(pt_intersect[ipt][3]) 
                txtline += "\n"
                f1.writelines(txtline)
        f1.close()  
Пример #6
0
def houghline(input_path, output_path):
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    im_fn_list = get_images(input_path)
    for im_fn in im_fn_list:
        print(im_fn)
        #edge detection of the line
        im_raw = cv2.imread(im_fn)
        im_draw = im_raw.copy()
        im_gray = cv2.cvtColor(im_raw, cv2.COLOR_BGR2GRAY)

        im_gray = cv2.subtract(255, im_gray)
        im_gray = skeleton(im_gray)
        #im_gray = thinning(im_gray)
        cv2.imwrite(
            os.path.join(output_path, "skeleton_" + os.path.basename(im_fn)),
            im_gray)

        #im_gray = cv2.erode(im_gray, kernel,iterations = 1)
        #kernel1 = np.array([ [0, 0, 0],
        #            [0, 1, 0],
        #            [0, 0, 0]],np.uint8)

        # im_gray = cv2.morphologyEx(im_gray, cv2.MORPH_HITMISS, kernel1)
        #
        #kernel2 =  np.ones((2,2),np.uint8)
        # im_gray = cv2.erode(im_gray, kernel2,iterations = 1)
        #im_gray = cv2.morphologyEx(im_gray, cv2.MORPH_OPEN, kernel2)

        #im_gray = cv2.dilate(im_gray, element,iterations = 1)
        #cv2.imwrite(os.path.join(output_path, "diat_"+os.path.basename(im_fn)),im_gray)

        #im_gray = cv2.subtract(255, im_gray)

        #im_gray = cv2.erode(im_gray, kernel2,iterations = 1)
        #cv2.imwrite(os.path.join(output_path, "erode_"+os.path.basename(im_fn)),im_gray)
        #im_gray = cv2.morphologyEx(im_gray, cv2.MORPH_OPEN, kernel1)
        #im_gray = cv2.dilate(im_gray, kernel1,iterations = 8)
        #cv2.imwrite(os.path.join(output_path, "diat_"+os.path.basename(im_fn)),im_gray)

        minLineLength = 5
        maxLineGap = 20
        """
        lines = probabilistic_hough_line(im_gray, threshold=100, line_length=minLineLength ,
                                 line_gap=maxLineGap)
        for line in lines:
            p0, p1 = line
            x1 = p0[0]
            y1 = p0[1]
            x2 = p1[0]
            y2 = p1[1]
            cv2.line(im_draw,(x1,y1),(x2,y2),(100),5)

        """
        lines = cv2.HoughLinesP(im_gray, 1, 0.1 / 180, 100, minLineLength,
                                maxLineGap)
        for iln in range(len(lines)):
            for x1, y1, x2, y2 in lines[iln]:
                cv2.line(im_draw, (x1, y1), (x2, y2), (100), 5)

        cv2.imwrite(os.path.join(output_path, os.path.basename(im_fn)),
                    im_draw)
Пример #7
0
def ctpn_pred(input_path, output_path,textloc_output_path, checkpoint_path,gpu):
    print("========== detect text using ctpn ==============")
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    os.environ['CUDA_VISIBLE_DEVICES'] = gpu
    tf.reset_default_graph()
    with tf.get_default_graph().as_default():
        
        input_image = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_image')
        input_im_info = tf.placeholder(tf.float32, shape=[None, 3], name='input_im_info')

        global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)

        bbox_pred, cls_pred, cls_prob = model.model(input_image)

        variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step)
        saver = tf.train.Saver(variable_averages.variables_to_restore())

        with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
            ckpt_state = tf.train.get_checkpoint_state(checkpoint_path)
            model_path = os.path.join(checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path))
            saver.restore(sess, model_path)

            im_fn_list = get_images(input_path)
            for im_fn in im_fn_list:
                print('===============')
                print(im_fn)

                #remove existing detected component from text
                txtfileloc = os.path.join (textloc_output_path, os.path.splitext(os.path.basename(im_fn))[0]) + "_loc.txt"
                if os.path.isfile(txtfileloc):
                    f =  open(txtfileloc, 'r+')
                    textlines = txtremove(f,['ORIG','ROT'])
                    f.seek(0)
                    f.write(textlines)
                    f.truncate()
                    f.close()   

                start = time.time()
                try:
                    img_raw = cv2.imread(im_fn)
                except:
                    print("Error reading image {}!".format(im_fn))
                    continue
                # image used to draw bounding box
                H, W, _ = img_raw.shape
                img_blank =  np.ones(shape=[H, W], dtype=np.uint8)*255
                img_draw = img_raw.copy()

                img, (rh, rw) = resize_image(img_raw)
                h, w, c = img.shape
                res = []
                for ifrot in ['ORIG','ROT']:
                    im = img.copy()

                    if ifrot == 'ROT':
                        im = cv2.transpose(im)
                        im = cv2.flip(im,1)
                        bbox_color = (255,0,0)
                        im_info = np.array([w, h, c]).reshape([1, 3])
                    else: 
                        bbox_color = (0,255,0)
                        im_info = np.array([h, w, c]).reshape([1, 3])
                    bbox_pred_val, cls_prob_val = sess.run([bbox_pred, cls_prob],
                                                           feed_dict={input_image: [im],
                                                                      input_im_info: im_info})
    
                    textsegs, _ = proposal_layer(cls_prob_val, bbox_pred_val, im_info)
                    scores = textsegs[:, 0]
                    textsegs = textsegs[:, 1:5]
                    
                    textdetector = TextDetector(DETECT_MODE='H')
                    boxes = textdetector.detect(textsegs, scores[:, np.newaxis], im.shape[:2])
                    boxes = np.array(boxes, dtype=np.int)
                    print("Find number of text:",len(boxes))
                    cost_time = (time.time() - start)
                    print("cost time: {:.2f}s".format(cost_time))
                    fx=1.0 / rw
                    fy=1.0 / rh

                    for i, box in enumerate(boxes):
                        if ifrot == 'ROT':
                            box = np.array([box[3],h-box[2],box[5],h-box[4],box[7],h-box[6],box[1],h-box[0],box[8]])
                            
                        #resize the images
                        box[:8:2] = (box[:8:2]*fx).astype(np.int32)
                        box[1::2] = (box[1::2]*fy).astype(np.int32)
                        loc = [int(i) for i in box[0:-1]]
                       
                        # crop image with rectangle box and save
                        x0,y0,w0,h0 = cv2.boundingRect(np.array(loc[:8]).astype(np.int32).reshape((-1, 2)))
                        img_crop = img_blank[y0:y0+h0,x0:x0+w0].copy()
                        hc, wc = img_crop.shape[:2]
                        countzero = hc*wc - cv2.countNonZero(img_crop)

                        if countzero *1.0 / (hc*wc) <= 0.2:
                            # if there is minimum overlap with previous bounding box
                            
                            cv2.drawContours(img_blank, [np.array(loc).reshape((-1,1,2))], 0, (0), thickness = -1, lineType=8)
                            cv2.polylines(img_draw, [box[:8].astype(np.int32).reshape((-1, 1, 2))], True, color=bbox_color, thickness=2)
                            # crop image with rectangle box and save
                            x0,y0,w0,h0 = cv2.boundingRect(box[:8].astype(np.int32).reshape((-1, 2)))
                            img_crop = img_raw[y0:y0+h0,x0:x0+w0].copy()
                            txtrecog = txt_recog(img_crop)
                            res.append([ifrot]+loc+[txtrecog]) 
                            cv2.imwrite(os.path.join(output_path, os.path.splitext(os.path.basename(im_fn))[0])+"_"+ifrot+"_"+str(format(i, "04"))+".jpg", img_crop) 
                            cv2.putText(img_draw, str(i), (box[0],box[1]), cv2.FONT_HERSHEY_SIMPLEX ,1.0, bbox_color, 2, cv2.LINE_AA) 
                cv2.imwrite(os.path.join(output_path, os.path.splitext(os.path.basename(im_fn))[0])+"_"+ifrot+".jpg", img_draw) 
                
                with open(txtfileloc, "a") as f:
                    for i, ir in enumerate(res):
                        line = "\t".join(str(ir[k]) for k in range(10))
                        line += "\n"
                        print(line)
                        f.writelines(line)
                    f.close()
Пример #8
0
def inoutlet_detect(input_path, output_path, ifmask, maskloc_output_path):
    """
    Identify the inlet and outlet arrow.
    ifmast ==  True, the area will be masked in output drawing
    """
    print("========== detect inlet and outlet ==============")
    epsilon = 0.02
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)

        #remove existing detected component from text
        txtfileloc = os.path.join(
            maskloc_output_path,
            os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt"
        if os.path.isfile(txtfileloc):
            f = open(txtfileloc, 'r+')
            textlines = txtremove(f, ['IO'])
            f.seek(0)
            f.write(textlines)
            f.truncate()
            f.close()

        img_raw = cv2.imread(img_fn)
        img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
        img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)[1]
        img_binary = cv2.subtract(255, img_binary)

        h, w = img_binary.shape[:2]
        img_blank = np.ones(shape=[h, w], dtype=np.uint8) * 255

        _, contours, _ = cv2.findContours(img_binary, cv2.RETR_TREE,
                                          cv2.CHAIN_APPROX_SIMPLE)
        num_of_IO = 0
        for cnt in contours:
            approx = cv2.approxPolyDP(cnt, epsilon * cv2.arcLength(cnt, True),
                                      True)

            # Identify all contours with only 5 or 7 vertices and contour area is greater than 500
            if (len(approx) == 7
                    or len(approx) == 5) and cv2.contourArea(cnt) > 500:
                #cv2.drawContours(img_raw, [approx], 0, (0,255,0), thickness = 1, lineType=8)
                l = len(approx)
                # Iterate all vertices as a arrow tip point
                for it in range(len(approx)):
                    tip = approx[it][0]
                    othervert = np.delete(approx, it, 0)
                    sumpts = np.zeros(2)
                    for ivert in othervert:
                        sumpts = sumpts + ivert
                    # Calculate the centroid for the rest of symmetric point
                    centpts = sumpts[0] / (l - 1)
                    A1 = tip[1] - centpts[1]
                    B1 = -(tip[0] - centpts[0])
                    dis = []

                    for ipt in range(1, int((l + 1) / 2)):
                        il1 = it + ipt
                        if il1 > l - 1:
                            il1 = il1 - l
                        il2 = it - ipt
                        A2 = approx[il2][0][1] - approx[il1][0][1]
                        B2 = -(approx[il2][0][0] - approx[il1][0][0])
                        # Calculate the distance between each of symmetric points
                        dis.append(np.sqrt(A2**2 + B2**2))
                        # Calculate the intersection angle between the line across tip and centroid and the line across two symmetric points
                        theta = abs(
                            np.arccos((A1 * A2 + B1 * B2) / np.sqrt(
                                (A1**2 + B1**2) * (A2**2 + B2**2))))
                        # Check if the line across tip and centroid is perpendicular with the line across two symmetric points with +/- 5 degree accuracy.
                        # Check if the distance between two symmetric points keep constant or decreasing with +5 pixel accuracy
                        if theta < 80 / 180 * np.pi or theta > 100 / 180 * np.pi or (
                                ipt > 1 and dis[ipt - 1] - dis[ipt - 2] > 5):
                            break
                        if ipt == int((l + 1) / 2) - 1:
                            for item in othervert:
                                cv2.drawMarker(img_raw,
                                               (item[0][0], item[0][1]),
                                               (0, 255, 0),
                                               markerType=cv2.MARKER_STAR,
                                               markerSize=4,
                                               thickness=1,
                                               line_type=cv2.LINE_AA)
                            cv2.drawMarker(img_raw, (tip[0], tip[1]),
                                           (0, 0, 255),
                                           markerType=cv2.MARKER_STAR,
                                           markerSize=8,
                                           thickness=1,
                                           line_type=cv2.LINE_AA)
                            cv2.drawMarker(img_raw,
                                           (int(centpts[0]), int(centpts[1])),
                                           (255, 0, 0),
                                           markerType=cv2.MARKER_STAR,
                                           markerSize=8,
                                           thickness=1,
                                           line_type=cv2.LINE_AA)
                            # make arrow tip point the first point
                            arrow_cnt = []
                            arrow_cnt.append(approx[it])

                            for ipt in range(1, l):
                                iptt = it + ipt
                                if it + ipt >= l:
                                    iptt = iptt - l
                                arrow_cnt.append(approx[iptt])
                            arrow_cnt = np.array(arrow_cnt)
                            arrow_cnt_scaled = scale_contour(arrow_cnt, 1.05)
                            (x, y, w, h) = cv2.boundingRect(arrow_cnt_scaled)
                            cv2.rectangle(img_blank, (x, y), (x + w, y + h),
                                          (0), -1)
                            #cv2.drawContours(img_blank, [arrow_cnt_scaled], 0, (0), thickness = -1, lineType=8)
                            num_of_IO += 1
                            with open(txtfileloc, "a") as f:
                                txtline = "IO"
                                txtline += "\t" + str(x + w) + "\t" + str(y +
                                                                          h)
                                txtline += "\t" + str(x) + "\t" + str(y + h)
                                txtline += "\t" + str(x) + "\t" + str(y)
                                txtline += "\t" + str(x + w) + "\t" + str(y)
                                txtline += "\n"
                                f.writelines(txtline)
                                f.close()
        print("Find " + str(num_of_IO) + " inlet/outlet")
        if ifmask:
            img_woIO = cv2.bitwise_or(cv2.bitwise_not(img_blank),
                                      cv2.bitwise_not(img_binary),
                                      mask=None)
            cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),
                        img_woIO)
        else:
            cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),
                        img_raw)
Пример #9
0
def circle_detect(input_path, output_path, ifmask, maskloc_output_path):
    """
    Identify the circles using houghcircle approach
    ifmast ==  True, the area will be masked in output drawing
    """
    print("========== detect circle ==============")
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)
        #remove existing detected component from text
        txtfileloc = os.path.join(
            maskloc_output_path,
            os.path.splitext(os.path.basename(img_fn))[0]) + "_loc.txt"
        if os.path.isfile(txtfileloc):
            f = open(txtfileloc, 'r+')
            textlines = txtremove(f, ['CIR'])
            f.seek(0)
            f.write(textlines)
            f.truncate()
            f.close()

        img_raw = cv2.imread(img_fn)
        img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
        num_of_cir = 0
        # apply automatic Canny edge detection using the computed median
        circles = cv2.HoughCircles(img_gray,
                                   cv2.HOUGH_GRADIENT,
                                   1.8,
                                   100,
                                   param1=200,
                                   param2=80,
                                   minRadius=9,
                                   maxRadius=60)

        h, w = img_gray.shape[:2]
        img_blank = np.ones(shape=[h, w], dtype=np.uint8) * 255
        img_blank2 = img_blank.copy()
        # pixel width of each circle
        wl = 3

        if circles is not None:
            circles = np.uint16(np.around(circles))
            for cir in circles[0, :]:
                cv2.circle(img_blank, (cir[0], cir[1]), int(cir[2] + wl), 0,
                           -1)  # -1 to draw filled circles
                cv2.circle(img_blank, (cir[0], cir[1]), int(cir[2] - wl), 255,
                           -1)  # -1 to draw filled circles

            bit_or = cv2.bitwise_or(img_blank, img_gray, mask=None)
            #cv2.imwrite(os.path.join(output_path, "cir"+"-"+os.path.basename(img_fn)), bit_or)
            bit_or = cv2.threshold(bit_or, 128, 255, cv2.THRESH_BINARY)[1]
            bit_or = cv2.subtract(255, bit_or)

            for cir in circles[0, :]:
                circheck = []
                for iy in range(cir[1] - cir[2] - 1, cir[1] + cir[2] + 1):
                    countblkpixel1 = np.sum(
                        bit_or[iy, cir[0] - cir[2] - wl:cir[0]] != 0)
                    countblkpixel2 = np.sum(
                        bit_or[iy, cir[0]:cir[0] + cir[2] + wl] != 0)
                    if countblkpixel1 > 1:
                        circheck.append(1)
                    else:
                        circheck.append(0)
                    if countblkpixel2 > 1:
                        circheck.append(1)
                    else:
                        circheck.append(0)
                # check if each ring contain a circle
                if np.sum(circheck) * 1.0 / len(circheck) > 0.9:
                    # draw the outer circle
                    cv2.circle(img_raw, (cir[0], cir[1]), cir[2], (0, 255, 0),
                               2)
                    # draw the center of a cirlce
                    cv2.circle(img_raw, (cir[0], cir[1]), 2, (0, 0, 255), 3)
                    cv2.circle(img_blank2, (cir[0], cir[1]), int(cir[2] + wl),
                               0, -1)  # -1 to draw filled circles
                    # draw bounding box to cover the circle
                    cir[2] += wl
                    bbox_scaled = [
                        cir[0] + cir[2], cir[1] + cir[2], cir[0] - cir[2],
                        cir[1] + cir[2], cir[0] - cir[2], cir[1] - cir[2],
                        cir[0] + cir[2], cir[1] - cir[2]
                    ]
                    num_of_cir += 1
                    with open(txtfileloc, "a") as f:
                        txtline = "CIR"
                        for ip in bbox_scaled:
                            txtline += "\t" + str(ip)
                        txtline += "\n"
                        f.writelines(txtline)
                        f.close()
        print("Find " + str(num_of_cir) + " circles")
        if ifmask:
            img_woCIR = cv2.bitwise_or(cv2.bitwise_not(img_blank2),
                                       img_gray,
                                       mask=None)
            cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),
                        img_woCIR)
        else:
            cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)),
                        img_raw)
Пример #10
0
def ctpn():
    if os.path.exists(FLAGS.ctpn_output_path):
        shutil.rmtree(FLAGS.ctpn_output_path)
    os.makedirs(FLAGS.ctpn_output_path)
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
    tf.reset_default_graph()
    with tf.get_default_graph().as_default():
        
        input_image = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_image')
        input_im_info = tf.placeholder(tf.float32, shape=[None, 3], name='input_im_info')

        global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)

        bbox_pred, cls_pred, cls_prob = model.model(input_image)

        variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step)
        saver = tf.train.Saver(variable_averages.variables_to_restore())

        with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
            ckpt_state = tf.train.get_checkpoint_state(FLAGS.checkpoint_path)
            model_path = os.path.join(FLAGS.checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path))
            saver.restore(sess, model_path)

            im_fn_list = get_images(FLAGS.ctpn_input_path)
            for im_fn in im_fn_list:
                print('===============')
                print(im_fn)
                start = time.time()
                try:
                    img_raw = cv2.imread(im_fn)
                    #(thresh, img_raw) = cv2.threshold(img_raw, 127, 255, cv2.THRESH_BINARY)
                    # Create kernel
                    #kernel = np.array([[-1, -1, -1], 
                    #                   [-1, 9,-1], 
                    #                   [-1, -1, -1]])

                    # Sharpen image
                    #img_raw = cv2.filter2D(img_raw, -1, kernel)
 
                except:
                    print("Error reading image {}!".format(im_fn))
                    continue
                img_draw = img_raw.copy()

                img, (rh, rw) = resize_image(img_raw)
                # image used to draw bounding box

                h, w, c = img.shape
                for ifrot in ['orig','rot']:
                    im = img.copy()

                    if ifrot == 'rot':
                        im = cv2.transpose(im)
                        im = cv2.flip(im,1)
                        bbox_color = (255,0,0)
                        im_info = np.array([w, h, c]).reshape([1, 3])
                    else: 
                        bbox_color = (0,255,0)
                        im_info = np.array([h, w, c]).reshape([1, 3])
                    bbox_pred_val, cls_prob_val = sess.run([bbox_pred, cls_prob],
                                                           feed_dict={input_image: [im],
                                                                      input_im_info: im_info})
    
                    textsegs, _ = proposal_layer(cls_prob_val, bbox_pred_val, im_info)
                    scores = textsegs[:, 0]
                    textsegs = textsegs[:, 1:5]
                    
                    textdetector = TextDetector(DETECT_MODE='H')
                    boxes = textdetector.detect(textsegs, scores[:, np.newaxis], im.shape[:2])
                    boxes = np.array(boxes, dtype=np.int)
                    print(len(boxes))
                    cost_time = (time.time() - start)
                    print("cost time: {:.2f}s".format(cost_time))
                    fx=1.0 / rw
                    fy=1.0 / rh
                    for i, box in enumerate(boxes):
                        if ifrot == 'rot':
                            box = np.array([box[3],h-box[2],box[5],h-box[4],box[7],h-box[6],box[1],h-box[0],box[8]])
                        #resize the images
                        box[:8:2] = (box[:8:2]*fx).astype(np.int32)
                        box[1::2] = (box[1::2]*fy).astype(np.int32)
                        
                        cv2.polylines(img_draw, [box[:8].astype(np.int32).reshape((-1, 1, 2))], True, color=bbox_color, thickness=2)
                        # crop image with rectangle box and save
                        x0,y0,w0,h0 = cv2.boundingRect(box[:8].astype(np.int32).reshape((-1, 2)))
                        img_crop = img_raw[y0:y0+h0,x0:x0+w0].copy()

                        cv2.imwrite(os.path.join(FLAGS.ctpn_output_path, ifrot+str(format(i, "04"))+"-"+os.path.basename(im_fn)), img_crop) 
                        cv2.putText(img_draw, str(i), (box[0],box[1]), cv2.FONT_HERSHEY_SIMPLEX ,1.0, bbox_color, 2, cv2.LINE_AA) 
   
                    #im = cv2.resize(img, None, None, fx=1.0 / rh, fy=1.0 / rw, interpolation=cv2.INTER_LINEAR)
                    cv2.imwrite(os.path.join(FLAGS.ctpn_output_path, ifrot+"-"+os.path.basename(im_fn)),img_draw[:, :, ::-1])

                    with open(os.path.join(FLAGS.ctpn_output_path, ifrot+"-"+os.path.splitext(os.path.basename(im_fn))[0]) + ".txt",
                                "a") as f:
                        for i, box in enumerate(boxes):
                            line = ",".join(str(box[k]) for k in range(8))
                            line += "," + str(scores[i]) + "\r\n"
                            f.writelines(line)
                        f.close()