Skip to content

“uav_camera_det” doesn't work like it should. #31

@SolidSnakeT

Description

@SolidSnakeT

Hi everyone!!!
I should mention right away that I have Windows 11 and am using the following:
WSL
Ubuntu-22.04
I recently came across your PX4-ROS2-Gazebo-YOLOv8 repository and it seemed very useful, but I ran into a problem with the "uav_camera_det" script hanging, here's a screenshot

Image

To try and figure out what's wrong I added some “print” calls to the code, here's what it looks like:

# Basic ROS 2 program to subscribe to real-time streaming
# video from your built-in webcam
# Author:
# - Addison Sears-Collins
# - https://automaticaddison.com

# Import the necessary libraries
import rclpy  # Python library for ROS 2
from rclpy.node import Node  # Handles the creation of nodes
from sensor_msgs.msg import Image  # Image is the message type
from cv_bridge import CvBridge  # Package to convert between ROS and OpenCV Images
import cv2  # OpenCV library
from ultralytics import YOLO  # YOLO library

# Load the YOLOv8 model
model = YOLO('yolov8m.pt')


class ImageSubscriber(Node):
    """
    Create an ImageSubscriber class, which is a subclass of the Node class.
    """

    def __init__(self):
        """
        Class constructor to set up the node
        """
        print("1")
        # Initiate the Node class's constructor and give it a name
        super().__init__('image_subscriber')
        print("2")
        # Create the subscriber. This subscriber will receive an Image
        # from the video_frames topic. The queue size is 10 messages.
        self.subscription = self.create_subscription(
            Image,
            'camera',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning
        print("3")
        # Used to convert between ROS and OpenCV images
        self.br = CvBridge()
        print("4")
    def listener_callback(self, data):
        print("5")
        """
        Callback function.
        """
        # Display the message on the console
        print("6")
        self.get_logger().info('Receiving video frame')
        print("7")
        # Convert ROS Image message to OpenCV image
        print("8")
        current_frame = self.br.imgmsg_to_cv2(data, desired_encoding="bgr8")
        print("9")
        image = current_frame
        print("10")
        # Object Detection
        results = model.predict(image, classes=[0, 2])
        print("11")
        img = results[0].plot()
        # Show Results
        print("12")
        cv2.imshow('Detected Frame', img)
        print("13")
        cv2.waitKey(1)


def main(args=None):
    # Initialize the rclpy library
    print("14")
    rclpy.init(args=args)
    print("15")
    # Create the node
    image_subscriber = ImageSubscriber()
    print("16")
    # Spin the node so the callback function is called.
    rclpy.spin(image_subscriber)
    print("17")
    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    image_subscriber.destroy_node()
    print("18")
    # Shutdown the ROS client library for Python
    rclpy.shutdown()
    print("19")

if __name__ == '__main__':
    main()

I got the following:
14
15
1
2
3
4
16

I followed the manual installation completely, there were no errors when entering the installation commands.
Here are more screenshots:
Micro-XRCE-DDS-Agent:

Image

PX4:

Image

ros2 run ros_gz_image image_bridge /camera:

Image

Controlling the drone with the keyboard works without errors

Thank you in advance!!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions