forked from NVlabs/Deep_Object_Pose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
executable file
·55 lines (44 loc) · 1.55 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
# This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
# https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
"""
This file opens an RGB camera and publishes images via ROS.
It uses OpenCV to capture from camera 0.
"""
from __future__ import print_function
import rospy
from std_msgs.msg import String
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image as ImageSensor
from sensor_msgs.msg import Image as Image_msg
import numpy as np
import cv2
# Global variables
cam_index = 0 # index of camera to capture
topic = '/dope/webcam_rgb_raw' # topic for publishing
cap = cv2.VideoCapture(cam_index)
if not cap.isOpened():
print("ERROR: Unable to open camera for capture. Is camera plugged in?")
exit(1)
def publish_images(freq=5):
rospy.init_node('dope_webcam_rgb_raw', anonymous=True)
images_out = rospy.Publisher(topic, Image_msg, queue_size=10)
rate = rospy.Rate(freq)
print ("Publishing images from camera {} to topic '{}'...".format(
cam_index,
topic
)
)
print ("Ctrl-C to stop")
while not rospy.is_shutdown():
ret, frame = cap.read()
if ret:
msg_frame_edges = CvBridge().cv2_to_imgmsg(frame, "bgr8")
images_out.publish(msg_frame_edges)
rate.sleep()
if __name__ == "__main__":
try :
publish_images()
except rospy.ROSInterruptException:
pass