how to make rospy.Subscriber("/camera/rgb/image_raw", Image, image_callback) work?
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 imutils.video 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:
try:
aa = bridge.imgmsg_to_cv2(_current_image, "bgr8")
except CvBridgeError as e:
print(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
stopEvent=threading.Event()
thread=threading.Thread(target=video_image,args=())
thread.start()
else:
print "Video already running"
def video_image():
# grab a reference to the image panels
global panelV1
try:
while not stopEvent.is_set():
# Capture a still image from the video stream
ret, frame = cap.read() # 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
lower=np.array([barH.get(),barS.get(),barV.get()])
upper=np.array([barH2.get(),barS2.get(),barV2.get()])
mask=cv2.inRange(hsvframe, lower, upper)
mask=Image.fromarray(mask)
mask=ImageTk.PhotoImage(mask)
'''
Keep for debug
'''
#cv2.imshow("Original",frame)
#cv2.imshow("Edged",edged)
#cv2.imshow("HSV",hsvframe)
#cv2.imshow("Mask", mask)
# convert the images to PIL format...
#pilframe=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#pilframe=Image.fromarray(pilframe)
#pilframe=ImageTk.PhotoImage(pilframe)
#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)
panelV1.pack()
# otherwise, update the image panels
else:
# update the panels
#print "enter panelV1 update"
panelV1.configure(image=mask)
panelV1.image = mask
panelV1.pack()
ch=cv2.waitKey(10) # 1 == capture every 10 millisec
if ch & 0xFF == ord('q'): # "q" to exit loop
break
# Required to release the device and prevent having to reset system
cap.release()
cv2.destroyAllWindows()
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)
barH.set(30)
barH.pack()
barH2 = Scale(master, from_=0, to=255, orient=HORIZONTAL, label="H max", length=600, tickinterval=128)
barH2.set(250)
barH2.pack()
#S value
barS ...
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, 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.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 withrostopic info /camera/rgb/image_raw
.@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 ofmany def
.I skipped it and used another code for the same purpose.