def __init__( self, camero=None, ): """ Init camera and wheels""" logging.info('Creating a DeepPiCar...') #jimmy setup motor: chnnel 0 for front whell ,1 for( horizontal) ultra,2 for (vitical) camera #GPIO.setwarnings(False) #GPIO.setmode(GPIO.BOARD) #turn.ahead() picar.setup() #linsensorsetup() logging.debug('Set up camera') if camero == None: self.camera = cv2.VideoCapture(-1) else: self.camera = camero #self.camera.set(3, self.__SCREEN_WIDTH) #self.camera.set(4, self.__SCREEN_HEIGHT) #self.pan_servo = picar.Servo.Servo(1) #self.pan_servo.offset = -30 # calibrate servo to center #self.pan_servo.write(90) #self.tilt_servo = picar.Servo.Servo(2) #self.tilt_servo.offset = 20 # calibrate servo to center #self.tilt_servo.write(90) logging.debug('Set up back wheels') #self.back_wheels = picar.back_wheels.Back_Wheels() #self.back_wheels.speed = 30 # Speed Range is 0 (stop) - 100 (fastest) #self.back_wheels.forward() #self.back_wheels.backward() logging.debug('Set up front wheels') #self.front_wheels = picar.front_wheels.Front_Wheels() #self.front_wheels.turning_offset = -25 # calibrate servo to center #self.front_wheels.turn(90) # Steering Range is 45 (left) - 90 (center) - 135 (right) self.lane_follower = HandCodedLaneFollower(self) self.traffic_sign_processor = ObjectsOnRoadProcessor(self) # lane_follower = DeepLearningLaneFollower() self.fourcc = cv2.VideoWriter_fourcc(*'XVID') datestr = datetime.datetime.now().strftime("%y%m%d_%H%M%S") #self.video_orig = self.create_video_recorder('/home/pi/data/tmp/car_video%s.avi' % datestr) self.video_lane = self.create_video_recorder( '/home/pi/data/tmp/car_video_lane%s.avi' % datestr) #self.video_objs = self.create_video_recorder('/home/pi/data/tmp/car_video_objs%s.avi' % datestr) logging.info('Created a DeepPiCar')
def __init__(self): """ Init camera and wheels""" logging.info('Creating a DeepPiCar...') # picar.setup() logging.debug('Set up camera') self.camera = cv2.VideoCapture(-1) self.camera.set(3, self.__SCREEN_WIDTH) self.camera.set(4, self.__SCREEN_HEIGHT) # self.pan_servo = picar.Servo.Servo(1) # self.pan_servo.offset = -30 # calibrate servo to center # self.pan_servo.write(90) # self.tilt_servo = picar.Servo.Servo(2) # self.tilt_servo.offset = 20 # calibrate servo to center # self.tilt_servo.write(90) # logging.debug('Set up back wheels') #self.back_wheels = ''#picar.back_wheels.Back_Wheels() #self.back_wheels.speed = 0 # Speed Range is 0 (stop) - 100 (fastest) self.back_wheels = rover.Rover(debug=False) self.back_wheels.debug = True self.back_wheels.speed(60) # logging.debug('Set up front wheels') # self.front_wheels = picar.front_wheels.Front_Wheels() # self.front_wheels.turning_offset = -25 # calibrate servo to center # self.front_wheels.turn(90) # Steering Range is 45 (left) - 90 (center) - 135 (right) self.lane_follower = HandCodedLaneFollower(self) # self.traffic_sign_processor = ObjectsOnRoadProcessor(self) # lane_follower = DeepLearningLaneFollower() self.fourcc = cv2.VideoWriter_fourcc(*'XVID') datestr = datetime.datetime.now().strftime("%y%m%d_%H%M%S") self.video_orig = self.create_video_recorder( '../data/tmp/car_video%s.avi' % datestr) self.video_lane = self.create_video_recorder( '../data/tmp/car_video_lane%s.avi' % datestr) self.video_objs = self.create_video_recorder( '../data/tmp/car_video_objs%s.avi' % datestr) logging.info('Created a DeepPiCar')
def save_image_and_steering_angle(video_file): lane_follower = HandCodedLaneFollower() cap = cv2.VideoCapture(video_file + '.avi') try: i = 0 while cap.isOpened(): _, frame = cap.read() lane_follower.follow_lane(frame) cv2.imwrite( "%s_%03d_%03d.png" % (video_file, i, lane_follower.curr_steering_angle), frame) i += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cap.release() cv2.destroyAllWindows()
def test_video(video_file): end_to_end_lane_follower = EndToEndLaneFollower() hand_coded_lane_follower = HandCodedLaneFollower() cap = cv2.VideoCapture(video_file + '.avi') # skip first second of video. for i in range(3): _, frame = cap.read() video_type = cv2.VideoWriter_fourcc(*'XVID') video_overlay = cv2.VideoWriter("%s_end_to_end.avi" % video_file, video_type, 20.0, (320, 240)) try: i = 0 while cap.isOpened(): _, frame = cap.read() frame_copy = frame.copy() logging.info('Frame %s' % i) combo_image1 = hand_coded_lane_follower.follow_lane(frame) combo_image2 = end_to_end_lane_follower.follow_lane(frame_copy) diff = end_to_end_lane_follower.curr_steering_angle - hand_coded_lane_follower.curr_steering_angle logging.info("desired=%3d, model=%3d, diff=%3d" % (hand_coded_lane_follower.curr_steering_angle, end_to_end_lane_follower.curr_steering_angle, diff)) video_overlay.write(combo_image2) cv2.imshow("Hand Coded", combo_image1) cv2.imshow("Deep Learning", combo_image2) i += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cap.release() video_overlay.release() cv2.destroyAllWindows()
class DeepPiCar(object): __INITIAL_SPEED = 0 __SCREEN_WIDTH = 320 __SCREEN_HEIGHT = 240 def __init__(self): """ Init camera and wheels""" logging.info('Creating a DeepPiCar...') picar.setup() logging.debug('Set up camera') self.camera = cv2.VideoCapture(-1) self.camera.set(3, self.__SCREEN_WIDTH) self.camera.set(4, self.__SCREEN_HEIGHT) self.pan_servo = picar.Servo.Servo(1) self.pan_servo.offset = -30 # calibrate servo to center self.pan_servo.write(90) self.tilt_servo = picar.Servo.Servo(2) self.tilt_servo.offset = 20 # calibrate servo to center self.tilt_servo.write(90) logging.debug('Set up back wheels') self.back_wheels = picar.back_wheels.Back_Wheels() self.back_wheels.speed = 0 # Speed Range is 0 (stop) - 100 (fastest) logging.debug('Set up front wheels') self.front_wheels = picar.front_wheels.Front_Wheels() self.front_wheels.turning_offset = -25 # calibrate servo to center self.front_wheels.turn(90) # Steering Range is 45 (left) - 90 (center) - 135 (right) self.lane_follower = HandCodedLaneFollower(self) # self.traffic_sign_processor = ObjectsOnRoadProcessor(self) # lane_follower = DeepLearningLaneFollower() self.fourcc = cv2.VideoWriter_fourcc(*'XVID') datestr = datetime.datetime.now().strftime("%y%m%d_%H%M%S") self.video_orig = self.create_video_recorder('../data/tmp/car_video%s.avi' % datestr) self.video_lane = self.create_video_recorder('../data/tmp/car_video_lane%s.avi' % datestr) self.video_objs = self.create_video_recorder('../data/tmp/car_video_objs%s.avi' % datestr) logging.info('Created a DeepPiCar') def create_video_recorder(self, path): return cv2.VideoWriter(path, self.fourcc, 20.0, (self.__SCREEN_WIDTH, self.__SCREEN_HEIGHT)) def __enter__(self): """ Entering a with statement """ return self def __exit__(self, _type, value, traceback): """ Exit a with statement""" if traceback is not None: # Exception occurred: logging.error('Exiting with statement with exception %s' % traceback) self.cleanup() def cleanup(self): """ Reset the hardware""" logging.info('Stopping the car, resetting hardware.') self.back_wheels.speed = 0 self.front_wheels.turn(90) self.camera.release() self.video_orig.release() self.video_lane.release() self.video_objs.release() cv2.destroyAllWindows() def drive(self, speed=__INITIAL_SPEED): """ Main entry point of the car, and put it in drive mode Keyword arguments: speed -- speed of back wheel, range is 0 (stop) - 100 (fastest) """ logging.info('Starting to drive at speed %s...' % speed) self.back_wheels.speed = speed i = 0 while self.camera.isOpened(): _, image_lane = self.camera.read() image_lane = cv2.rotate(image_lane, cv2.ROTATE_180) image_objs = image_lane.copy() i += 1 self.video_orig.write(image_lane) # image_objs = self.process_objects_on_road(image_objs) # self.video_objs.write(image_objs) # show_image('Detected Objects', image_objs) image_lane = self.follow_lane(image_lane) self.video_lane.write(image_lane) show_image('Lane Lines', image_lane) if cv2.waitKey(1) & 0xFF == ord('q'): self.cleanup() break #def process_objects_on_road(self, image): # image = self.traffic_sign_processor.process_objects_on_road(image) # return image def follow_lane(self, image): image = self.lane_follower.follow_lane(image) return image
return cv2.VideoWriter(path, fourcc, 20.0, (SCREEN_WIDTH, SCREEN_HEIGHT)) def show_image(title, frame, show=_SHOW_IMAGE): if show: cv2.imshow(title, frame) INITIAL_SPEED = 0 SCREEN_WIDTH = 320 SCREEN_HEIGHT = 240 #Create all the objects #car = SDCar() lane_follower = HandCodedLaneFollower() #traffic_sign_processor = ObjectsOnRoadProcessor() fourcc = cv2.VideoWriter_fourcc(*'XVID') datestr = datetime.datetime.now().strftime("%y%m%d_%H%M%S") video_orig = create_video_recorder('../data/tmp/car_video%s.avi' % datestr) video_lane = create_video_recorder('../data/tmp/car_video_lane%s.avi' % datestr) video_objs = create_video_recorder('../data/tmp/car_video_objs%s.avi' % datestr) camera = cv2.VideoCapture(1) #camera.set(3, SCREEN_WIDTH) #amera.set(4, SCREEN_HEIGHT) i = 0 while camera.isOpened():