点回归标注工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csuzhaoqinghui/article/details/84797378
############################################################################################    
#!/usr/bin/python2.7    
# -*- coding: utf-8 -*-    
#Author  : zhaoqinghui    
#Date    : 2018.02.11   
#Function:  1. Mark lane lines
##########################################################################################  
  
import cv2
import copy as cp

###########################################################################################  
#set param  
###########################################################################################  

infilename="/media/zhao/LANE/zhao/badcase/xiangbei7"
outfilename="/home/zhao/data/txt" 

###########################################################################################  
  

def daire_ciz(event, x, y, flags, param):
    global flag_left, flag_right, resim , resim_copy
    if event == cv2.EVENT_LBUTTONDOWN:        
        if flag_left == True or (len(list_l) == 0 and len(list_r) == 0):
            print " mark left lane point "
            resim = cv2.circle(resim, (x, y), 2, (255, 0, 0), 1)
            list_l.append([y, x])
            index_l = len(list_l)
            if index_l > 1:
                resim=cv2.line(resim, (list_l[index_l-2][1], list_l[index_l-2][0]), (list_l[index_l-1][1] , list_l[index_l-1][0]), (0, 255,0), 2)
        if flag_right == True: 
            print " mark right lane point "
            resim = resim=cv2.circle(resim, (x, y), 2, (255, 0, 255), 1)
            list_r.append([y, x])
            index_r = len(list_r)
            if index_r > 1:
                resim = cv2.line(resim, (list_r[index_r-2][1], list_r[index_r-2][0]), (list_r[index_r-1][1] , list_r[index_r-1][0]), (0, 0,255), 2)
   
    elif event == cv2.EVENT_RBUTTONDOWN:
        flag_left = False
        flag_right = True
        print "switch to mark right lane !" 

    elif event == cv2.EVENT_MBUTTONDOWN :
         resim = cp.deepcopy(resim_copy)
         if (flag_left == True and len(list_l) > 0): 
             print " drop left lane point "
             list_l.pop(len(list_l) - 1)    
         if (len(list_r) == 0 and flag_right == True):
             print " drop left lane point "
             list_l.pop(len(list_l) - 1) 
             flag_left = True
             flag_right = False
         if flag_right == True and len(list_r) > 0 and flag_left == False:
             print " drop right lane point "
             list_r.pop(len(list_r) - 1) 
         for list_index in range(len(list_l)):
             cv2.circle(resim, (list_l[list_index][1], list_l[list_index][0]), 2, (255, 0, 0), 1)
             if len(list_l) > 1 and list_index < len(list_l) - 1:
                resim = cv2.line(resim, (list_l[list_index][1], list_l[list_index][0]), (list_l[list_index+1][1] , list_l[list_index+1][0]), (0, 255,0), 2)
         for list_index in range(len(list_r)):
             resim = cv2.circle(resim, (list_r[list_index][1], list_r[list_index][0]), 2, (255, 0, 255), 1)
             if len(list_r) > 1 and list_index < len(list_r) - 1:
                resim = cv2.line(resim, (list_r[list_index][1], list_r[list_index][0]), (list_r[list_index+1][1] , list_r[list_index+1][0]), (0, 0,255), 2)
         
def mkdir(path):
    import os
    path=path.strip()
    path=path.rstrip("\\")
    isExists=os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        return True
    else:
        return False
if __name__ == "__main__": 
    imgname = []
    mkdir(outfilename)
    with open(str(infilename+'/list.txt'), 'r') as f:
        while True: 
            line = f.readline() 
            line = line.strip('\n') 
            if not line:
                break
            imgname.append(line) 
    f.close()
    list_l = []
    list_r = []
    flag_left = True
    flag_right = False
    img_i=0

    cv2.namedWindow('markLane')
    cv2.setMouseCallback('markLane', daire_ciz)
    
    global resim,resim_copy
    resim = cv2.imread(infilename+"/"+imgname[img_i], 1)
    if resim is None :
        print "the image is not exsit !"
    print "read image "+infilename+"/"+imgname[img_i]
    resim_copy = cp.deepcopy(resim)

    while True:    
        cv2.imshow('markLane', resim)
        if  cv2.waitKey(10) &0xFF == 9: #TAB               
            with open(str(outfilename+"/"+imgname[img_i]+".txt"), 'w') as f:
                for list_index in range(len(list_l)):
                    f.write("0 "+ str(list_l[list_index][1])+" "+str(list_l[list_index][0])+"\n")
                for list_index in range(len(list_r)):
                    f.write("1 "+ str(list_r[list_index][1])+" "+str(list_r[list_index][0])+"\n")
            f.close()  
            print "finish save lane point file !" 

            flag_left = True
            flag_right = False 
            del list_l[:]
            del list_r[:]    	
            img_i = img_i + 1 
        
            resim = cv2.imread(infilename+"/"+imgname[img_i], 1)
            if resim is None :
                print "the image is not exsit !"
                break
            print "read image "+infilename+"/"+imgname[img_i]
            resim_copy = cp.deepcopy(resim)  
                           
    cv2.destroyAllWindows()

以上是博主作点回归时写的实时标注工具。

猜你喜欢

转载自blog.csdn.net/csuzhaoqinghui/article/details/84797378