Esempio n. 1
0
def single_image_histograms():
    p = dtu.require_resource("frame0002.jpg")

    image_cv = dtu.image_cv_from_jpg_fn(p)

    res = go(image_cv)
    outd = dtu.get_output_dir_for_test()
    dtu.write_bgr_images_as_jpgs(res, outd)
Esempio n. 2
0
    def go(self):
        vehicle_name = dtu.get_current_robot_name()

        # noinspection PyUnresolvedReferences
        output = self.options.output
        if output is None:
            output = "out/pipeline"  # + dtu.get_md5(self.options.image)[:6]
            self.info(f"No --output given, using {output}")

        # noinspection PyUnresolvedReferences
        opt_image = self.options.image
        if opt_image is not None:
            image_filename = opt_image
            if image_filename.startswith("http"):
                image_filename = dtu.get_file_from_url(image_filename)

            bgr = dtu.bgr_from_jpg_fn(image_filename)
        else:
            self.info("Validating using the ROS image stream...")
            import rospy
            from sensor_msgs.msg import CompressedImage

            topic_name = os.path.join("/", vehicle_name,
                                      "camera_node/image/compressed")

            self.info("Let's wait for an image. Say cheese!")

            # Dummy to get ROS message
            rospy.init_node("single_image")

            try:
                img_msg = cast(
                    CompressedImage,
                    rospy.wait_for_message(topic_name,
                                           CompressedImage,
                                           timeout=10))
                self.info("Image captured")
            except rospy.ROSException as e:
                self.info(
                    f"\n\n\nDidn't get any message: {e}\n MAKE SURE YOU USE DT SHELL COMMANDS OF VERSION "
                    "4.1.9 OR HIGHER!\n\n\n")
                raise

            bgr = dtu.bgr_from_rgb(dru.rgb_from_ros(img_msg))
            self.info(f"Picture taken: {str(bgr.shape)} ")

        dtu.DuckietownConstants.show_timeit_benchmarks = True
        res, _stats = run_pipeline(bgr)

        self.info("Resizing images..")
        res = dtu.resize_small_images(res)
        self.info("Writing images..")
        dtu.write_bgr_images_as_jpgs(res, output)
Esempio n. 3
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. 4
0
def create_visuals(robots: List[str], actual_map_name: str, out: str):
    db = get_easy_algo_db()
    actual_map = db.create_instance(FAMILY_SEGMAPS, actual_map_name)
    res = {}
    res2 = {}

    for i, robot_name in enumerate(sorted(robots)):
        logger.info(f"{i}/{len(robots)}: {robot_name}")
        rcg = get_robot_camera_geometry(robot_name)

        pose = np.eye(3)
        simulated_data = simulate_image(actual_map, pose, gpg=rcg.gpg, rectifier=rcg.rectifier, blur_sigma=1)
        res[robot_name] = simulated_data.rectified_synthetic_bgr
        res2[robot_name] = simulated_data.distorted_synthetic_bgr
    if not res:
        msg = "No images to draw."
        dtu.logger.error(msg)
    else:
        output = os.path.join(out, "distorted")
        dtu.write_bgr_images_as_jpgs(res2, output)
        output = os.path.join(out, "rectified")
        dtu.write_bgr_images_as_jpgs(res, output)
