forked from omsv135/SIV-ROS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode1.py
More file actions
53 lines (40 loc) · 1.26 KB
/
node1.py
File metadata and controls
53 lines (40 loc) · 1.26 KB
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
#!/usr/bin/env python3
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
def node1():
rospy.init_node('node1', anonymous=True)
rate = rospy.Rate(10) # 10 Hz
pub = rospy.Publisher('Webcam_img', Image, queue_size=10)
bridge = CvBridge()
while not rospy.is_shutdown():
# Capture image frames from webcam
image_frame = capture_image_from_webcam()
# Convert the image to a ROS message
ros_image = bridge.cv2_to_imgmsg(image_frame, encoding='bgr8')
# Publish the image frame
pub.publish(ros_image)
rate.sleep()
def capture_image_from_webcam():
# Open the webcam (usually with device index 0)
cap = cv2.VideoCapture(0)
cv2.waitKey(0)
# Check if the webcam is successfully opened
if not cap.isOpened():
print("Failed to open the webcam")
return None
# Capture frames from the webcam
ret, frame = cap.read()
# Release the webcam
cap.release()
# Check if the frame is captured successfully
if not ret:
print("Failed to capture frame from the webcam")
return None
return frame
if __name__ == '__main__':
try:
node1()
except rospy.ROSInterruptException:
pass