apollo icon indicating copy to clipboard operation
apollo copied to clipboard

How to feed images to lane detection module properly?

Open tandf opened this issue 5 years ago • 35 comments

I'm trying to feed images from the KAIST data set to the lane detection module.

My procedure is like this:

  1. Build a cyber record file. First, create a \tf message. Then, resize KAIST images from 1280x560 to 1920x1080, and create /apollo/sensor/camera/front_6mm/image messages. Pack them as a cyber record file.
  2. Play the record file while running the lane detection module. The lane detection module is run as indicated in this page

The lane detection module is running without reporting any error, and the detected lanes match the lanes in the picture very well. But in the BEV, the detected lanes are not parallel while they should be (see the picture below). My guess is that the camera location of the KAIST data set is not the same as Apollo's default camera location, but I couldn't find a way to correct it.

So my question is, how should I properly feed images to the lane detection module so that it can yield parallel lanes? Thanks a lot.

screenshot of lane detection module In the right part of the screenshot, the detected lanes are not parallel.

tandf avatar Apr 23 '21 05:04 tandf

You need to change extrinsics and intrinsics in modules/perception/data/params

qwetqwe avatar Apr 28 '21 08:04 qwetqwe

You need to change extrinsics and intrinsics in modules/perception/data/params

Thank you for your reply! I use the correct camera height but it helps just a little. But later I figure out how to change the pitch of the camera. After some trial and error I find the correct rotation params and the results seem right.

tandf avatar Apr 29 '21 14:04 tandf

@tandf Hello, Could you give more details on how to generate the cyber record based on the benckmark datasets such as KAIST image dataset? I can't find the relevant reference and have been troubled by this problem for a long time. Thank you in advance!

ideasplus avatar Jul 31 '21 18:07 ideasplus

@ideasplus First I used cyber rt's Python API to write cyber messages based on KAIST image, see this link

At the same time, you can record Apollo's output to record files for future use. You can use cyber-recorder or write a script to do this.

Note that KAIST images are not in RGB format and you may need to read them like this

    byr = cv2.imread(input_file, cv2.IMREAD_ANYDEPTH)
    rgb = cv2.cvtColor(byr, cv2.COLOR_BayerRG2RGB)

I make a demo from my script. Note that this is just an example and it's not guaranteed to work. You need to change it according to your situation

from cyber_py import cyber
from cyber_py import record
from modules.drivers.proto.sensor_image_pb2 import Image
from modules.transform.proto.transform_pb2 import TransformStampeds
import cv2

def read_kaist_stereo(input_file):
    byr = cv2.imread(input_file, cv2.IMREAD_ANYDEPTH)
    #  rgb = cv2.cvtColor(byr, cv2.COLOR_BayerRG2RGB)
    # You may need to use 48 instead of cv.COLOR_BayerRG2RGB with older version
    # of opencv
    rgb = cv2.cvtColor(byr, 48)
    resized = cv2.resize(rgb, (1920, 1080))
    return resized

cyber.init()

# Prepare cyber writers
image_node = cyber.Node("image_writer_node")
image_writer = image_node.create_writer(
        "/apollo/sensor/camera/front_6mm/image", Image)
tf_writer = image_node.create_writer( "/tf", TransformStampeds)

# Read image file
image_file = read_kaist_stereo(image_file_path)
videoHeight, videoWidth, _ = image_file.shape

image_msg = Image()

# Set up header
image_msg.header.sequence_num = sequence_num
image_msg.header.lidar_timestamp = lidar_timestamp
image_msg.header.radar_timestamp = radar_timestamp
image_msg.header.version = version
image_msg.header.frame_id = frame_id

# Frame_id
image_msg.frame_id = frame_id
            
# Image metadata
image_msg.height = videoHeight
image_msg.width = videoWidth
image_msg.encoding = "rgb8"
image_msg.step = videoWidth * 3

# Image data
image_msg.data = image_file.tobytes()

# Write image and tf cyber message
image_msg.header.camera_timestamp = camera_timestamp
image_msg.header.timestamp_sec = timestamp_sec
image_msg.measurement_time = measurement_time
image_writer.write(image_msg)

