diff --git a/package.xml b/package.xml new file mode 100644 index 0000000..a076abc --- /dev/null +++ b/package.xml @@ -0,0 +1,25 @@ + + + + cam_opencv + 0.0.1 + Using OpenCV to publish UVC camera stream (should be fast?) + bjorn + Apache License 2.0 + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + rclpy + sensor_msgs + cv_bridge + opencv + + + ament_python + + diff --git a/resource/ros2_cam_openCV b/resource/ros2_cam_openCV new file mode 100644 index 0000000..e69de29 diff --git a/ros2_cam_openCV/ros2_cam_openCV/__init__.py b/ros2_cam_openCV/ros2_cam_openCV/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ros2_cam_openCV/ros2_cam_openCV/cam_node.py b/ros2_cam_openCV/ros2_cam_openCV/cam_node.py new file mode 100644 index 0000000..91a43ca --- /dev/null +++ b/ros2_cam_openCV/ros2_cam_openCV/cam_node.py @@ -0,0 +1,49 @@ +import rclpy +from rclpy.node import Node +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 + +class UvcCameraNode(Node): + def __init__(self): + super().__init__('uvc_camera_node') + self.bridge = CvBridge() + self.publisher = self.create_publisher(Image, 'camera/image', 10) + self.timer = self.create_timer(1/30, self.timer_callback) # Adjust the interval as needed --> here should be 30 Hz for usb webcam (30fps) + + # Initialize the UVC camera capture + self.cap = cv2.VideoCapture(0) # Modify the index if needed + + # Set the desired resolution + width = 640 + height = 480 + self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) + self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) + + def timer_callback(self): + ret, frame = self.cap.read() + + if ret: + # Perform any necessary image processing or hardware acceleration here + # Example: Apply a grayscale filter using the GPU-accelerated function + frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + + # Flip the image + frame_flipped = cv2.flip(frame_gray, 0) + + # Convert the processed frame to a ROS Image message + # img_msg = self.bridge.cv2_to_imgmsg(frame_gray, 'mono8') # or 'bgr8' for colored image + img_msg = self.bridge.cv2_to_imgmsg(frame_flipped, 'mono8') + + # Publish the processed image + self.publisher.publish(img_msg) + +def main(args=None): + rclpy.init(args=args) + node = UvcCameraNode() + rclpy.spin(node) + node.destroy_node() + rclpy.shutdown() + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3377d7b --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script_dir=$base/lib/ros2_cam_openCV +[install] +install_scripts=$base/lib/ros2_cam_openCV diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..13afb31 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup + +package_name = 'ros2_cam_openCV' + +setup( + name=package_name, + version='0.0.1', + packages=[package_name], + data_files=[ + ('share/ament_index/resource_index/packages', + ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), + ], + install_requires=['setuptools'], + zip_safe=True, + maintainer='bjorn', + maintainer_email='bjoern.ellensohn@gmail.com', + description='Using OpenCV to publish UVC camera stream (should be fast?)', + license='Apache License 2.0', + tests_require=['pytest'], + entry_points={ + 'console_scripts': [ + 'cam_node = ros2_cam_openCV.cam_node:main' + ], + }, +) diff --git a/test/test_copyright.py b/test/test_copyright.py new file mode 100644 index 0000000..97a3919 --- /dev/null +++ b/test/test_copyright.py @@ -0,0 +1,25 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_copyright.main import main +import pytest + + +# Remove the `skip` decorator once the source file(s) have a copyright header +@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.') +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/test/test_flake8.py b/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/test/test_flake8.py @@ -0,0 +1,25 @@ +# Copyright 2017 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_flake8.main import main_with_errors +import pytest + + +@pytest.mark.flake8 +@pytest.mark.linter +def test_flake8(): + rc, errors = main_with_errors(argv=[]) + assert rc == 0, \ + 'Found %d code style errors / warnings:\n' % len(errors) + \ + '\n'.join(errors) diff --git a/test/test_pep257.py b/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/test/test_pep257.py @@ -0,0 +1,23 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_pep257.main import main +import pytest + + +@pytest.mark.linter +@pytest.mark.pep257 +def test_pep257(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found code style errors / warnings'