-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_cam_v2_pub.py
46 lines (35 loc) · 1.37 KB
/
pi_cam_v2_pub.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
#!/usr/bin/env python3
# Impoor the necessary libraries for reading raspberry pi camera on jetson nano
import cv2
import numpy as np
import time
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
# Define the class for publishing the image
class ImagePublisher(Node):
def __init__(self):
super().__init__('image_pub')
self.publisher_ = self.create_publisher(Image, 'image', 10)
def publisher(self, img):
self.publisher_.publish(img)
self.get_logger().info('Publishing image')
def main():
# Initialize the video capture from gstreamer
cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! appsink")
_, img = cap.read()
rclpy.init()
image_publisher = ImagePublisher()
rclpy.spin(image_publisher)
while not rclpy.is_shutdown() and cap.isOpened():
#read the image from the camera on /dev/video0
_, img = cap.read()
img = CvBridge().cv2_to_imgmsg(img, encoding="bgr8")
image_publisher.publisher(img=img)
time.sleep(0.05)
cap.release()
image_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()