def auto_lane(img): polyfit = Polyfit() img = cv2.resize(img,dsize=(320,180)) lower_black = np.array([0,0,0]) upper_black = np.array([180,255,70]) frame = warp(img) cv2.imshow('frame',frame) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower_black, upper_black) cv2.imwrite('./mask.jpg',mask) cv2.imshow('frame:',mask) cv2.waitKey(1) # frame = combined_threshold(frame) try: left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit =='None' and right_fit == 'None': result,offset,Radius = img,0,10000 cv2.imshow('re', cv2.resize(img, dsize=(640, 320))) cv2.waitKey(1) elif left_fit !='None' and right_fit == 'None' or left_fit !='None' and right_fit != 'None' : result,offset,Radius = draw(img, mask, left_fit, right_fit,para = 277,meter = 30) cv2.imshow('re',cv2.resize(result,dsize=(640,320))) cv2.waitKey(1) else: print '-----1----unexcept error--------1----' result,offset,Radius = img,0,10000 except: print '---------unexcept error------------' result,offset,Radius = img,0,10000 return result,offset,Radius
def predict(video_path): polyfit = Polyfit() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) loader = tf.train.import_meta_graph( './model_test/lane-conv--490--299.meta') loader.restore(sess, './model_test/lane-conv--490--299') y = tf.get_collection('pred_network')[0] graph = tf.get_default_graph() input_x = graph.get_operation_by_name('x_input').outputs[0] keep_prob = graph.get_operation_by_name('prob').outputs[0] cap = cv2.VideoCapture(video_path) ret, frame = cap.read() while ret: ret, frame = cap.read() # cv2.imshow("img",frame) img_arr = cv2.resize(frame, dsize=(107, 60)) img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY) imgarr = np.reshape(img_arr, newshape=[1, 60, 107, 1]) predict = sess.run(y, feed_dict={ input_x: imgarr, keep_prob: 1.0 })[0] predict = np.uint8(np.where(predict == 0, 0, 255)) predict = cv2.resize(predict, dsize=(320, 180)) warped = warp(predict) left_fit, right_fit, vars = polyfit.poly_fit_slide(warped) print(left_fit) result, offset, Radius, k_error = draw( cv2.resize(frame, dsize=(320, 180)), warped, left_fit, right_fit) cv2.imshow('warped', warped) cv2.imshow('res', result) cv2.waitKey(1)
def draw_lane(img_arr): polyfit = Polyfit() combined_output = combined_threshold(img_arr) warped = warp(combined_output) left_fit, right_fit, vars = polyfit.poly_fit_skip(warped) # print(left_fit,right_fit) result = draw(img_arr, warped, left_fit, right_fit) return result,left_fit,right_fit
def predict(video_path): polyfit = Polyfit() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) loader = tf.train.import_meta_graph( './model_test/lane-conv--490--299.meta') loader.restore(sess, './model_test/lane-conv--490--299') y = tf.get_collection('pred_network')[0] graph = tf.get_default_graph() input_x = graph.get_operation_by_name('x_input').outputs[0] keep_prob = graph.get_operation_by_name('prob').outputs[0] cap = cv2.VideoCapture(video_path) ret, frame = cap.read() n = 0 while ret: ret, frame = cap.read() # cv2.imshow("img",frame) img_arr = cv2.resize(frame, dsize=(107, 60)) img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY) imgarr = np.reshape(img_arr, newshape=[1, 60, 107, 1]) predict = sess.run(y, feed_dict={ input_x: imgarr, keep_prob: 1.0 })[0] predict = np.uint8(np.where(predict == 0, 0, 255)) predict = cv2.resize(predict, dsize=(320, 180)) cv2.imwrite("./predict/" + str(n) + '.jpg', predict) n = n + 1 cv2.imshow('pre', predict) cv2.waitKey(1)
def auto_lane(img, n): polyfit = Polyfit() img = cv2.resize(img, dsize=(320, 180)) frame = warp(img) cv2.imshow('warped', frame) # mask_flag = hsv(frame) # dec_flag = dec_lane(mask_flag) # mask = sobel(frame) # corner_det(mask,frame) mask = hsv(frame) print(mask[2]) cv2.waitKey(1) #mask = cv2.GaussianBlur(frame,(5,5),0) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) mask = cv2.erode(mask, kernel) mask = cv2.dilate(mask, kernel) cv2.imwrite('./image1/' + str(n) + '.jpg', hsv(img)) cv2.imwrite('./mask.jpg', mask) cv2.imshow('frame:', mask) cv2.waitKey(1) # frame = combined_threshold(frame) try: left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit == 'None' and right_fit == 'None': result, offset, Radius = img, 'None', 10000 cv2.imshow('re', cv2.resize(img, dsize=(640, 320))) cv2.waitKey(1) elif left_fit != 'None' and right_fit == 'None' or left_fit != 'None' and right_fit != 'None': result, offset, Radius = draw(img, mask, left_fit, right_fit) cv2.imshow('re', cv2.resize(result, dsize=(640, 320))) cv2.waitKey(1) else: result, offset, Radius = img, 0, 10000 except: result, offset, Radius = img, 0, 10000 '''offset --------------------''' if offset == 'None': offset = 'None' else: offset = offset + 24.00 return result, offset, Radius
def auto_lane(img): polyfit = Polyfit() img = cv2.resize(img,dsize=(320,180)) frame = warp(img) cv2.imshow('warped',frame) mask_flag = hsv(frame) dec_flag = dec_lane(mask_flag) print 'dec_lane flag = ',dec_flag mask = sobel(frame) #mask = hsv(frame) #mask = canny(frame,50,150) #mask = cv2.GaussianBlur(frame,(5,5),0) kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2)) mask = cv2.erode(mask,kernel) mask = cv2.dilate(mask,kernel) cv2.imshow('mask',mask) cv2.waitKey(1) # frame = combined_threshold(frame) try: left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit =='None' and right_fit == 'None': result,offset,Radius = img,'None',10000 cv2.imshow('re', cv2.resize(img, dsize=(640, 320))) cv2.waitKey(1) elif left_fit !='None' and right_fit == 'None' or left_fit !='None' and right_fit != 'None' : result,offset,Radius = draw(img, mask, left_fit, right_fit) cv2.imshow('re',cv2.resize(result,dsize=(640,320))) cv2.waitKey(1) else: print '-----1----unexcept error--------1----' result,offset,Radius = img,0,10000 except: print '---------unexcept error------------' result,offset,Radius = img,0,10000 '''offset --------------------''' if offset == 'None': offset = 'None' else: offset = offset return result,offset,Radius
def auto_lane(img): polyfit = Polyfit() img = cv2.resize(img, dsize=(320, 180)) cv2.imwrite('./resize.jpg', img) warped = warp(img) mask = abs_threshold(warped) # mask = hsv(warped) # frame = combined_threshold(frame) left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit == 'None' or right_fit == 'None': result, offset, Radius = None, None, None cv2.imshow('re', cv2.resize(img, dsize=(853, 480))) cv2.waitKey(0) return result, offset, Radius else: result, offset, Radius = draw(img, mask, left_fit, right_fit) cv2.imshow('re', cv2.resize(result, dsize=(853, 480))) cv2.waitKey(0) cv2.imwrite("./res.jpg", result) return result, offset, Radius
def auto_lane(img): polyfit = Polyfit() img = cv2.resize(img, dsize=(320, 180)) lower_black = np.array([0, 0, 0]) upper_black = np.array([180, 255, 70]) frame = warp(img) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower_black, upper_black) cv2.imshow('mask', mask) # frame = combined_threshold(frame) left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit == 'None' or right_fit == 'None': result, offset, Radius = None, None, None cv2.imshow('re', cv2.resize(img, dsize=(853, 480))) cv2.waitKey(0) return result, offset, Radius else: result, offset, Radius = draw(img, mask, left_fit, right_fit) cv2.imshow('re', cv2.resize(result, dsize=(853, 480))) cv2.waitKey(0) return result, offset, Radius
def auto_lane(img): polyfit = Polyfit() img = cv2.resize(img, dsize=(320, 180)) #light recongise GreenLight, RedLight, SpeedLimit, HireSign = recongise(img) GreenLight = main(img) frame = warp(img) mask = hsv(frame) # dec_flag = dec_lane(mask) #mask = canny(frame,50,150) #mask = cv2.GaussianBlur(frame,(5,5),0) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) mask = cv2.erode(mask, kernel) mask = cv2.dilate(mask, kernel) cv2.imshow('mask', mask) cv2.waitKey(1) try: left_fit, right_fit, vars = polyfit.poly_fit_skip(mask) if left_fit == 'None' and right_fit == 'None': result, offset, Radius, k_error = img, 'None', 10000, 0 ## cv2.imshow('re', cv2.resize(img, dsize=(640, 320))) ## cv2.waitKey(1) elif left_fit != 'None' and right_fit == 'None' or left_fit != 'None' and right_fit != 'None': result, offset, Radius, k_error = draw(img, mask, left_fit, right_fit) ## cv2.imshow('re',cv2.resize(result,dsize=(640,320))) cv2.waitKey(1) else: result, offset, Radius, k_error = img, 0, 10000, 0 except: result, offset, Radius, k_error = img, 0, 10000, 0 '''offset --------------------''' '''--------------------------lanetype--------------------------''' '''zhixian:1..wandao:2...zhijiaowan: 3''' if offset == 'None': offset = 'None' else: offset = offset if abs(k_error) < 0.06: cur_mode = 1 #wandao elif abs(k_error) >= 0.06: cur_mode = 2 #zhidao elif k_error == 'None': cur_mode = last_mode #keeping '''cornerflage''' corner_flag = corner_det(mask, img) if corner_flag == 0: pass if corner_flag == 1: cur_mode = 3 last_mode = cur_mode Lane_type = cur_mode '''-----------SpeedLimit----------''' SpeedLimit = 0 '''-----------RedLight------------''' RedLight = 0 '''-----------GreenLight----------''' GreenLight = 0 '''-----------HireSign------------''' HireSign = 0 '''-----------StopSign------------''' StopSign = 0 cv2.imshow('result', result) cv2.waitKey(1) return result, offset, Radius, Lane_type, SpeedLimit, RedLight, GreenLight, HireSign, StopSign
'''-----------SpeedLimit----------''' SpeedLimit = 0 '''-----------RedLight------------''' RedLight = 0 '''-----------GreenLight----------''' GreenLight = 0 '''-----------HireSign------------''' HireSign = 0 '''-----------StopSign------------''' StopSign = 0 cv2.imshow('result', result) cv2.waitKey(1) return result, offset, Radius, Lane_type, SpeedLimit, RedLight, GreenLight, HireSign, StopSign if __name__ == "__main__": polyfit = Polyfit() original_video = './test4.avi' cap = cv2.VideoCapture(original_video) n = 0 while (1): n = n + 1 ret, img = cap.read() #cv2.imwrite('./tst3/' + str(n) + '.jpg',img) start = time.time() ## cv2.imshow('01',img) ## cv2.waitKey(1) result, offset, Radius, _, _, _, _, _, _ = auto_lane(img) print('Time per px :', time.time() - start) print(offset, Radius) ''' polyfit = Polyfit()
# print(data['x'].shape,data['y'].shape) train(3000) # # Single image process # filename = selectfile() # img_arr = cv2.imread(filename) # start = time.time() # result = prediction(img_arr) # print('time:',time.time()-start) # cv2.imshow('result',result) # cv2.waitKey(0) # Video process # VideoProcee(input_path='./test5.avi',output_path='./lane_detect.avi',timeF=1,fps=30) # train() # prediction() # data_read() sess = tf.Session() polyfit = Polyfit() sess.run(tf.global_variables_initializer()) loader = tf.train.import_meta_graph('./model_test/lane-conv---99.meta') loader.restore(sess, './model_test/lane-conv---99') y = tf.get_collection('pred_network')[0] graph = tf.get_default_graph() input_x = graph.get_operation_by_name('x_input').outputs[0] keep_prob = graph.get_operation_by_name('prob').outputs[0] cap = cv2.VideoCapture('./test5.avi') ret, frame = cap.read() while ret: s = time.time() ret, frame = cap.read() imgarr = cv2.resize(frame, dsize=(107, 60)) predict = cnn_lane(sess, imgarr) predict = np.uint8(np.where(predict == 0, 0, 255))
def auto_lane(img): global cur_mode global last_mode global counter start = time.time() img = cv2.resize(img, (320, 180)) GreenLight, RedLight, SpeedLimit, HireSign = recongise(img) ## GreenLight = circle_dec(img) polyfit = Polyfit() #light recongise ## GreenLight = recongise_light(img) frame = warp(img) mask = hsv(frame) cv2.imshow('warpedmask', frame) ## mask = canny(frame,50,150) zebracrossing = dec_lane(mask) print '......................0.0.0.13212', zebracrossing #mask = cv2.GaussianBlur(frame,(5,5),0) ## kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2)) ## mask = cv2.erode(mask,kernel) ## mask = cv2.dilate(mask,kernel) ## cv2.imshow('mask',mask) cv2.waitKey(1) try: left_fit, right_fit, vars = polyfit.poly_fit_slide(mask) print '---------------------left,right', left_fit, right_fit if left_fit == 'None' and right_fit == 'None': print '11111circle' result, offset, Radius, k_error = img, 'None', 10000, 'None' ## cv2.imshow('re', cv2.resize(img, dsize=(640, 320))) ## cv2.waitKey(1) else: print '2222circle' result, offset, Radius, k_error = draw(img, mask, left_fit, right_fit) ## cv2.imshow('re',cv2.resize(result,dsize=(640,320))) ## cv2.waitKey(1) except: print '---------unexcept error------------' result, offset, Radius, k_error = img, 0, 10000, 'None' '''--------------------------lanetype--------------------------''' '''zhixian:1..wandao:2...zhijiaowan: 3''' print "........kerror:...", k_error if k_error == 'None': cur_mode = last_mode #keeping else: if abs(k_error) < 0.06: cur_mode = 1 if abs(k_error) >= 0.06: cur_mode = 2 '''cornerflage''' print '--------', cur_mode, '------', last_mode corner_flag = corner_det(mask, img) if corner_flag == 0: pass if corner_flag == 1: cur_mode = 3 last_mode = cur_mode Lane_type = cur_mode '''-----------SpeedLimit----------''' SpeedLimit = 0 '''-----------RedLight------------''' GreenLight = 0 '''-----------HireSign------------''' '''-----------StopSign------------''' StopSign = 0 print 'Corner .....', Lane_type cv2.imshow('result', result) cv2.waitKey(1) return result, offset, Radius, Lane_type, SpeedLimit, RedLight, GreenLight, HireSign, StopSign, zebracrossing
return result, left_fit, right_fit if __name__ == "__main__": # polyfit = Polyfit() # original_video = './video.mp4' # cap = cv2.VideoCapture(original_video) # n = 0 # while (1): # n = n+1 # ret,img = cap.read() # img = cv2.resize(img,dsize=(320,180)) # frame = warp(img) # cv2.imshow('warp',frame) # frame = combined_threshold(frame) # left_fit, right_fit, vars = polyfit.poly_fit_skip(frame) # result = draw(img, frame, left_fit, right_fit) # cv2.imshow('re', result) # cv2.waitKey(1) #1280 720 polyfit = Polyfit() img = cv2.imread('./101.jpg') frame = warp(img) frame = combined_threshold(frame) left_fit, right_fit, vars = polyfit.poly_fit_skip(frame) result = draw(img, frame, left_fit, right_fit) cv2.imshow('re', result) cv2.waitKey(0)