ros调用摄像头的代码样例
时间: 2023-10-22 08:10:59 浏览: 124
你好!以下是调用USB摄像头的ROS代码样例:
```python
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class Camera:
def __init__(self):
self.bridge = CvBridge()
self.image_pub = rospy.Publisher("image_topic", Image, queue_size=1)
self.cap = cv2.VideoCapture(0)
def publish(self):
ret, frame = self.cap.read()
if ret:
try:
self.image_pub.publish(self.bridge.cv2_to_imgmsg(frame, "bgr8"))
except CvBridgeError as e:
print(e)
if __name__ == '__main__':
rospy.init_node('camera', anonymous=True)
rate = rospy.Rate(10)
cam = Camera()
while not rospy.is_shutdown():
cam.publish()
rate.sleep()
```
希望以上代码能够帮助您!
阅读全文