# Also make a tf message
tf_msg = TransformStampeds()
tf_msg.transforms.add()
tf_msg.transforms[0].header.frame_id = 'world'
tf_msg.transforms[0].child_frame_id = 'localization'
tf_msg.transforms[0].transform.translation.x = 0
tf_msg.transforms[0].transform.translation.y = 0
tf_msg.transforms[0].transform.translation.z = 0
tf_msg.transforms[0].transform.rotation.qx = -0.5
tf_msg.transforms[0].transform.rotation.qy = 0.5
tf_msg.transforms[0].transform.rotation.qz = -0.5
tf_msg.transforms[0].transform.rotation.qw = 0.5
tf_msg.transforms[0].header.timestamp_sec = timestamp
tf_writer.write(tf_msg)

cyber.shutdown()

tandf avatar Aug 01 '21 02:08 tandf

@ideasplus First I used cyber rt's Python API to write cyber messages based on KAIST image, see this link

At the same time, you can record Apollo's output to record files for future use. You can use cyber-recorder or write a script to do this.

Note that KAIST images are not in RGB format and you may need to read them like this

    byr = cv2.imread(input_file, cv2.IMREAD_ANYDEPTH)
    rgb = cv2.cvtColor(byr, cv2.COLOR_BayerRG2RGB)

I make a demo from my script. Note that this is just an example and it's not guaranteed to work. You need to change it according to your situation

from cyber_py import cyber
from cyber_py import record
from modules.drivers.proto.sensor_image_pb2 import Image
import cv2

def read_kaist_stereo(input_file):
    byr = cv2.imread(input_file, cv2.IMREAD_ANYDEPTH)
    #  rgb = cv2.cvtColor(byr, cv2.COLOR_BayerRG2RGB)
    # You may need to use 48 instead of cv.COLOR_BayerRG2RGB with older version
    # of opencv
    rgb = cv2.cvtColor(byr, 48)
    resized = cv2.resize(rgb, (1920, 1080))
    return resized

cyber.init()

# Prepare cyber writers
image_node = cyber.Node("image_writer_node")
image_writer = image_node.create_writer(
        "/apollo/sensor/camera/front_6mm/image", Image)
tf_writer = image_node.create_writer( "/tf", TransformStampeds)

# Read image file
image_file = read_kaist_stereo(image_file_path)
videoHeight, videoWidth, _ = image_file.shape

image_msg = Image()

# Set up header
image_msg.header.sequence_num = sequence_num
image_msg.header.lidar_timestamp = lidar_timestamp
image_msg.header.radar_timestamp = radar_timestamp
image_msg.header.version = version
image_msg.header.frame_id = frame_id

# Frame_id
image_msg.frame_id = frame_id
            
# Image metadata
image_msg.height = videoHeight
image_msg.width = videoWidth
image_msg.encoding = "rgb8"
image_msg.step = videoWidth * 3

# Image data
image_msg.data = image_file.tobytes()

# Write image and tf cyber message
image_msg.header.camera_timestamp = camera_timestamp
image_msg.header.timestamp_sec = timestamp_sec
image_msg.measurement_time = measurement_time
image_writer.write(image_msg)

# Also make a tf message
tf_msg = TransformStampeds()
tf_msg.transforms.add()
tf_msg.transforms[0].header.frame_id = 'world'
tf_msg.transforms[0].child_frame_id = 'localization'
tf_msg.transforms[0].transform.translation.x = 0
tf_msg.transforms[0].transform.translation.y = 0
tf_msg.transforms[0].transform.translation.z = 0
tf_msg.transforms[0].transform.rotation.qx = -0.5
tf_msg.transforms[0].transform.rotation.qy = 0.5

tf_msg.transforms[0].transform.rotation.qz = -0.5

tf_msg.transforms[0].transform.rotation.qw = 0.5 tf_msg.transforms[0].header.timestamp_sec = timestamp tf_writer.write(tf_msg)

cyber.shutdown()

Thank you for your generous help and detailed answers. It's really helpful! I will try to get it work based on your scripts.

