inital commit

This commit is contained in:
Björn Ellensohn 2023-05-25 11:04:14 +02:00
parent cb8c69805c
commit 3f4d31a0c4
9 changed files with 177 additions and 0 deletions

25
package.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cam_opencv</name>
<version>0.0.1</version>
<description>Using OpenCV to publish UVC camera stream (should be fast?)</description>
<maintainer email="bjoern.ellensohn@gmail.com">bjorn</maintainer>
<license>Apache License 2.0</license>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<!-- Dependencies -->
<buildtool_depend>ament_python</buildtool_depend>
<build_depend>rclpy</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>cv_bridge</build_depend>
<build_depend>opencv</build_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>

0
resource/ros2_cam_openCV Normal file
View File

View File

@ -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()

4
setup.cfg Normal file
View File

@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/ros2_cam_openCV
[install]
install_scripts=$base/lib/ros2_cam_openCV

26
setup.py Normal file
View File

@ -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'
],
},
)

25
test/test_copyright.py Normal file
View File

@ -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'

25
test/test_flake8.py Normal file
View File

@ -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)

23
test/test_pep257.py Normal file
View File

@ -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'