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)
# 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)) predict = cv2.resize(predict, dsize=(320, 180)) warped = warp(predict) left_fit, right_fit, vars = polyfit.poly_fit_slide(warped) result, offset, Radius, k_error = draw( cv2.resize(frame, dsize=(320, 180)), warped, left_fit, right_fit) print('Total;', time.time() - s) cv2.imshow('warped', warped) cv2.imshow('res', result) cv2.waitKey(1)
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