how to make rospy.Subscriber("/camera/rgb/image_raw", Image, image_callback) work?

asked 2019-10-30 07:33:32 -0500

Redhwan gravatar image

updated 2019-10-30 07:34:59 -0500

I tried to change this code here using ROS but I can't.

it is working without ros.

I only modified this code:

import rospy

from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import numpy as np
import cv2
import os
import threading
import time
import sys

from Tkinter import *   # Library for slider widgets
import tkFileDialog
from PIL import Image
from PIL import ImageTk
from import FPS
import time
bridge = CvBridge()

_current_image = None
def image_callback(ros_image):
    global _current_image
    _current_image = ros_image

def start_video():
    global videostarted, stopEvent, cap, thread
    global bridge ,  _current_image
    if _current_image is not None:
            aa = bridge.imgmsg_to_cv2(_current_image, "bgr8")
        except CvBridgeError as e:

        # Create video camera object and start streaming to it.
        # cap = cv2.VideoCapture(0) # 0 == Continuous
        cap = aa
        fps = FPS().start()

        # The video_image loop must use tkinter threading or the sliders
        # won't work, so this sets up threading before calling video_image()
        if videostarted == False:
            videostarted = True
            print "Video already running"

def video_image():
    # grab a reference to the image panels
    global panelV1

        while not stopEvent.is_set():
            # Capture a still image from the video stream
            ret, frame = # Read a new frame
            frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)

        Keep for debug
            # Canny test code, only called w/ imshow() below
            #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            #edged = cv2.Canny(gray, 50, 100)

           # Convert image to HSV
            hsvframe = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            # Set up the min and max HSV settings
            mask=cv2.inRange(hsvframe, lower, upper)

        Keep for debug
            #cv2.imshow("Mask", mask)

            # convert the images to PIL format...

            #If the panels are None, initialize them
            if panelV1 is None:
                print "enter panelV1 None"
                # the first panel will store our original image
                panelV1 = Label(master,image=mask)
                panelV1.image = mask
                #panelV1.pack(side="right", padx=10,pady=10)

            # otherwise, update the image panels
                # update the panels
                #print "enter panelV1 update"
                panelV1.image = mask

                ch=cv2.waitKey(10)   # 1 == capture every 10 millisec
                if ch & 0xFF == ord('q'):  # "q" to exit loop

        # Required to release the device and prevent having to reset system

        print "exit video_image()"

    except RuntimeError, e:
        print "runtime error()"

def slider_init():
    global barH, barH2, barS, barS2, barV, barV2
    Set up sliders
    #H value
    barH = Scale(master, from_=0, to=255, orient=HORIZONTAL, label="H min", length=600, tickinterval=128)

    barH2 = Scale(master, from_=0, to=255, orient=HORIZONTAL, label="H max", length=600, tickinterval=128)

    #S value
    barS ...
edit retag flag offensive close merge delete


It would be helpful if you could add what commands you tried to run, what the output was (especially any errors), and what you mean when you say it's not working?

Thomas D gravatar image Thomas D  ( 2019-10-30 07:56:19 -0500 )edit

@ Thomas D, there are not any errors but I can't see the video when use the cv_bridge. So, I asked here, I don't know the reason, you can download the original code, then run it. there is not an error in both original and modified code.

Redhwan gravatar image Redhwan  ( 2019-10-30 08:03:55 -0500 )edit

What commands are you running? What is the output that makes you think something is not working? Do you have any publishers providing /camera/rgb/image_raw? You can check that with rostopic info /camera/rgb/image_raw.

Thomas D gravatar image Thomas D  ( 2019-10-31 15:00:30 -0500 )edit

@Thomas, thank you so much for your help. I used this subscriber rospy.Subscriber("/camera/rgb/image_raw", Image, image_callback)

I think that it needs to use class then def instead of many def.

I skipped it and used another code for the same purpose.

Redhwan gravatar image Redhwan  ( 2019-11-05 06:54:03 -0500 )edit