Esempio n. 1
0
def jobs_videos(context, log, name, outd, only_camera):
    filename = get_local_bag_file(log)

    bag = rosbag.Bag(filename)
    main_camera_topic = dbu.get_image_topic(bag)
    min_messages = 5  # need at least 5 frames to make a video

    topics = [
        _ for _, __ in dbu.d8n_get_all_images_topic_bag(
            bag, min_messages=min_messages)
    ]
    bag.close()

    only_camera_fn = outd + "-video.mp4"

    for topic in topics:
        stop_at = min_messages + 2
        actual_count, count, _stopped_early = dbu.count_messages_in_slice(
            filename, topic, log.t0, log.t1, stop_at=stop_at)

        assert count >= min_messages
        if actual_count < min_messages:
            msg = f"There are only {actual_count} (out of {count}) in the slice [{log.t0}, {log.t1}]"
            msg += f"\n topic: {topic}"
            continue

        d = topic.replace("/", "_")
        if d.startswith("_"):
            d = d[1:]

        if only_camera:
            if topic != main_camera_topic:
                continue
            out = only_camera_fn
            j = context.comp(
                dbu.d8n_make_video_from_bag,
                filename,
                topic,
                out,
                t0=log.t0,
                t1=log.t1,
                job_id=f"{name}-{topic}",
            )

        else:
            out = os.path.join(outd, name + "-" + d + ".mp4")
            j = context.comp(dbu.d8n_make_video_from_bag,
                             filename,
                             topic,
                             out,
                             job_id=f"{name}-{topic}")

            # create link
            if topic == main_camera_topic:
                context.comp(link, j, out, only_camera_fn)
Esempio n. 2
0
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)
Esempio n. 3
0
def work(log, outd, max_images, only_camera, write_frames):
    filename = get_local_bag_file(log)
    t0 = log.t0
    t1 = log.t1

    MIN_HEIGHT = 480

    # noinspection PyUnboundLocalVariable
    bag = rosbag.Bag(filename)

    main = dbu.get_image_topic(bag)

    topics = [_ for _, __ in dbu.d8n_get_all_images_topic_bag(bag)]
    bag.close()
    dtu.logger.debug(f"{filename} - topics: {topics}")
    for topic in topics:

        if only_camera and topic != main:
            continue

        try:
            bag = rosbag.Bag(filename)
        except:
            msg = f"Cannot read Bag file {filename}"
            dtu.logger.error(msg)
            raise
        bag_proxy = dbu.BagReadProxy(bag, t0, t1)
        res = dbu.d8n_read_all_images_from_bag(bag_proxy,
                                               topic,
                                               max_images=max_images,
                                               use_relative_time=True)
        bag.close()

        d = topic.replace("/", "_")
        if d.startswith("_"):
            d = d[1:]

        d0 = os.path.join(outd, d)

        images_with_label = []
        for i in range(len(res)):
            rgb = res[i]["rgb"]

            H, _W = rgb.shape[:2]
            if H < MIN_HEIGHT:
                zoom = int(np.ceil(MIN_HEIGHT / H))
                rgb = dtu.zoom_image(rgb, zoom)

            timestamp = res[i]["timestamp"]
            s = f"t = {timestamp - t0:.2f}"
            with_label = dtu.add_header_to_rgb(rgb, s)
            images_with_label.append(with_label)

        if write_frames:
            for i, rgb in enumerate(images_with_label):
                rgb = res[i]["rgb"]
                fn = os.path.join(d0, f"image-{i:05d}" + ".jpg")
                dtu.write_bgr_as_jpg(dtu.bgr_from_rgb(rgb), fn)

        grid = dtu.make_images_grid(
            images_with_label,
            pad=4,
            bgcolor=dtu.ColorConstants.RGB_DUCKIETOWN_YELLOW)
        s = log.log_name
        grid = dtu.add_header_to_rgb(grid, s, max_height=32)

        if (topic != main) or len(topics) > 1:
            fn = d0 + ".jpg"
            dtu.write_rgb_as_jpg(grid, fn)

        if topic == main:
            fn = outd + ".thumbnails.jpg"
            dtu.write_rgb_as_jpg(grid, fn)
    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)