import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
import sys
def webcamImagePub():
rospy.init_node('webcam_puber', anonymous=True)
img_pub = rospy.Publisher('webcam/image_raw', Image, queue_size=2)
rate = rospy.Rate(5)
cap = cv2.VideoCapture(0)
scaling_factor = 0.5
bridge = CvBridge()
if not cap.isOpened():
sys.stdout.write("Webcam is not available !")
return -1
count = 0
while not rospy.is_shutdown():
ret, frame = cap.read()
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