Skip to content

Commit

Permalink
fixing multiple publishers
Browse files Browse the repository at this point in the history
  • Loading branch information
atobaruela-ibrobotics committed Dec 3, 2024
1 parent 81f3ff8 commit 908814f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
68 changes: 47 additions & 21 deletions src/image_object_detection/image_object_detection_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ def __init__(self):
)


# Add the parameter callback handler
self.add_on_set_parameters_callback(self.parameters_callback)

self.declare_parameter("subscribers.qos_policy", "best_effort")
self.subscribers_qos = (
Expand Down Expand Up @@ -157,6 +155,9 @@ def __init__(self):

self.initialize_model()

# Add the parameter callback handler
self.add_on_set_parameters_callback(self.parameters_callback)

def parameters_callback(self, params):
result = SetParametersResult(successful=True)

Expand Down Expand Up @@ -200,27 +201,52 @@ def parameters_callback(self, params):

return result
def setup_camera_topics(self):
# Clear existing subscribers and publishers
self.subscribers = []
self.detection_publishers = {}
self.debug_image_publishers = {}

for topic in self.camera_topics:
self.subscribers.append(
self.create_subscription(
Image,
topic,
callback=self.image_callback_factory(topic),
qos_profile=self.qos,
# Create sets of existing topics
existing_sub_topics = {sub.topic_name for sub in self.subscribers}
existing_pub_topics = set(self.detection_publishers.keys())
existing_debug_topics = set(self.debug_image_publishers.keys())

# Set of new topics
new_topics = set(self.camera_topics)

# Remove subscribers for topics that no longer exist
for sub in list(self.subscribers):
if sub.topic_name not in new_topics:
sub.destroy()
self.subscribers.remove(sub)

# Remove publishers for topics that no longer exist
for topic in list(self.detection_publishers.keys()):
if topic not in new_topics:
self.detection_publishers[topic].destroy()
self.detection_publishers[topic].destroy()
del self.detection_publishers[topic]

# Remove debug image publishers for topics that no longer exist
for topic in list(self.debug_image_publishers.keys()):
if topic not in new_topics:
self.debug_image_publishers[topic].destroy()
del self.debug_image_publishers[topic]

# Add new topics
for topic in new_topics:
if topic not in existing_sub_topics:
self.subscribers.append(
self.create_subscription(
Image,
topic,
callback=self.image_callback_factory(topic),
qos_profile=self.qos,
)
)
)

detection_topic = f"{topic}/detections"
self.detection_publishers[topic] = self.create_publisher(
Detection2DArray, detection_topic, self.qos
)
if topic not in existing_pub_topics:
detection_topic = f"{topic}/detections"
self.detection_publishers[topic] = self.create_publisher(
Detection2DArray, detection_topic, self.qos
)

if self.enable_publish_debug_image:
if self.enable_publish_debug_image and topic not in existing_debug_topics:
debug_image_topic = f"{topic}/debug_image"
self.debug_image_publishers[topic] = self.create_publisher(
Image, debug_image_topic, self.qos
Expand All @@ -229,7 +255,7 @@ def setup_camera_topics(self):
def initialize_model(self):
with torch.no_grad():
set_logging()
self.device = select_device(self.device)
self.device = select_device(str(self.device))
self.half = self.device.type != "cpu"

self.model = attempt_load(
Expand Down
4 changes: 2 additions & 2 deletions src/image_object_detection/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ <h3>Camera Topics</h3>
</form>

<div class="video-grid">
{% for topic in camera_topics %}
{% for topic in camera_debug_image_topics %}
<div class="video-container">
<div class="video-title">{{ topic }}</div>
<img src="http://localhost:8080/stream?topic={{ topic }}&type=mjpeg" alt="{{ topic }} stream">
<img src="http://localhost:8080/stream?topic={{ topic }}&type=mjpeg&qos_profile=sensor_data" alt="{{ topic }} stream">
</div>
{% endfor %}
</div>
Expand Down
10 changes: 9 additions & 1 deletion src/image_object_detection/web_interface_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ def __init__(self, detection_node):
def setup_routes(self):
@self.app.route('/')
def index():

camera_debug_image_topics = []
for camera in self.detection_node.camera_topics:
camera_debug_image_topics.append(camera.replace("/image,", "/debug_image"))

print(camera_debug_image_topics)

return render_template('index.html',
available_classes=self.detection_node.names,
selected_classes=self.detection_node.selected_detections,
confidence=self.detection_node.confidence,
iou_threshold=self.detection_node.iou_threshold,
camera_topics=self.detection_node.camera_topics,
publish_debug_image=self.detection_node.enable_publish_debug_image,
image_size=self.detection_node.model_image_size)
image_size=self.detection_node.model_image_size,
camera_debug_image_topics= [topic.replace("/image", "/image/debug_image") for topic in self.detection_node.camera_topics])

@self.app.route('/update_params', methods=['POST'])
def update_params():
Expand Down

0 comments on commit 908814f

Please sign in to comment.