ideasplus avatar Aug 01 '21 09:08 ideasplus

Hi @tandf

I'm a beginner in Apollo, and I am working in a project to test the perception module, in this case lane detection. So I would like to know what I have to do, to have the same result has yours. My main doubt is the code and how to run this code.

I appreciate if you can give me a lite help

rzr900ipl avatar Aug 23 '21 09:08 rzr900ipl

Hi @rzr900ipl You can learn the basics of Apollo and cyber first. After that, you should be able to understand and run the code. If you have any questions, please be specific so that I can help.

tandf avatar Aug 23 '21 12:08 tandf

Hi @tandf Thank you for the advice, I will do it. But just a question, where and how you call the image or video file on your code?

rzr900ipl avatar Aug 23 '21 14:08 rzr900ipl

@rzr900ipl I read each video frame and send them as cyber messages to the topic Apollo requires, and the perception module handles the rest. You can do this using the script I provided above.

tandf avatar Aug 23 '21 14:08 tandf

@tandf First thank you for you fast answer, I appreciate that very must Second, is possible put the perception module lane detection in a simple image of a Road? What I have to replace in your code? Third, if /apollo/sensor/camera/front_6mm/image already exists, I only need to create the /tf massages to see the lane detection? I obtain the image and the topic from the SVL Simulator.

Sorry for the amount of questions, but I need to understand this the fast that a can. Thank you again

rzr900ipl avatar Aug 23 '21 15:08 rzr900ipl

Second, is possible put the perception module lane detection in a simple image of a Road? What I have to replace in your code?

Do you mean you want to run the perception module on just one image? I think you should first run the perception module, then send the cyber message containing the image with some necessary tf messages, and the perception module should output the result.

Third, if /apollo/sensor/camera/front_6mm/image already exists, I only need to create the /tf massages to see the lane detection? I obtain the image and the topic from the SVL Simulator.

Actually, I think the svl simulator will produce everything necessary for the module. When I first tried to run the perception module, I ran Apollo 2.5 perception module as told in this doc and connect it with the svl simulator. I remember I didn't modify anything except enabling visualization and it worked fine. I think this also applies to later versions of Apollo with dreamviewer.

tandf avatar Aug 23 '21 15:08 tandf

Do you mean you want to run the perception module on just one image? I think you should first run the perception module, then send the cyber message containing the image with some necessary tf messages, and the perception module should output the result.

Yes. I say that because I don't working with KAIST, and I don't understand where you call the video in the code. The Apollo have a test images, and I would like to work with them. I don't know if apollo use them for test the componentes, or if is for the developers test de componentes

Actually, I think the svl simulator will produce everything necessary for the module. When I first tried to run the perception module, I ran Apollo 2.5 perception module as told in this doc and connect it with the svl simulator. I remember I didn't modify anything except enabling visualization and it worked fine. I think this also applies to later versions of Apollo with dreamviewer.

I already do it, but it don't work. I have create this issue #13976.

rzr900ipl avatar Aug 23 '21 17:08 rzr900ipl

Yes. I say that because I don't working with KAIST, and I don't understand where you call the video in the code. The Apollo have a test images, and I would like to work with them. I don't know if apollo use them for test the componentes, or if is for the developers test de components

@rzr900ipl I don't know if Apollo provides any test program for these images. If not, you can try my code above. The image is read like this:

# Read image file
image_file = read_kaist_stereo(image_file_path)
videoHeight, videoWidth, _ = image_file.shape

image_msg = Image()

# ...

image_msg.height = videoHeight
image_msg.width = videoWidth
image_msg.encoding = "rgb8"
image_msg.step = videoWidth * 3

# Image data
image_msg.data = image_file.tobytes()

You may need to change the read_kaist_stereo() function according to the image format

tandf avatar Aug 24 '21 01:08 tandf

@tandf But in the read_kaist_stereo() function, how do i call de image in the folder I told you above?

And for use this code need a spesific version of Apollo?

rzr900ipl avatar Aug 24 '21 10:08 rzr900ipl

Something like this

def read_your_image(input_file_path):
    img = cv2.imread(input_file_path)
    resized = cv2.resize(img, (1920, 1080))
    return resized

