From 400307527e8c36751021b4a1a8eb46cb5feda456 Mon Sep 17 00:00:00 2001 From: yexia553 <1906390603@qq.com> Date: Mon, 27 Mar 2023 06:34:40 -0700 Subject: [PATCH 1/3] add home monitor func --- .../originbot_demo/api_connection.py | 40 +++++++++++++++ originbot_demo/originbot_demo/cam_sub.py | 51 +++++++++++++++++++ originbot_demo/setup.py | 1 + 3 files changed, 92 insertions(+) create mode 100644 originbot_demo/originbot_demo/api_connection.py create mode 100755 originbot_demo/originbot_demo/cam_sub.py diff --git a/originbot_demo/originbot_demo/api_connection.py b/originbot_demo/originbot_demo/api_connection.py new file mode 100644 index 0000000..c2c71bd --- /dev/null +++ b/originbot_demo/originbot_demo/api_connection.py @@ -0,0 +1,40 @@ +""" +API CONNECTION FOR IMPORTING WRAPPER +""" +import logging +import requests + + +logging.basicConfig( + format="%(asctime)s %(levelname)-8s %(message)s", + level=logging.INFO, + datefmt="%Y-%m-%d %H:%M:%S", +) + + +class APIConnection: + """ + Api Connection + """ + + def __init__(self): + self.api_url = "http://:/videos/upload/" + self.auth = ('', '') + + def post_data(self, path): + """ + """ + try: + with open(path, 'rb') as f: + files = {'file': f} + response = requests.post(self.api_url, files=files, auth=self.auth) + + if response.status_code in [200, 201]: + logging.info("上传成功") + return True + return False + + except Exception as err: + logging.error(err) + + return False diff --git a/originbot_demo/originbot_demo/cam_sub.py b/originbot_demo/originbot_demo/cam_sub.py new file mode 100755 index 0000000..d1c44e0 --- /dev/null +++ b/originbot_demo/originbot_demo/cam_sub.py @@ -0,0 +1,51 @@ +import rclpy +from rclpy.node import Node +from sensor_msgs.msg import Image +import cv2 +import time +from datetime import datetime +import os + +from .api_connection import APIConnection + + +class ImageSubscriber(Node): + + def __init__(self, output_dir='output'): + super().__init__('image_subscriber') + self.subscription = self.create_subscription( + Image, + 'image_raw', + self.callback, + 10) + self.output_dir = output_dir + self.conn = APIConnection() + + def callback(self, data): + # 将ROS2 Image转换为OpenCV图像,假设已安装了cv_bridge + import cv_bridge + bridge = cv_bridge.CvBridge() + img = bridge.imgmsg_to_cv2(data) + timestamp = str(datetime.now()) # 根据时间戳创建新的输出文件 + # output_file_path = '/path/to/your/output/' + timestamp + '.jpg' + output_file_path = '/home/pzx/workspace/originbot_desktop/output/' + timestamp + '.jpg' + cv2.imwrite(output_file_path, img) + self.conn.post_data(output_file_path) + os.remove(output_file_path) + + time.sleep(60) # 每分钟获取一次照片并上传 + + +def main(args=None): + rclpy.init(args=args) + + image_subscriber = ImageSubscriber() + + rclpy.spin(image_subscriber) + + image_subscriber.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main() diff --git a/originbot_demo/setup.py b/originbot_demo/setup.py index 2cd9af9..e7fc9fb 100644 --- a/originbot_demo/setup.py +++ b/originbot_demo/setup.py @@ -25,6 +25,7 @@ setup( 'echo_status = originbot_demo.echo_status:main', 'control_buzzer = originbot_demo.control_buzzer:main', 'control_led = originbot_demo.control_led:main', + 'cam_sub = originbot_demo.cam_sub:main', ], }, ) -- Gitee From 28c0f5c65e8819b8c962d358c0c0e1aa0a5b4a7a Mon Sep 17 00:00:00 2001 From: yexia553 <1906390603@qq.com> Date: Mon, 27 Mar 2023 06:59:15 -0700 Subject: [PATCH 2/3] refactor: create a separate pkg for home monitor --- originbot_demo/setup.py | 1 - .../originbot_home_monitor/__init__.py | 0 .../originbot_home_monitor}/api_connection.py | 1 + .../originbot_home_monitor}/cam_sub.py | 5 ++-- originbot_home_monitor/package.xml | 18 +++++++++++++ .../resource/originbot_home_monitor | 0 originbot_home_monitor/setup.cfg | 4 +++ originbot_home_monitor/setup.py | 26 +++++++++++++++++++ originbot_home_monitor/test/test_copyright.py | 23 ++++++++++++++++ originbot_home_monitor/test/test_flake8.py | 25 ++++++++++++++++++ originbot_home_monitor/test/test_pep257.py | 23 ++++++++++++++++ 11 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 originbot_home_monitor/originbot_home_monitor/__init__.py rename {originbot_demo/originbot_demo => originbot_home_monitor/originbot_home_monitor}/api_connection.py (95%) rename {originbot_demo/originbot_demo => originbot_home_monitor/originbot_home_monitor}/cam_sub.py (83%) create mode 100644 originbot_home_monitor/package.xml create mode 100644 originbot_home_monitor/resource/originbot_home_monitor create mode 100644 originbot_home_monitor/setup.cfg create mode 100644 originbot_home_monitor/setup.py create mode 100644 originbot_home_monitor/test/test_copyright.py create mode 100644 originbot_home_monitor/test/test_flake8.py create mode 100644 originbot_home_monitor/test/test_pep257.py diff --git a/originbot_demo/setup.py b/originbot_demo/setup.py index e7fc9fb..2cd9af9 100644 --- a/originbot_demo/setup.py +++ b/originbot_demo/setup.py @@ -25,7 +25,6 @@ setup( 'echo_status = originbot_demo.echo_status:main', 'control_buzzer = originbot_demo.control_buzzer:main', 'control_led = originbot_demo.control_led:main', - 'cam_sub = originbot_demo.cam_sub:main', ], }, ) diff --git a/originbot_home_monitor/originbot_home_monitor/__init__.py b/originbot_home_monitor/originbot_home_monitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/originbot_demo/originbot_demo/api_connection.py b/originbot_home_monitor/originbot_home_monitor/api_connection.py similarity index 95% rename from originbot_demo/originbot_demo/api_connection.py rename to originbot_home_monitor/originbot_home_monitor/api_connection.py index c2c71bd..8f0b84d 100644 --- a/originbot_demo/originbot_demo/api_connection.py +++ b/originbot_home_monitor/originbot_home_monitor/api_connection.py @@ -18,6 +18,7 @@ class APIConnection: """ def __init__(self): + # 下面需要填写web的ip和端口 self.api_url = "http://:/videos/upload/" self.auth = ('', '') diff --git a/originbot_demo/originbot_demo/cam_sub.py b/originbot_home_monitor/originbot_home_monitor/cam_sub.py similarity index 83% rename from originbot_demo/originbot_demo/cam_sub.py rename to originbot_home_monitor/originbot_home_monitor/cam_sub.py index d1c44e0..a4daef0 100755 --- a/originbot_demo/originbot_demo/cam_sub.py +++ b/originbot_home_monitor/originbot_home_monitor/cam_sub.py @@ -27,13 +27,12 @@ class ImageSubscriber(Node): bridge = cv_bridge.CvBridge() img = bridge.imgmsg_to_cv2(data) timestamp = str(datetime.now()) # 根据时间戳创建新的输出文件 - # output_file_path = '/path/to/your/output/' + timestamp + '.jpg' - output_file_path = '/home/pzx/workspace/originbot_desktop/output/' + timestamp + '.jpg' + output_file_path = '/absolute/path/to/your/output/' + timestamp + '.jpg' cv2.imwrite(output_file_path, img) self.conn.post_data(output_file_path) os.remove(output_file_path) - time.sleep(60) # 每分钟获取一次照片并上传 + time.sleep(60) # 每分钟获取一次照片并上传,可以自定义 def main(args=None): diff --git a/originbot_home_monitor/package.xml b/originbot_home_monitor/package.xml new file mode 100644 index 0000000..cf5828e --- /dev/null +++ b/originbot_home_monitor/package.xml @@ -0,0 +1,18 @@ + + + + originbot_home_monitor + 0.0.0 + OriginBot Home Monitor + pzx + Apache 2.0 + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + + diff --git a/originbot_home_monitor/resource/originbot_home_monitor b/originbot_home_monitor/resource/originbot_home_monitor new file mode 100644 index 0000000..e69de29 diff --git a/originbot_home_monitor/setup.cfg b/originbot_home_monitor/setup.cfg new file mode 100644 index 0000000..c48254e --- /dev/null +++ b/originbot_home_monitor/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script-dir=$base/lib/originbot_home_monitor +[install] +install-scripts=$base/lib/originbot_home_monitor diff --git a/originbot_home_monitor/setup.py b/originbot_home_monitor/setup.py new file mode 100644 index 0000000..f0ec3aa --- /dev/null +++ b/originbot_home_monitor/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup + +package_name = 'originbot_home_monitor' + +setup( + name=package_name, + version='0.0.0', + 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='潘智祥', + maintainer_email='1906390603@qq.com', + description='Originbot Home Monitor', + license='Apache 2.0', + tests_require=['pytest'], + entry_points={ + 'console_scripts': [ + 'cam_sub = originbot_demo.cam_sub:main', + ], + }, +) diff --git a/originbot_home_monitor/test/test_copyright.py b/originbot_home_monitor/test/test_copyright.py new file mode 100644 index 0000000..cc8ff03 --- /dev/null +++ b/originbot_home_monitor/test/test_copyright.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_copyright.main import main +import pytest + + +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/originbot_home_monitor/test/test_flake8.py b/originbot_home_monitor/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/originbot_home_monitor/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/originbot_home_monitor/test/test_pep257.py b/originbot_home_monitor/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/originbot_home_monitor/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' -- Gitee From 44afcb2247a5429e9edb1ea8a783258e4daa24f2 Mon Sep 17 00:00:00 2001 From: yexia553 <1906390603@qq.com> Date: Sun, 2 Apr 2023 10:24:11 +0000 Subject: [PATCH 3/3] update originbot_home_monitor/setup.py. fix Signed-off-by: yexia553 <1906390603@qq.com> --- originbot_home_monitor/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/originbot_home_monitor/setup.py b/originbot_home_monitor/setup.py index f0ec3aa..226ffef 100644 --- a/originbot_home_monitor/setup.py +++ b/originbot_home_monitor/setup.py @@ -20,7 +20,7 @@ setup( tests_require=['pytest'], entry_points={ 'console_scripts': [ - 'cam_sub = originbot_demo.cam_sub:main', + 'cam_sub = originbot_home_monitor.cam_sub:main', ], }, ) -- Gitee