def process_log(self, bag_in, prefix: str, bag_out, prefix_out: str, utils: ProcessorUtilsInterface): algo_db = get_easy_algo_db() line_detector = algo_db.create_instance(FAMILY_LINE_DETECTOR, self.line_detector) image_prep = algo_db.create_instance("image_prep", self.image_prep) vehicle = dbu.which_robot(bag_in) topic = f"/{vehicle}/camera_node/image/compressed" context = FakeContext() transform = None frame = 0 for compressed_img_msg in dbu.d8n_bag_read_with_progress(bag_in, topic): with context.phase("decoding"): try: image_cv = dtu.bgr_from_jpg(compressed_img_msg.data) except ValueError as e: msg = f"Could not decode image: {e}" dtu.raise_wrapped(ValueError, e, msg) segment_list = image_prep.process(context, image_cv, line_detector, transform) rendered = vs_fancy_display(image_prep.image_cv, segment_list) rendered = dtu.d8_image_zoom_linear(rendered, 2) log_name = "log_name" time = 12 rendered = dtu.add_duckietown_header(rendered, log_name, time, frame) out = dru.d8n_image_msg_from_cv_image(rendered, "bgr8", same_timestamp_as=compressed_img_msg) # Write to the bag bag_out.write("processed", out) # out = d8n_image_msg_from_cv_image(image_cv, "bgr8", same_timestamp_as=compressed_img_msg) bag_out.write("image", compressed_img_msg) frame += 1
def get_robot_camera_geometry_from_log( brp: dru.BagReadProxy) -> RobotCameraGeometry: robot_name = dru.which_robot(brp) K = get_homography_for_robot(robot_name) ci = dru.read_camera_info_from_bag(brp) rectifier = Rectify(ci) gpg = GroundProjectionGeometry(ci.width, ci.height, K) return RobotCameraGeometry(rectifier, gpg)
def look_at( log, output: str, anti_instagram: str, line_detector: str, image_prep: str, lane_filter: str, all_details: bool, ) -> None: filename = get_local_bag_file(log) bag = rosbag.Bag(filename) vehicle_name = dbu.which_robot(bag) dtu.logger.info(f"Vehicle name: {vehicle_name}") brp = dbu.BagReadProxy(bag) rcg = get_robot_camera_geometry_from_log(brp) topic = dbu.get_image_topic(bag) res = dbu.d8n_read_all_images_from_bag(bag, topic, max_images=1) image_cv = res[0]["rgb"] # dtu.logger.debug(dtu.describe_value(image_cv)) image_cv_bgr = dtu.bgr_from_rgb(image_cv) dtu.DuckietownConstants.show_timeit_benchmarks = True res, _stats = run_pipeline( image_cv_bgr, gpg=rcg.gpg, rectifier=rcg.rectifier, anti_instagram_name=anti_instagram, line_detector_name=line_detector, image_prep_name=image_prep, lane_filter_name=lane_filter, all_details=all_details, ) res = dtu.resize_small_images(res) dtu.write_bgr_images_as_jpgs(res, output)
def process_log( self, bag_in: dbu.BagReadProxy, prefix_in: str, bag_out, prefix_out: str, utils: ProcessorUtilsInterface, ): log_name = utils.get_log().log_name vehicle_name = dbu.which_robot(bag_in) logger.info(f"Vehicle name: {vehicle_name}") topic = dbu.get_image_topic(bag_in) rcg = get_robot_camera_geometry_from_log(bag_in) bgcolor = dtu.ColorConstants.BGR_DUCKIETOWN_YELLOW sequence = bag_in.read_messages_plus(topics=[topic]) for _i, mp in enumerate(sequence): bgr = dtu.bgr_from_rgb(dru.rgb_from_ros(mp.msg)) res, stats = run_pipeline( bgr, gpg=rcg.gpg, rectifier=rcg.rectifier, line_detector_name=self.line_detector, image_prep_name=self.image_prep, lane_filter_name=self.lane_filter, anti_instagram_name=self.anti_instagram, all_details=self.all_details, ) rect = (480, 640) if not self.all_details else (240, 320) res = dtu.resize_images_to_fit_in_rect(res, rect, bgcolor=bgcolor) msg = ( f"abs: {mp.time_absolute} window: {mp.time_window} from log: " f"{mp.time_from_physical_log_start}") dtu.logger.info(msg) headers = [ f"Robot: {vehicle_name} log: {log_name} time: {mp.time_from_physical_log_start:.2f} s", f"Algorithms | color correction: {self.anti_instagram} | preparation: {self.image_prep} | " f"detector: {self.line_detector} | filter: {self.lane_filter}", ] res = dtu.write_bgr_images_as_jpgs(res, dirname=None, bgcolor=bgcolor) cv_image = res["all"] for head in reversed(headers): max_height = 35 cv_image = dtu.add_header_to_bgr(cv_image, head, max_height=max_height) otopic = "all" omsg = dru.d8_compressed_image_from_cv_image( cv_image, same_timestamp_as=mp.msg) t = rospy.Time.from_sec(mp.time_absolute) dtu.logger.info(f"written {otopic!r} at t = {t.to_sec()}") bag_out.write(prefix_out + "/" + otopic, omsg, t=t) for name, value in list(stats.items()): utils.write_stat(prefix_out + "/" + name, value, t=t)