DOCS: object detection usecase

This commit is contained in:
Igor Brylyov 2025-02-27 16:55:58 +03:00 committed by Igor Brylev
parent 649a426c25
commit dc59211634
9 changed files with 1920 additions and 0 deletions

View file

@ -10,3 +10,7 @@
Надстройка для ArgumentParser.
Позволяет использовать передачу аргументов командной строки для Bledner и FreeCAD.
### get_interfaces.py
Получение информации о ROS2-топиках в пакете цифрового двойника, навыка.

68
utils/get_interfaces.py Normal file
View file

@ -0,0 +1,68 @@
import argparse
import os
import json
import subprocess
import signal
import time
from ros2cli.node.strategy import NodeStrategy
from ros2topic.api import get_topic_names_and_types
OUTPUT_FILE = "topics.json"
TOPICS_FILTER = ["/parameter_events", "/rosout"]
def get_script_args(cfg):
args = cfg["command"].split()
args.append(cfg["package"])
args.append(cfg["executable"])
return args
def get_topics(filename, path):
jsonpath = os.path.join(path, filename)
with NodeStrategy({}) as node:
topic_names_and_types = get_topic_names_and_types(node=node, include_hidden_topics=False)
topic_info = []
for (topic_name, topic_types) in topic_names_and_types:
if not topic_name in TOPICS_FILTER:
topic_info.append({"name": topic_name, "type": topic_types[0]})
print(f"---> number of topics: {len(topic_info)}")
j_data = {"topics": topic_info}
with open(jsonpath, "w") as fh:
json.dump(j_data, fh, indent=2)
for topic in topic_info:
print(topic["name"])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--package", required=True, help="Json-string/file with package parameters")
parser.add_argument("--path", default="", help="Output path")
parser.add_argument("--json", default=OUTPUT_FILE, help="Output file name in json-format")
parser.add_argument('--delay', default=5, type=int, help="Delay in seconds")
args = parser.parse_args()
if args.package[-5:] == ".json":
if not os.path.isfile(args.package):
print(f"Error: no such file '{args.package}'")
exit(-1)
with open(args.package, "r") as f:
j_data = f.read()
else:
j_data = args.package
try:
cfg = json.loads(j_data)
except json.JSONDecodeError as e:
print(f"JSon error: {e}")
exit(-2)
cmd = get_script_args(cfg)
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(args.delay)
get_topics(args.json, args.path)
process.send_signal(signal.SIGINT)

View file

@ -0,0 +1,72 @@
"""
ROS2_Topic_to_json
ROS 2 program for outputting system objects to json format
From https://github.com/ros2/ros2cli/blob/humble/ros2topic/ros2topic/verb/list.py
@shalenikol release 0.1
"""
"""
usage: python ros2_topic_to_json.py [-h] [--jsonpath JSONPATH]
options:
-h, --help show this help message and exit
--jsonpath JSONPATH Output file in json-format
"""
import argparse
import json
from ros2cli.node.strategy import NodeStrategy
from ros2topic.api import get_topic_names_and_types
OUTPUT_FILE = "topics.json"
# def show_topic_info(topic_info, is_publisher):
# message = ('Published' if is_publisher else 'Subscribed') + ' topics:\n'
# for (topic_name, topic_types, pub_count, sub_count) in topic_info:
# count = pub_count if is_publisher else sub_count
# if count:
# topic_types_formatted = ', '.join(topic_types)
# count_str = str(count) + ' ' + ('publisher' if is_publisher else 'subscriber') \
# + ('s' if count > 1 else '')
# message += f' * {topic_name} [{topic_types_formatted}] {count_str}\n'
# return message
def main(args, jsonpath):
topic_info = []
with NodeStrategy(args) as node:
topic_names_and_types = get_topic_names_and_types(
node=node,
include_hidden_topics=False)
for (topic_name, topic_types) in topic_names_and_types:
# if args.verbose:
# pub_count = node.count_publishers(topic_name)
# sub_count = node.count_subscribers(topic_name)
# topic_info.append((topic_name, topic_types, pub_count, sub_count))
# else:
topic_info.append((topic_name, topic_types))
# if args.count_topics:
print(f"---> number of topics: {len(topic_names_and_types)}")
j_data = {"topics": topic_info}
with open(jsonpath, "w") as fh:
json.dump(j_data, fh, indent=2)
# elif topic_names_and_types:
# if args.verbose:
# print(show_topic_info(topic_info, is_publisher=True))
# print(show_topic_info(topic_info, is_publisher=False))
# else:
for (topic_name, topic_types) in topic_info:
msg = "{topic_name}"
# topic_types_formatted = ', '.join(topic_types)
# if args.show_types:
# msg += ' [{topic_types_formatted}]'
print(msg.format_map(locals()))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--jsonpath", default=OUTPUT_FILE, help="Output file in json-format")
m_args = parser.parse_args()
args = {}
main(args, m_args.jsonpath)