Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instance detection ros image #272

Closed
wants to merge 2 commits into from
Closed

Conversation

RobinSchmid7
Copy link
Collaborator

@RobinSchmid7 RobinSchmid7 commented Aug 16, 2023

The instance() method does not work (on my system) with the ros_img.
Replaced by inbuilt type + .__name__ method which works.

Output of

def ros_image_to_torch(ros_img, desired_encoding="rgb8", device="cpu"):
if type(ros_img).__name__ is "_sensor_msgs__Image":
np_image = CV_BRIDGE.imgmsg_to_cv2(ros_img, desired_encoding=desired_encoding)
elif type(ros_img).__name__ is "_sensor_msgs__CompressedImage":
np_arr = np.fromstring(ros_img.data, np.uint8)
np_image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if "bgr" in ros_img.format:
np_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
return TO_TENSOR(np_image).to(device)
for ros_img :

print(isinstance(ros_img, Image)): False
print(isinstance(ros_img, CompressedImage)): False
print(type(ros_img).__name__ is "_sensor_msgs__Image"): True

@mmattamala
Copy link
Collaborator

isinstance should be fairly standard. Can you also try to compare the type of ros_msg with the type of Image for instance?

@RobinSchmid7
Copy link
Collaborator Author

RobinSchmid7 commented Aug 16, 2023

Yes, it seems to be a type difference:

a = Image()
print(type(a)): <class 'sensor_msgs.msg._Image.Image'>
print(type(ros_msg): <class 'tmpjz2xjmha._sensor_msgs__Image'>

So the tmpjz... refers to a unique identifier which changes each time I run the code. The actual python class for the sensor_msgs seems to be dynamically allocated. Maybe we can we fix this dynamic allocation?

@RobinSchmid7 RobinSchmid7 self-assigned this Aug 16, 2023
@RobinSchmid7
Copy link
Collaborator Author

RobinSchmid7 commented Aug 23, 2023

Problem resolved: the problem only occurs if bags are replayed using the BagTfTransformer package from python not when replaying via a terminal. This generates a dynamically allocated python class for the sensor_msgs topics which is not recognized with the isinstance() method. Since the ros_image_to_torch() method is used both during deployment and offline data generation I added both options so its compatible.

@JonasFrey96 JonasFrey96 deleted the fix/ros_img_conversion branch August 29, 2023 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants