def updateImage(self, img):
arr = self.bridge.imgmsg_to_cv2(img,"bgr8")
# Uncomment following two lines for CompressedImage topic
#np_arr = np.fromstring(img.data, np.uint8)
#arr = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if self.image_lock.acquire(True):
self.img = arr
if self.model is None:
self.model = self.get_model()
self.img_out, self.boxes = self.predict(self.model, self.img)
self.img_out = np.asarray(self.img_out[0,:,:,:])
for box in self.boxes:
if 'traffic light' in box['label']:
cv2.rectangle(self.img_out,(box['topleft']['x'],
box['topleft']['y']),
(box['bottomright']['x'],
box['bottomright']['y']),
(255,0,0), 6)
cv2.putText(self.img_out, box['label'],
(box['topleft']['x'],
box['topleft']['y'] - 12), 0, 0.6, (255,0,0) ,6//3)
print(self.img_out.shape)
self.image_lock.release()
python类CompressedImage()的实例源码
def filter_image_msgs(topic, datatype, md5sum, msg_def, header):
if(datatype=="sensor_msgs/CompressedImage"):
if (opt_topic != "" and opt_topic == topic) or opt_topic == "":
print "############# USING ######################"
print topic,' with datatype:', str(datatype)
return True;
if(datatype=="theora_image_transport/Packet"):
if (opt_topic != "" and opt_topic == topic) or opt_topic == "":
print topic,' with datatype:', str(datatype)
# print "############# USING ######################"
print '!!! theora not supportet, sorry !!!'
return False;
if(datatype=="sensor_msgs/Image"):
if (opt_topic != "" and opt_topic == topic) or opt_topic == "":
print "############# USING ######################"
print topic,' with datatype:', str(datatype)
return True;
return False;
def __init__(self, width, height):
self.width = width
self.height = height
self.image = None
self.image_y = None
self.camera_sub = rospy.Subscriber("/camera/image_raw/compressed", CompressedImage,
callback=self.callback_camera, queue_size=1)
self.camera_sub_y = rospy.Subscriber("/camera_y/image_raw/compressed", CompressedImage,
callback=self.callback_camera_y, queue_size=1)
def from_msg(cls, msg):
"""Serializes a ROS Image message into a compressed PNG image.
Args:
msg: ROS Image or CompressedImage message.
Returns:
Compressed PNG image.
Raises:
CvBridgeError: On image conversion error.
"""
if isinstance(msg, CompressedImage):
# Decompress message.
msg = cls.from_raw(msg.data)
# Convert ROS Image to OpenCV image.
bridge = CvBridge()
img = bridge.imgmsg_to_cv2(msg)
# Convert to PNG with highest level of compression to limit bandwidth
# usage. PNG is used since it is a lossless format, so this can later
# be retrieved as a ROS image without issue.
compression = [cv2.IMWRITE_PNG_COMPRESSION, 9]
png = cv2.imencode(".png", img, compression)[1].tostring()
return png
def from_raw(cls, raw, compress=False):
"""Deserializes binary-encoded image data into a ROS Image message.
Args:
raw: Binary encoded image data.
compress: Whether to return a compressed image or not.
Returns:
ROS Image or CompressedImage message.
Raises:
CvBridgeError: On image conversion error.
"""
# Convert to OpenCV image.
nparr = np.fromstring(raw, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Convert to ROS message.
bridge = CvBridge()
msg = bridge.cv2_to_imgmsg(img)
if compress:
data = cls.from_msg(msg)
msg = CompressedImage()
msg.format = "png"
msg.data = data
return msg
def __init__(self):
"""ROS Subscriptions """
self.image_sub = rospy.Subscriber("/raspicam_node/image/compressed",CompressedImage,self.cvt_image)
self.cmdVelSub = rospy.Subscriber("/cmd_vel", Twist, self.callback)
self.baseVelocityPub = rospy.Publisher('/cmd_vel_stamped', TwistStamped, queue_size=10)
""" Variables """
self.secs = 0
self.nsecs = 0
self.last = 0
self.cmdvel = Twist()
self.baseVelocity = TwistStamped()
self.flag = 0
def __init__(self, get_model_callback, model_callback):
rospy.init_node('tlight_model')
self.model = get_model_callback()
self.get_model = get_model_callback
self.predict = model_callback
self.bridge = CvBridge()
self.boxes = None
self.img = None
self.img_out = None
self.image_lock = threading.RLock()
#self.sub = rospy.Subscriber('/left_camera/image_color/compressed', CompressedImage, self.updateImage) # Use for CompressedImage topic
self.sub = rospy.Subscriber('/image_raw', Image, self.updateImage, queue_size=1)
self.pub = rospy.Publisher('/out_image', Image, queue_size=1)
rospy.Timer(rospy.Duration(0.04), self.callbackImage)
def detect_and_visualize(self, root_dir=None, extension=None,
classes=[], thresh=0.6, show_timer=False):
global imgi
global img_rect
global Frame
global show
global trackpos
global imshow
global acc_pub
global acc_enable
acc_pub = rospy.Publisher("acc_cmd", ACCCommand, queue_size=2)
acc_enable = rospy.Publisher("acc_enable", Bool, queue_size=2)
# rospy.Timer(rospy.Duration(0.02), self.trackCallback)
rospy.Subscriber("/zed/left/image_rect_color/compressed",CompressedImage, self.ImgCcallback, queue_size = 4)
# rospy.Subscriber("/zed/left/image_rect_color",Image, self.Imgcallback, queue_size = 4)
rospy.Subscriber("/zed/depth/depth_registered",Image, self.DepthImgcallback, queue_size = 4)
im_path = '/home/wolfram/mxnet/example/ssd/data/demo/dog.jpg'
with open(im_path, 'rb') as fp:
img_content = fp.read()
trackpos = 0
imshow = 0
imgi = mx.img.imdecode(img_content)
while(1):
# ret,img_rect = cap.read()
dets = self.im_detect(root_dir, extension, show_timer=show_timer)
# if not isinstance(im_list, list):
# im_list = [im_list]
# assert len(dets) == len(im_list)
# for k, det in enumerate(dets):
# img[:, :, (0, 1, 2)] = img[:, :, (2, 1, 0)]
# img = img_rect.copy()
self.visualize_detection(img_rect, dets[0], classes, thresh)
if imshow == 1:
cv2.imshow("tester", show)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()