Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

error in image callback: 'dict' object has no attribute 'name' #276

@haniraid

Description

@haniraid

Hi evrybody. so im trying to creat a node and run Zoedepth using robomaster s1 monocular camera. the robot is connected and evrything is set and the script is like below, notice that im using torch==2.4.1 ubuntu 20.04 timm==0.6.13 but running the script im getting this error
self.zoe.load_state_dict(torch.load(weights_path, map_location=self.DEVICE), strict=False)
[INFO] [1726359598.098118317] [depth_estimation_node]: Weights loaded from /home/raidhani/ros2_ws/src/MiDaS/weights/dpt_large_384.pt
[INFO] [1726359598.105714726] [depth_estimation_node]: Received image with dimensions: (360, 640, 3)
[INFO] [1726359598.108058095] [depth_estimation_node]: Resized image to: (384, 512, 3)
[ERROR] [1726359603.679989582] [depth_estimation_node]: Error in image callback: 'dict' object has no attribute 'name'
[INFO] [1726359603.681861777] [depth_estimation_node]: Received image with dimensions: (360, 640, 3)
[INFO] [1726359603.683620926] [depth_estimation_node]: Resized image to: (384, 512, 3)

script used:
#!/usr/bin/env python3

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import torch
from PIL import Image as PilImage
import json
from zoedepth.utils.misc import colorize
from zoedepth.models.zoedepth_nk.zoedepth_nk_v1 import ZoeDepthNK

class DepthEstimationNode(Node):
def init(self):
super().init('depth_estimation_node')

# Set up a subscriber to the camera topic
self.subscription = self.create_subscription(
    Image,
    '/camera/image_color',
    self.image_callback,
    10)
self.subscription

# Create a cv_bridge object for converting ROS images to OpenCV format
self.bridge = CvBridge()

# Force CPU usage
self.DEVICE = "cpu"

# Path to the configuration file
config_path = "/home/raidhani/ZoeDepth/zoedepth/models/zoedepth_nk/config_zoedepth_nk.json"
weights_path = "/home/raidhani/ros2_ws/src/MiDaS/weights/dpt_large_384.pt"

# Load the JSON config
try:
    with open(config_path, 'r') as f:
        config = json.load(f)
        self.get_logger().info(f"Successfully loaded config from {config_path}")
except FileNotFoundError:
    self.get_logger().error(f"Configuration file not found at {config_path}")
    return

# Extract model configuration and debug the contents
try:
    model_conf = config.get('model', None)
    if not model_conf:
        raise ValueError(f"Model configuration not found in {config_path}")
    
    self.get_logger().info(f"Raw model configuration: {model_conf}")

    # Ensure 'bin_conf' contains valid entries and log each entry
    valid_bin_conf = []
    for conf in model_conf['bin_conf']:
        if isinstance(conf, dict):
            if 'name' in conf:
                self.get_logger().info(f"Valid bin_conf entry found: {conf}")
                valid_bin_conf.append(conf)
            else:
                self.get_logger().error(f"Invalid entry (missing 'name'): {conf}")
        else:
            self.get_logger().error(f"Invalid bin_conf entry (not a dict): {conf}")
    
    if not valid_bin_conf:
        raise ValueError(f"No valid 'bin_conf' entries found in {config_path}")

    # Update model configuration with valid bin_conf
    model_conf['bin_conf'] = valid_bin_conf
    self.get_logger().info(f"Filtered model configuration: {model_conf}")
except (KeyError, ValueError) as e:
    self.get_logger().error(f"Error processing model configuration: {e}")
    return

# Build the model using the valid model configuration
try:
    self.zoe = ZoeDepthNK.build(bin_conf=model_conf['bin_conf']).to(self.DEVICE)
    self.get_logger().info("Model successfully built.")
except KeyError as e:
    self.get_logger().error(f"Missing key in model configuration: {e}")
    return
except Exception as e:
    self.get_logger().error(f"Error building the model: {e}")
    return

# Load the weights into the model with strict=False to ignore mismatches
try:
    self.zoe.load_state_dict(torch.load(weights_path, map_location=self.DEVICE), strict=False)
    self.get_logger().info(f"Weights loaded from {weights_path}")
except FileNotFoundError:
    self.get_logger().error(f"Weights file not found at {weights_path}")
    return
except RuntimeError as e:
    self.get_logger().error(f"Error loading weights: {e}")
    return

def image_callback(self, msg):
# Ensure that the model has been built before processing images
if not hasattr(self, 'zoe'):
self.get_logger().error("Model has not been initialized.")
return

try:
    # Convert ROS image to OpenCV format
    cv_image = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8')
    self.get_logger().info(f"Received image with dimensions: {cv_image.shape}")

    # Resize image
    target_size = (512, 384)
    cv_image_resized = cv2.resize(cv_image, target_size)
    self.get_logger().info(f"Resized image to: {cv_image_resized.shape}")

    # Convert to PIL image
    pil_image = PilImage.fromarray(cv_image_resized)

    # Run depth inference
    depth_numpy = self.zoe.infer_pil(pil_image)

    # Colorize depth for visualization
    colored_depth = colorize(depth_numpy)

    # Show the depth image
    cv2.imshow("Depth", colored_depth)
    cv2.waitKey(1)

except Exception as e:
    self.get_logger().error(f"Error in image callback: {e}")

def main(args=None):
rclpy.init(args=args)
try:
depth_estimation_node = DepthEstimationNode()
rclpy.spin(depth_estimation_node)
except Exception as e:
print(f"Error running the node: {e}")
finally:
rclpy.shutdown()

if name == 'main':
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions