Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

ROS subscribes to image type video frames (Python) through topic Publishing

#!/usr/bin/env python
#!coding=utf-8
#write by leo at 2018.04.26
#function:
#1, Get live_video from the webcam.
#2, With ROS publish Image_info (to yolo and so on).
#3, Convert directly, don't need to save pic at local.
import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
import sys
def webcamImagePub():
# init ros_node
rospy.init_node('webcam_puber', anonymous=True)
# queue_size should be small in order to make it 'real_time'
# or the node will pub the past_frame
img_pub = rospy.Publisher('webcam/image_raw', Image, queue_size=2)
rate = rospy.Rate(5) # 5hz
# make a video_object and init the video object
cap = cv2.VideoCapture(0)
# define picture to_down' coefficient of ratio
scaling_factor = 0.5
# the 'CVBridge' is a python_class, must have a instance.
# That means "cv2_to_imgmsg() must be called with CvBridge instance"
bridge = CvBridge()
if not cap.isOpened():
sys.stdout.write("Webcam is not available !")
return -1
count = 0
# loop until press 'esc' or 'q'
while not rospy.is_shutdown():
ret, frame = cap.read()
# resize the frame
if ret:
count = count + 1
else:
rospy.loginfo("Capturing image failed.")
if count == 2:
count = 0
frame = cv2.resize(frame,None,fx=scaling_factor,fy=scaling_factor,interpolation=cv2.INTER_AREA)
msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")
img_pub.publish(msg)
print '** publishing webcam_frame ***'
rate.sleep()
if __name__ == '__main__':
try:
webcamImagePub()
except rospy.ROSInterruptException:
pass
Comment

PREVIOUS NEXT
Code Example
Python :: miniforge cv2 vscode 
Python :: Printing a long code line to span over multiple lines 
Python :: ttk.frame 
Python :: using .get() for deep dictionary 
Python :: #check if the given date is a weekday or weekend 
Python :: cast set 
Python :: adjugate of 3x3 matrix in python 
Python :: penggunaan values di python 
Python :: Remove Brackets from List Using join method with loop 
Python :: python code to java code converter 
Python :: python to dart converter 
Python :: python tkinter.ttk combobox down event on mouseclick 
Python :: Data Extraction in Python 
Python :: python turtle star 
Python :: matrix implement 
Python :: how to end if else statement in python 
Python :: Flask_SQLAlchemy is claiming my value is not boolean 
Python :: Flask error: werkzeug.routing.BuildError 
Python :: how to wait 5 seconds in python 
Python :: containsDuplicate Set Solution 
Python :: ring Copy Lists 
Python :: delet categories from coco dataset 
Python :: Lambda expressions using f-string 
Python :: cuantas palabras hay en una frase en python 
Python :: read past tense 
Python :: obtenir coordonnees souris python 
Python :: ffmpeg python get total frames 
Python :: Wireframes and Surface Plots 
Python :: how to shorten turtle. to t. 
Python :: count numbers that add up to 10 in python 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =