Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
larm-astro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Inès EL HADRI
larm-astro
Commits
777efb1b
Commit
777efb1b
authored
Jan 22, 2024
by
Inès EL HADRI
💤
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Limit image publishing to one per second
parent
1617c5be
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
11 deletions
+26
-11
cam_vision
grp_astro/src/cam_vision
+26
-11
No files found.
grp_astro/src/cam_vision
View file @
777efb1b
...
@@ -64,29 +64,40 @@ class CameraVision(CameraDriver):
...
@@ -64,29 +64,40 @@ class CameraVision(CameraDriver):
# Initialize a clock for the publisher
# Initialize a clock for the publisher
self
.
create_timer
(
timerFreq
,
self
.
loop
)
self
.
create_timer
(
timerFreq
,
self
.
loop
)
self
.
create_timer
(
1
,
self
.
publish_imgs
)
# Limit the publishing of images to 1 each second (to prevent network issues)
def
publish_imgs
(
self
):
if
self
.
_rgb_detection
is
None
or
self
.
_mask_detection
is
None
:
return
def
publish_msgs
(
self
,
image
,
mask
,
xDist
,
yDist
):
# Convert cv2 images to ROS2 RGB Image
# Convert cv2 images to ROS2 RGB Image20
ros2_mask_image
=
cv2
.
cvtColor
(
self
.
_mask_detection
,
cv2
.
COLOR_GRAY2RGB
)
ros2_mask_image
=
cv2
.
cvtColor
(
mask
,
cv2
.
COLOR_GRAY2RGB
)
ros2_mask_image
=
cv2
.
cvtColor
(
ros2_mask_image
,
cv2
.
COLOR_RGB2BGR
)
ros2_mask_image
=
cv2
.
cvtColor
(
ros2_mask_image
,
cv2
.
COLOR_RGB2BGR
)
ros2_mask_image
=
self
.
_bridge
.
cv2_to_imgmsg
(
ros2_mask_image
,
"bgr8"
)
ros2_mask_image
=
self
.
_bridge
.
cv2_to_imgmsg
(
ros2_mask_image
,
"bgr8"
)
ros2_mask_image
.
header
.
stamp
=
self
.
get_clock
()
.
now
()
.
to_msg
()
ros2_mask_image
.
header
.
stamp
=
self
.
get_clock
()
.
now
()
.
to_msg
()
ros2_mask_image
.
header
.
frame_id
=
"image"
ros2_mask_image
.
header
.
frame_id
=
"image"
ros2_rgb_image
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_HSV2BGR
)
ros2_rgb_image
=
cv2
.
cvtColor
(
self
.
_rgb_detection
,
cv2
.
COLOR_HSV2BGR
)
ros2_rgb_image
=
self
.
_bridge
.
cv2_to_imgmsg
(
ros2_rgb_image
,
"bgr8"
)
ros2_rgb_image
=
self
.
_bridge
.
cv2_to_imgmsg
(
ros2_rgb_image
,
"bgr8"
)
ros2_rgb_image
.
header
.
stamp
=
self
.
get_clock
()
.
now
()
.
to_msg
()
ros2_rgb_image
.
header
.
stamp
=
self
.
get_clock
()
.
now
()
.
to_msg
()
ros2_rgb_image
.
header
.
frame_id
=
"image"
ros2_rgb_image
.
header
.
frame_id
=
"image"
# Publish Images -> to be able to detect false positives and improve the algorithm
self
.
_rgb_detection_publisher
.
publish
(
ros2_rgb_image
)
self
.
_mask_detection_publisher
.
publish
(
ros2_mask_image
)
# Reset images
self
.
_rgb_detection
=
None
self
.
_mask_detection
=
None
def
publish_data
(
self
,
xDist
,
yDist
):
# Publish String message -> to inform that we have detected a bottle
# Publish String message -> to inform that we have detected a bottle
str
=
String
()
str
=
String
()
str
.
data
=
"A bottle has been detected"
str
.
data
=
"A bottle has been detected"
self
.
_detection_publisher
.
publish
(
str
)
self
.
_detection_publisher
.
publish
(
str
)
# Publish Images -> to be able to detect false positives and improve the algorithm
self
.
_rgb_detection_publisher
.
publish
(
ros2_rgb_image
)
self
.
_mask_detection_publisher
.
publish
(
ros2_mask_image
)
# Depth publish
# Depth publish
relative_position
=
Point
()
relative_position
=
Point
()
...
@@ -94,6 +105,8 @@ class CameraVision(CameraDriver):
...
@@ -94,6 +105,8 @@ class CameraVision(CameraDriver):
relative_position
.
y
=
yDist
relative_position
.
y
=
yDist
self
.
_bottle_relative_pos_publisher
.
publish
(
relative_position
)
self
.
_bottle_relative_pos_publisher
.
publish
(
relative_position
)
# Node internal loop
# Node internal loop
def
loop
(
self
):
def
loop
(
self
):
"""
"""
...
@@ -158,15 +171,17 @@ class CameraVision(CameraDriver):
...
@@ -158,15 +171,17 @@ class CameraVision(CameraDriver):
cv2
.
circle
(
image
,
(
int
(
x
),
int
(
y
)),
int
(
radius
),
self
.
_color_info
,
2
)
cv2
.
circle
(
image
,
(
int
(
x
),
int
(
y
)),
int
(
radius
),
self
.
_color_info
,
2
)
cv2
.
circle
(
image
,
(
int
(
x
),
int
(
y
)),
5
,
self
.
_color_info
,
10
)
cv2
.
circle
(
image
,
(
int
(
x
),
int
(
y
)),
5
,
self
.
_color_info
,
10
)
# Store images for publishing
self
.
_rgb_detection
=
image
self
.
_mask_detection
=
mask
x
,
y
=
int
(
x
),
int
(
y
)
x
,
y
=
int
(
x
),
int
(
y
)
# Depth calculus
# Depth calculus
xDist
,
yDist
=
self
.
pixelToRelativePos
(
x
,
y
)
xDist
,
yDist
=
self
.
pixelToRelativePos
(
x
,
y
)
# Publish images, string and position message
# Publish string and position message
self
.
publish_msgs
(
image
,
mask
,
xDist
,
yDist
)
self
.
publish_data
(
xDist
,
yDist
)
...
@@ -205,7 +220,7 @@ def main():
...
@@ -205,7 +220,7 @@ def main():
Main loop
Main loop
"""
"""
rclpy
.
init
()
rclpy
.
init
()
rosNode
=
CameraVision
(
timerFreq
=
1.0
/
12
0
)
rosNode
=
CameraVision
(
timerFreq
=
1.0
/
6
0
)
rclpy
.
spin
(
rosNode
)
rclpy
.
spin
(
rosNode
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment