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

Callback freezes after 1sst frame with YOLO #4

Open
starcheek opened this issue Aug 1, 2024 · 1 comment
Open

Callback freezes after 1sst frame with YOLO #4

starcheek opened this issue Aug 1, 2024 · 1 comment

Comments

@starcheek
Copy link

Hi,

I have been trying to apply YOLO similar to PX4 version, but after 1st frame the callback stops calling. The problem is in combining the cv2.imshow and inference call. If i call them separately, i.e. only inference or only showing original frame wuth cv2, they both work fine. But if I add them in same block of callback, even if I do not try to plot results, the callback stops working after 1st frame.

#!/usr/bin/env python

import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
from ultralytics import YOLO

model = YOLO("yolov8n.pt")  

class Camera1:
    def __init__(self):
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/uav1/down_camera/image_raw", Image, self.callback)
        rospy.loginfo("Camera1 node initialized")

    def callback(self, data):
        rospy.loginfo("Received image frame")
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, desired_encoding="bgr8")
        except CvBridgeError as e:
            rospy.logerr(e)
            return

        resized_image = cv2.resize(cv_image, (640, 640))
        results = model.predict(resized_image)

        cv2.imshow('Detected Frame', results[0].plot())    
        cv2.waitKey(1)

def main():
    rospy.init_node('camera_read', anonymous=False)
    Camera1()
    
    try:
        rospy.spin()
    except KeyboardInterrupt:
        rospy.loginfo("Shutting down")
    
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()
@monemati
Copy link
Owner

monemati commented Aug 1, 2024

@starcheek Hi, I remember that I did something like this in here, and it was fine combining inference and imshow, you can check that out.

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

No branches or pull requests

2 participants