Esempio n. 5
0
    def go(self):
        robot_name = dtu.get_current_robot_name()

        # noinspection PyUnresolvedReferences
        output = self.options.output
        # noinspection PyUnresolvedReferences
        the_input = self.options.input
        if output is None:
            output = "out/calibrate-extrinsics"  # + dtu.get_md5(self.options.image)[:6]
            self.info(f"No --output given, using {output}")

        if the_input is None:

            self.info(
                ("{}\nCalibrating using the ROS image stream...\n".format("*" *
                                                                          20)))
            import rospy
            from sensor_msgs.msg import CompressedImage

            topic_name = os.path.join("/", robot_name,
                                      "camera_node/image/compressed")
            logger.info(f"Topic to listen to is: {topic_name}")

            self.info("Let's wait for an image. Say cheese!")

            # Dummy for getting a ROS message
            rospy.init_node("calibrate_extrinsics")

            try:
                img_msg = rospy.wait_for_message(topic_name,
                                                 CompressedImage,
                                                 timeout=10)
                self.info("Image captured")
            except rospy.ROSException as e:
                print((
                    "\n\n\n"
                    f"Didn't get any message: {e}\n "
                    "MAKE SURE YOU USE DT SHELL COMMANDS OF VERSION 4.1.9 OR HIGHER."
                ))
                raise
            img_msg = cast(CompressedImage, img_msg)
            bgr = dtu.bgr_from_rgb(dru.rgb_from_ros(img_msg))
            self.info(f"Picture taken: {str(bgr.shape)} ")

        else:
            self.info(f"Loading input image {the_input}")
            bgr = dtu.bgr_from_jpg_fn(the_input)

        if bgr.shape[1] != 640:
            interpolation = cv2.INTER_CUBIC
            bgr = dtu.d8_image_resize_fit(bgr, 640, interpolation)
            self.info(f"Resized to: {str(bgr.shape)} ")
        # Disable the old calibration file
        self.info("Disableing old homography")
        disable_old_homography(robot_name)
        self.info("Obtaining camera info")
        try:
            camera_info = get_camera_info_for_robot(robot_name)
        except Exception as e:
            msg = "Error on obtaining camera info."
            raise Exception(msg) from e

        self.info("Get default homography")
        try:
            homography_dummy = get_homography_default()
        except Exception as e:
            msg = "Error on getting homography."
            raise Exception(msg) from e

        self.info("Rectify image")
        try:
            rect = Rectify(camera_info)
        except Exception as e:
            msg = "Error rectifying image."
            raise Exception(msg) from e

        self.info("Calculate GPG")
        try:
            gpg = GroundProjectionGeometry(camera_info.width,
                                           camera_info.height,
                                           homography_dummy.reshape((3, 3)))
        except Exception as e:
            msg = "Error calculating GroundProjectionGeometry."
            raise Exception(msg) from e
        self.info("Ordered Dict")
        res = {}
        try:
            bgr_rectified = rect.rectify(bgr, interpolation=cv2.INTER_CUBIC)

            res["bgr"] = bgr
            res["bgr_rectified"] = bgr_rectified

            _new_matrix, res["rectified_full_ratio_auto"] = rect.rectify_full(
                bgr, ratio=1.65)

            (result_gpg, status) = gpg.estimate_homography(bgr_rectified)

            if status is not None:
                raise Exception(status)

            save_homography(result_gpg.H, robot_name)
            msg = """

To check that this worked well, place the robot on the road, and run:

    rosrun complete_image_pipeline single_image

Look at the produced jpgs.

"""
            self.info(msg)
        except Exception as E:
            self.error(E)
        finally:
            dtu.write_bgr_images_as_jpgs(res, output)
Esempio n. 6
0
def test_synthetic(
    actual_map_name: str,
    template,
    robot_name: str,
    line_detector_name: str,
    image_prep_name: str,
    lane_filter_name: str,
    pose_or_location,
    outd: str,
    seed: int = 42,
) -> Tuple[object, object]:
    np.random.seed(seed)
    db = get_easy_algo_db()
    actual_map = db.create_instance(FAMILY_SEGMAPS, actual_map_name)

    # first, load calibration for robot
    easy_algo_db = get_easy_algo_db()
    dtu.logger.debug(f"looking for localization template {template!r}")
    localization_template = easy_algo_db.create_instance(
        FAMILY_LOC_TEMPLATES, template)

    rcg = get_robot_camera_geometry(robot_name)

    if pose_or_location.shape == (3, 3):  # SE(2)
        pose = pose_or_location
        location = localization_template.coords_from_pose(pose)
    else:
        location = pose_or_location
        pose = localization_template.pose_from_coords(location)

    simulation_data = simulate_image(actual_map,
                                     pose,
                                     gpg=rcg.gpg,
                                     rectifier=rcg.rectifier,
                                     blur_sigma=0.3)

    image = simulation_data.distorted_synthetic_bgr

    #     anti_instagram_name='identity' # skip
    anti_instagram_name = "baseline"

    all_details = False
    res, stats = run_pipeline(
        image,
        gpg=rcg.gpg,
        rectifier=rcg.rectifier,
        line_detector_name=line_detector_name,
        image_prep_name=image_prep_name,
        lane_filter_name=lane_filter_name,
        anti_instagram_name=anti_instagram_name,
        all_details=all_details,
        ground_truth=pose,
        actual_map=actual_map,
    )

    error = np.empty_like(location)
    for k in error.dtype.fields:
        error[k] = stats["estimate"][k] - location[k]
    stats["error"] = error

    res = dtu.resize_small_images(res)

    dtu.write_bgr_images_as_jpgs(res, outd, extra_string=outd.split("/")[-1])
    return res, stats
    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)