def process_level(self, level, u0, v0): I0_ = self.I0pyr[level] I1_ = self.I1pyr[level] #print I1_.shape, u0.shape, self.I0pyr[level].shape wrpr = Warper(I0_.shape, u0, v0, I0_, I1_, train_function=self.class_train_function(u0, v0, **self.train_function_args), display=0) for i in range(self.warps): wrpr.warp() self.u, self.v = wrpr.u, wrpr.v
class Loss(): def __init__(self, opts): super(Loss, self).__init__() self.ssim_wt = opts.ssim_wt self.l1_wt = opts.l1_wt self.smooth_wt = opts.smooth_wt self.ssim = SSIM(opts) self.warper = Warper(opts) # for debugging only self.recon_im = [] def __call__(self, in_data, disp): left_im = in_data['left_im'] right_im = in_data['right_im'] right_recon = self.warper.warp(left_im, disp) self.recon_im = right_recon photo_loss = self.ssim_wt * self.ssim(right_im, right_recon).mean() + \ self.l1_wt * (right_recon - right_im).abs().mean() smooth_loss = self.compute_smooth_loss(disp, left_im) net_loss = (1.0 - self.smooth_wt) * photo_loss + self.smooth_wt * smooth_loss return {'photo_loss': photo_loss, 'smooth_loss': smooth_loss}, net_loss def compute_smooth_loss(self, depth, frame): depth = depth.unsqueeze(1) mean_depth = depth.mean(2, True).mean(3, True) n_depth = depth / (mean_depth + 1e-7) grad_depth_x = torch.abs(n_depth[:, :, :, :-1] - n_depth[:, :, :, 1:]) grad_depth_y = torch.abs(n_depth[:, :, :-1, :] - n_depth[:, :, 1:, :]) grad_fr_x = torch.mean(torch.abs(frame[:, :, :, :-1] - frame[:, :, :, 1:]), 1, keepdim=True) grad_fr_y = torch.mean(torch.abs(frame[:, :, :-1, :] - frame[:, :, 1:, :]), 1, keepdim=True) wt_grad_depth_x = grad_depth_x * torch.exp(-grad_fr_x) wt_grad_depth_y = grad_depth_y * torch.exp(-grad_fr_y) return torch.mean(wt_grad_depth_x) + torch.mean(wt_grad_depth_y)
def process_level(self, level, u0, v0): I0_ = self.I0pyr[level] I1_ = self.I1pyr[level] #print I1_.shape, u0.shape, self.I0pyr[level].shape wrpr = Warper(I0_.shape, u0, v0, I0_, I1_, train_function=self.class_train_function( u0, v0, **self.train_function_args), display=0) for i in range(self.warps): wrpr.warp() self.u, self.v = wrpr.u, wrpr.v
import cv2 import numpy as np from thresholder import Thresholder from warper import Warper from polyfitter import Polyfitter from polydrawer import Polydrawer if __name__ == '__main__': cap = cv2.VideoCapture(0) threshold_object = Thresholder() warp_object = Warper() fitt_object = Polyfitter() draw_object = Polydrawer() while (cap.isOpened()): ret, color = cap.read() print color.shape th = threshold_object.threshold(color) # cv2.imshow('Combined', threshold_object.threshold(color)) # cv2.imshow('Warped', warp_object.warp(th)) img = warp_object.warp(th) left_fit, right_fit = fitt_object.polyfit(img) print left_fit, right_fit img = draw_object.draw(color, left_fit, right_fit, warp_object.Minv) cv2.imshow('Lane Detection', img) if cv2.waitKey(1) & 0xFF == 27: break cv2.destroyAllWindows()
def main(): os.environ["CUDA_VISIBLE_DEVICES"] = '0' #cudnn.benchmark = True pidcal = PidCal() opt = opts.parse() warper = Warper() slidewindow = SlideWindow() print(("device id: {}".format(torch.cuda.current_device()))) print("torch.version", torch.__version__) print("cuda_version", torch.version.cuda) models = importlib.import_module('models.init') # print(models) criterions = importlib.import_module('criterions.init') checkpoints = importlib.import_module('checkpoints') Trainer = importlib.import_module('models.' + opt.netType + '-train') # Data loading print('=> Setting up data loader') # Load previous checkpoint, if it exists print('=> Checking checkpoints') checkpoint = checkpoints.load(opt) # Create model model, optimState = models.setup(opt, checkpoint) model.cuda() criterion = criterions.setup(opt, checkpoint, model) ################################################################################## model.eval() cap = cv2.VideoCapture("input_video/test.avi") if cap.isOpened(): print("width : {}, height : {}".format(cap.get(3), cap.get(4))) video_width = int(cap.get(3)) video_height = int(cap.get(4)) fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # out = cv2.VideoWriter('output_video/TEST_1.avi', fourcc, 20.0, (1280,480),0) prev_time = 0 fps_list = [] # fourcc =cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('input_video/processed_video.avi', fourcc, 40.0, (480, 320), 0) steer_list = list() lpf_list = list() while True: ret, frame = cap.read() if ret: cur_time = time.time() frame_new = cv2.resize(frame, (320, 180)) input_img = frame_new / 255. input_img = preprocess_img(input_img) # array to tensor input_img = torch.from_numpy(input_img).float() with torch.no_grad(): inputData_var = Variable(input_img).unsqueeze(0).cuda() # inference output = model.forward(inputData_var) output = torch.sigmoid(output) #output = F.softmax(output, dim=1) # gpu -> cpu, tensor -> numpy output = output.detach().cpu().numpy() output = output[0] output = postprocess_img(output) output = np.clip(output, 0, 1) output *= 255 output = np.uint8(output) output = cv2.resize(output, (640, 360)) output[output > 80] = 255 output[output <= 80] = 0 warper_img, point_img = warper.warp(output) ret, left_start_x, right_start_x, cf_img = slidewindow.w_slidewindow( warper_img) if ret: left_x_current, right_x_current, sliding_img, steer_theta = slidewindow.h_slidewindow( warper_img, left_start_x, right_start_x) cv2.imshow('sliding_img', sliding_img) steer_list.append(steer_theta) lpf_result = lpf(steer_theta, 0.5) lpf_list.append(lpf_result) print("steer theta:", steer_theta) if steer_theta < -28 or steer_theta > 28: continue else: pid = round(pidcal.pid_control(int(50 * steer_theta)), 6) print("pid :", pid) ''' auto_drive(pid) ''' else: pidcal.error_sum = 0 end_time = time.time() sec = end_time - cur_time fps = 1 / sec fps_list.append(fps) print("Estimated fps {0} ".format(fps)) # out.write(add_img) cv2.imshow("frame", frame) out.write(warper_img) # cv2.imshow("src", warper_img) # cv2.imshow("out_img", output) cv2.imshow("cf_img", cf_img) key = cv2.waitKey(1) & 0xFF if key == 27: break elif key == ord('p'): cv2.waitKey(-1) plt.figure(1) plt.plot(steer_list) plt.figure(2) plt.plot(lpf_list) plt.show()
def main(): os.environ["CUDA_VISIBLE_DEVICES"] = '0' #cudnn.benchmark = True opt = opts.parse() warper = Warper() #slidewindow = SlideWindow() # slidewindow = LineDetector() pidcal = PidCal() print(("device id: {}".format(torch.cuda.current_device()))) print("torch.version", torch.__version__) print("cuda_version", torch.version.cuda) models = importlib.import_module('models.init') # print(models) criterions = importlib.import_module('criterions.init') checkpoints = importlib.import_module('checkpoints') Trainer = importlib.import_module('models.' + opt.netType + '-train') # Data loading print('=> Setting up data loader') #trainLoader, valLoader = DataLoader.create(opt) #print('opt',opt) # Load previous checkpoint, if it exists print('=> Checking checkpoints') checkpoint = checkpoints.load(opt) # Create model model, optimState = models.setup(opt, checkpoint) model.cuda() criterion = criterions.setup(opt, checkpoint, model) ################################################################################## model.eval() cap = cv2.VideoCapture( "/home/foscar/ISCC_2019/src/race/src/my_lane_detection/input_video/0.avi" ) ret, frame = cap.read() slidewindow = LineDetector(frame) if cap.isOpened(): print("width : {}, height : {}".format(cap.get(3), cap.get(4))) video_width = int(cap.get(3)) video_height = int(cap.get(4)) fourcc = cv2.VideoWriter_fourcc(*'DIVX') video_name = time.time() out = cv2.VideoWriter('output_video/{}.avi'.format(video_name), fourcc, 25.0, (video_width, video_height), 0) prev_time = 0 count = 0 while True: ret, frame = cap.read() count += 1 if ret: cur_time = time.time() frame = cv2.resize(frame, (480, 360)) input_img = frame / 255. input_img = preprocess_img(input_img) # array to tensor input_img = torch.from_numpy(input_img).float() with torch.no_grad(): inputData_var = Variable(input_img).unsqueeze(0).cuda() # inference output = model.forward(inputData_var) print("output.shape : ", output.shape) # gpu -> cpu, tensor -> numpy output = output.detach().cpu().numpy() output = output[0] output = postprocess_img(output) # cv2.imshow("203",output) output *= 255 output = np.clip(output, 0, 255) output = np.uint8(output) # resize output = cv2.resize(output, (640, 480)) # cv2.imshow('resize',output) # threshold ret, thr_img = cv2.threshold(output, 20, 255, 0) # cv2.imshow('threshold',thr_img) # warp warp_img = warper.warp(thr_img) # cv2.imshow('warped',warp_img) # cv2.imshow("new output", canny_like_output) #canny = cv2.Canny(warp_img, 40, 255) kernel1 = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) kernel2 = np.ones(( 5, 5, ), np.uint8) #dilate = cv2.dilate(warp_img, kernel1, iterations=2) #closed = cv2.morphologyEx(dilate, cv2.MORPH_OPEN, kernel2) # x_start_L, x_start_R=slidewindow.find_sliding_point(warp_img) # img, x_location = slidewindow.slide_window(x_start_L,x_start_R,warp_img) slided_img, x_location, point_list_left, point_list_right = slidewindow.main( warp_img) if x_location != None: # cv2.circle(img,(int(x_location),300),5,(0,0,255),3) pid = round(pidcal.pid_control(int(x_location)), 6) #print("pid rate : ", pid) auto_drive(pid, x_location) else: pid = pidcal.pid_control(slidewindow_middle) print("pid rate : ", pid) auto_drive(pid) end_time = time.time() sec = end_time - cur_time fps = 1 / sec fps_list.append(fps) print("Estimated fps {0} ".format(fps)) out.write(output) cv2.imshow("src", frame) pid_draw.append(pid) # cv2.imshow("th_img", thr_img) # cv2.imshow("output", output) # img = cv2.imread('/home/foscar/Downloads/wapped_screenshot_12.08.2020.png',cv2.IMREAD_GRAYSCALE) # img = cv2.resize(img, (640, 480)) # ret, thr_img = cv2.threshold(img,20,255,cv2.THRESH_BINARY) # img ,xloc, point_list_left, point_list_right = slidewindow.main(thr_img) # plt.xlim(0,640) # plt.ylim(0,480) # plt.plot(point_list_left[0], point_list_left[1]) # plt.plot(point_list_right[0], point_list_right[1]) # plt.show() # cv2.imshow('aa',img) # key = cv2.waitKey(1) & 0xFFx # if key == 27: break # elif key == ord('p'): # cv2.waitKey(-1) cv2.imshow("ws", slided_img) print("x_loc :", x_location) key = cv2.waitKey(1) & 0xFF if key == 27: break elif key == ord('p'): cv2.waitKey(-1)
def main(): os.environ["CUDA_VISIBLE_DEVICES"] = '0' #cudnn.benchmark = True opt = opts.parse() warper = Warper() slidewindow = SlideWindow() pidcal = PidCal() print(("device id: {}".format(torch.cuda.current_device()))) print("torch.version",torch.__version__) print("cuda_version",torch.version.cuda) models = importlib.import_module('models.init') # print(models) criterions = importlib.import_module('criterions.init') checkpoints = importlib.import_module('checkpoints') Trainer = importlib.import_module('models.' + opt.netType + '-train') # Data loading print('=> Setting up data loader') #trainLoader, valLoader = DataLoader.create(opt) #print('opt',opt) # Load previous checkpoint, if it exists print('=> Checking checkpoints') checkpoint = checkpoints.load(opt) # Create model model, optimState = models.setup(opt, checkpoint) model.cuda() criterion = criterions.setup(opt, checkpoint, model) ################################################################################## model.eval() cap = cv2.VideoCapture(0) if cap.isOpened(): print("width : {}, height : {}".format(cap.get(3), cap.get(4))) video_width = int(cap.get(3)) video_height = int(cap.get(4)) fourcc = cv2.VideoWriter_fourcc(*'DIVX') video_name = time.time() out = cv2.VideoWriter('output_video/{}.avi'.format(video_name), fourcc, 25.0, (video_width,video_height),0) prev_time = 0 fps_list = [] while True: ret, frame = cap.read() if ret: cur_time = time.time() frame = cv2.resize(frame, (480,360)) input_img = frame / 255. input_img = preprocess_img(input_img) # array to tensor input_img = torch.from_numpy(input_img).float() with torch.no_grad(): inputData_var = Variable(input_img).unsqueeze(0).cuda() # inference output = model.forward(inputData_var) print("output.shape : ", output.shape) # gpu -> cpu, tensor -> numpy output = output.detach().cpu().numpy() output = output[0] output = postprocess_img(output) output *= 255 output = np.clip(output, 0, 255) output = np.uint8(output) # resize output = cv2.resize(output, (640, 480)) # threshold ret, thr_img = cv2.threshold(output, 180, 255, 0) # warp output, warp_img = warper.warp(output, thr_img) img, x_location = slidewindow.slidewindow(warp_img) if x_location != None: pid = round(pidcal.pid_control(int(x_location)), 6) print("pid rate : ", pid) auto_drive(pid, x_location) else: pid = pidcal.pid_control(320) print("pid rate : ", pid) auto_drive(pid) end_time = time.time() sec = end_time - cur_time fps = 1/sec fps_list.append(fps) print("Estimated fps {0} " . format(fps)) out.write(output) cv2.imshow("src", frame) cv2.imshow("output", output) cv2.imshow("thre", img) key = cv2.waitKey(1) & 0xFF if key == 27: break elif key == ord('p'): cv2.waitKey(-1)
class LaneFinder: def __init__(self, gray_shape, objpoints, imgpoints): self.camera_undistort = CameraUndistort(gray_shape, objpoints, imgpoints) self.gradient_filter = GradientFilter() self.warper = Warper(gray_shape) self.window_searcher = WindowSearcher() self.cnt = 0 def run_pipeline(self, img, left_fit=None, right_fit=None): self.cnt += 1 dst = self.camera_undistort.undistort(img) #cv2.imwrite('../output_images/undistort_images/undistort_' + file_name, dst) self.binary = self.gradient_filter.process(dst) # cv2.imwrite('../output_images/gradient_filter/filter_' + file_name, binary.astype('uint8') * 255) # cv2.imwrite('../output_images/debug/binary_%d.png' % self.cnt, self.binary.astype('uint8') * 255) #cv2.polylines(img, [np.int32(warper.src.reshape(-1,1,2))], True,(0,0,255), 3) #cv2.imwrite('../output_images/warped/src_' + file_name, img) # warped = warper.warp(img) # cv2.polylines(warped, [np.int32(warper.dst.reshape(-1,1,2))], True, (0,0,255), 3) # cv2.imwrite('../output_images/warped/dst_' + file_name, warped) self.warped = self.warper.warp(self.binary) # cv2.imwrite('../output_images/debug/warped_%d.png' % self.cnt, self.warped.astype('uint8') * 255) # cv2.imwrite('../output_images/warped/dst_binary_' + file_name, warped.astype('uint8') * 255) out_img = self.window_searcher.fit_polynomial(self.warped, left_fit, right_fit) # cv2.imwrite('../output_images/debug/out_%d.png' % self.cnt, out_img) # cv2.imwrite('../output_images/window_search/' + file_name, out_img) # unwarped = warper.unwarp(out_img) # cv2.imwrite('../output_images/unwarped/' + file_name, unwarped) # print(window_searcher.measure_curvature_real()) # print(window_searcher.measure_center()) def add_detect_lanes(self, img, left_line, right_line): ploty = left_line.ally left_fitx = left_line.allx right_fitx = right_line.allx dst = self.camera_undistort.undistort(img) warp_zero = np.zeros_like(self.warped).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) # Recast the x and y points into usable format for cv2.fillPoly() pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) pts_right = np.array( [np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) pts = np.hstack((pts_left, pts_right)) # Draw the lane onto the warped blank image cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0)) # Warp the blank back to original image space using inverse perspective matrix (Minv) newwarp = self.warper.unwarp(color_warp) # Combine the result with the original image result = cv2.addWeighted(dst, 1, newwarp, 0.3, 0) # cv2.imwrite('../output_images/final/' + file_name, result) font = cv2.FONT_HERSHEY_SIMPLEX result = cv2.putText( result, 'radius of curvature = %d m' % ((left_line.radius_of_curvature + right_line.radius_of_curvature) / 2.0), (50, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA) result = cv2.putText( result, 'distance from the center = %.2f m' % left_line.line_base_pos, (50, 100), font, 1, (255, 255, 255), 2, cv2.LINE_AA) return result
def main(): os.environ["CUDA_VISIBLE_DEVICES"] = '0' #cudnn.benchmark = True pidcal = PidCal() opt = opts.parse() warper = Warper() slidewindow = SlideWindow() print(("device id: {}".format(torch.cuda.current_device()))) print("torch.version", torch.__version__) print("cuda_version", torch.version.cuda) models = importlib.import_module('models.init') # print(models) criterions = importlib.import_module('criterions.init') checkpoints = importlib.import_module('checkpoints') Trainer = importlib.import_module('models.' + opt.netType + '-train') # Data loading print('=> Setting up data loader') # Load previous checkpoint, if it exists print('=> Checking checkpoints') checkpoint = checkpoints.load(opt) # Create model model, optimState = models.setup(opt, checkpoint) model.cuda() criterion = criterions.setup(opt, checkpoint, model) ################################################################################## model.eval() cap = None if opt.video_idx is 0: cap = cv2.VideoCapture("input_video/720p.mp4") elif opt.video_idx is 1: cap = cv2.VideoCapture("input_video/straight.avi") elif opt.video_idx is 2: cap = cv2.VideoCapture("input_video/test.avi") else: cap = cv2.VideoCapture(0) # video test cap.set(3, 1280) cap.set(4, 720) if cap.isOpened(): print("width : {}, height : {}".format(cap.get(3), cap.get(4))) video_width = int(cap.get(3)) video_height = int(cap.get(4)) #fourcc = cv2.VideoWriter_fourcc('M','J','P','G') # out = cv2.VideoWriter('output_video/TEST_1.avi', fourcc, 20.0, (1280,480),0) prev_time = 0 fps_list = [] now = datetime.datetime.now() fourcc = None out = None if opt.video_idx > 2: fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output_video/' + str(now) + '.avi', fourcc, 30.0, (1280, 720)) pid_list = list() steer_list = list() lpf_list = list() pid_old = None i = 0 while True: ret, frame = cap.read() if frame is None: break frame_height, frame_width, frame_channels = frame.shape print("Frame Info : (Height, Width, Channels) : ({}, {}, {})".format( frame_height, frame_width, frame_channels)) record_frame = cv2.resize(frame, (1280, 720)) if ret: cur_time = time.time() frame_new = cv2.resize(frame, (320, 180)) input_img = frame_new / 255. input_img = preprocess_img(input_img) # array to tensor input_img = torch.from_numpy(input_img).float() with torch.no_grad(): inputData_var = Variable(input_img).unsqueeze(0).cuda() # inference output = model.forward(inputData_var) output = torch.sigmoid(output) #output = F.softmax(output, dim=1) # gpu -> cpu, tensor -> numpy output = output.detach().cpu().numpy() output = output[0] output = postprocess_img(output) output = np.clip(output, 0, 1) output *= 255 output = np.uint8(output) output = cv2.resize(output, (640, 360)) output[output > 80] = 255 output[output <= 80] = 0 cv2.imshow("output_img", output) warper_img = warper.warp(output) cv2.imshow("warp_img", warper_img) # warper_img_test = warper.warp_test(output) # cv2.imshow("warp_img_test",warper_img_test) ret, left_start_x, right_start_x, cf_img = slidewindow.w_slidewindow( warper_img) if ret: i += 1 left_x_current, right_x_current, sliding_img, steer_theta = slidewindow.h_slidewindow( warper_img, left_start_x, right_start_x) cv2.imshow('sliding_img', sliding_img) steer_list.append(steer_theta) # low pass filter lpf_result = lpf(steer_theta, 0.3) lpf_list.append(lpf_result) # steer theta : Degree print("steer theta:", steer_theta) if steer_theta < -28.0 or steer_theta > 28.0: auto_drive(pid_old) else: # degree angle pid = round(pidcal.pid_control(lpf_result), 6) pid_list.append(pid) print("pid :", pid) pid_old = pid auto_drive(pid) else: pidcal.error_sum = 0 pidcal.error_old = 0 end_time = time.time() sec = end_time - cur_time fps = 1 / sec fps_list.append(fps) print("Estimated fps {0} ".format(fps)) # out.write(add_img) cv2.imshow("frame", frame) if opt.video_idx > 2: print("frame.shape : {}".format(frame.shape)) out.write(frame) # cv2.imshow("src", warper_img) # cv2.imshow("out_img", output) cv2.imshow("cf_img", cf_img) key = cv2.waitKey(1) & 0xFF if key == 27: break elif key == ord('p'): cv2.waitKey(-1) cap.release() cv2.destroyAllWindows() plt.plot(range(i), steer_list, label='steer') plt.legend() plt.plot(range(i), pid_list, label='pid') plt.legend() plt.plot(range(i), lpf_list, label='lpf') plt.legend() plt.savefig('output_video/' + str(now) + '.png', dpi=300)
nonzero = self.check_many_lines(warp_img) if nonzero > 1350 and self.check_time(): self.on_detected_crosswallk() self.previous_time = time.time() return True return False if __name__ == '__main__': stop_counter = StopDetector() warper = None cap = cv2.VideoCapture('../capture/origin18654.avi') while cap.isOpened(): ret, frame = cap.read() if not ret: break if warper == None: warper = Warper(frame) # stop_counter.check_yellow_line(frame) warp_img = warper.warp(frame) stop_counter.check_crocss_walk(frame, warp_img) # cv2.imshow('frame', detect_img) if cv2.waitKey(0) == 27: break