Specify the image path like

read_your_image("/apollo/modules/perception/testdata/camera/app/lane_images/1525854441139346413.jpg")

I don't think this needs a specific version of Apollo. It simply makes cyber messages from some images and sends them to Apollo. But there are indeed some differences between these versions when using cyber, like python2 or 3...

tandf avatar Aug 24 '21 13:08 tandf

@tandf I tried to run the code, but I got a erro.

@in_5_0_dev_docker:/apollo$ python project/project.py WARNING: Logging before InitGoogleLogging() is written to STDERR I0824 23:24:03.164288 13386 cyber_record_wrap.cc:562] [] init _cyber_record I0824 23:24:03.224678 13386 global_data.cc:150] [cyber_py] host ip: 192.168.11.106 Traceback (most recent call last): File "project/project.py", line 17, in <module> tf_writer = image_node.create_writer( "/tf", TransformStampeds) NameError: name 'TransformStampeds' is not defined

I have create a python folder, and put the code you provide with the changes for the image. Can you help me. Thank you

rzr900ipl avatar Aug 24 '21 22:08 rzr900ipl

@rzr900ipl Sorry there was a mistake in my script. Please add

from modules.transform.proto.transform_pb2 import TransformStampeds

tandf avatar Aug 26 '21 03:08 tandf

@tandf I did what you said, and now when I run it I got this erro @in_5_0_dev_docker:/apollo$ python project.py File "project.py", line 23 image_file = read_your_image(/apollo-5.0/modules/perception/testdata/...) .............................^ SyntaxError: invalid syntax

And when I remove the '/' behind the apollo-5.0, I got @in_5_0_dev_docker:/apollo$ python project.py File "project.py", line 23 image_file = read_your_image(apollo-5.0/modules/perception/testdata/camera/app/lane_images/1525854441139346413.jpg) ............................................................................^ SyntaxError: invalid syntax

And when I remove 'jpg', i got @in_5_0_dev_docker:/apollo$ python project.py WARNING: Logging before InitGoogleLogging() is written to STDERR I0826 12:06:03.125723 30230 cyber_record_wrap.cc:562] [] init _cyber_record I0826 12:06:03.223012 30230 global_data.cc:150] [cyber_py] host ip: 192.168.11.106 Traceback (most recent call last): File "project.py", line 23 image_file = read_your_image(apollo-5.0/modules/perception/testdata/camera/app/lane_images/1525854441139346413) NameError: name 'apollo' is not defined

Can you help me? Thank you for all

rzr900ipl avatar Aug 26 '21 11:08 rzr900ipl

@rzr900ipl Sorry I forgot quotation marks here. Use

read_your_image("/apollo/modules/perception/testdata/camera/app/lane_images/1525854441139346413.jpg")

tandf avatar Aug 26 '21 11:08 tandf

@tandf Thank you for you answer, it works. But now I have another erro @in_5_0_dev_docker:/apollo$ python project.py WARNING: Logging before InitGoogleLogging() is written to STDERR I0826 19:53:57.867893 4115 cyber_record_wrap.cc:562] [] init _cyber_record I0826 19:53:57.931190 4115 global_data.cc:150] [cyber_py] host ip: 192.168.11.106 Traceback (most recent call last): File "project.py", line 34, in <module> image_msg.data = image_file.tobytes() AttributeError: 'numpy.ndarray' object has no attribute 'tobytes'

When i put this line as a comment, shows another erro @in_5_0_dev_docker:/apollo$ python project.py WARNING: Logging before InitGoogleLogging() is written to STDERR I0826 19:56:25.942515 4204 cyber_record_wrap.cc:562] [] init _cyber_record I0826 19:56:26.005947 4204 global_data.cc:150] [cyber_py] host ip: 192.168.11.106 Traceback (most recent call last): File "project.py", line 37, in <module> image_msg.header.camera_timestamp = camera_timestamp NameError: name 'camera_timestamp' is not defined

Can you help me? And thanks for all again

rzr900ipl avatar Aug 26 '21 19:08 rzr900ipl