ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hello subarashi,
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
For checking the, topic uses Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release()
cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img) pub.publish(image_message)
2 | No.2 Revision |
Hello subarashi,
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
For checking the, topic uses Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release()
cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img)
bridge.cv2_to_imgmsg(img, encoding="passthrough")
pub.publish(image_message)
3 | No.3 Revision |
Hello subarashi,
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
For checking the, topic uses Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release()
cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img, encoding="passthrough") pub.publish(image_message)
4 | No.4 Revision |
Hello subarashi,
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
For checking the, topic uses Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release()
cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img, encoding="passthrough") pub.publish(image_message)
5 | No.5 Revision |
Hello subarashi,
For checking the, topic uses use Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release()
cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img, encoding="passthrough") pub.publish(image_message)
If you are not clear or have some questions feel free to drop commet.