ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Calling subscriber class in __init__ asks for 2 arguments

asked 2022-09-05 08:44:00 -0500

updated 2022-09-07 21:15:33 -0500

ravijoshi gravatar image

Please see my code below:

import tensorflow as tf
import cv2
from cv_bridge import CvBridge
import rospy
from sensor_msgs.msg import Image as ImageMsg
import colorsys
import os
from timeit import default_timer as timer
from yolo import YOLO, detect_video
import numpy as np

from keras import backend as K
from keras.models import load_model
from keras.layers import Input
from PIL import Image, ImageFont, ImageDraw

from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body
from yolo3.utils import letterbox_image
from keras.utils import multi_gpu_model

class subscriber(object):

    def __init__(self):
        self.frame = None

    @staticmethod
    def callback(self, data):
        # Used to convert between ROS and OpenCV images
        vid = CvBridge() 
        accum_time = 0
        curr_fps = 0
        fps = "FPS: ??"
        prev_time = timer()       
        # Output debugging information to the terminal
        rospy.loginfo("receiving video frame")
        # Convert ROS Image message to OpenCV image

        self.frame = vid.imgmsg_to_cv2(data)
        pub = rospy.Publisher('imagefeed', ImageMsg, queue_size=10)
        rospy.loginfo("Publishing image")
        pub.publish(vid.cv2_to_imgmsg(self.frame, "bgr8"))

        image = Image.fromarray(self.frame)
        image = yolo.detect_image(image)
        result = np.asarray(image)
        curr_time = timer()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1
        if accum_time > 1:
            accum_time = accum_time - 1
            fps = "FPS: " + str(curr_fps)
            curr_fps = 0
        cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=0.50, color=(255, 0, 0), thickness=2)
        cv2.namedWindow("result", cv2.WINDOW_NORMAL)
        cv2.imshow("result", result)
        if isOutput:
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            yolo.close_session()

    def listen(self):
        rospy.init_node("video_sub_py", anonymous=True)
        rospy.Subscriber("/tello/camera/image_raw", ImageMsg, self.callback)
        rospy.spin()

if __name__ == "__main__":
    listener = subscriber()
    listener.listen()

This gives the output:

[ERROR] [1662384402.347352]: bad callback: <function callback at 0x7fc810608350>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
TypeError: callback() takes exactly 2 arguments (1 given)

Why does this not work on python 2.7? This way works on 3.6.9 but not on this one.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-07 21:19:00 -0500

ravijoshi gravatar image

Just remove the @staticmethod decorator from the code and you are good to go!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-09-05 08:44:00 -0500

Seen: 25 times

Last updated: Sep 07 '22