OpenCV工具使用非常广。关于在ROS2中如何使用的博客一直没有更新:
最早,2019年:ROS2使用OpenCV基础
最近更新了一些:
仿真中使用习题如下:
那么如何实现如下的人脸识别效果呢?
蒙娜丽莎的微笑,永远那么神秘……
遇到的问题如:
[INFO] [1655036597.738995500] [face_detection]: Receiving image
Traceback (most recent call last):
File "/opt/ros/humble/local/lib/python3.10/dist-packages/cv_bridge/core.py", line 194, in imgmsg_to_cv2
res = cvtColor2(im, img_msg.encoding, desired_encoding)
RuntimeError: [8UC3] is not a color format. but [bgr8] is. The conversion does not make sense
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/zhangrelay/ros_ws/opencv_ros2-main/opencv_ros2/face_detection.py", line 95, in
main()
File "/home/zhangrelay/ros_ws/opencv_ros2-main/opencv_ros2/face_detection.py", line 84, in main
rclpy.spin(face_detection)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/__init__.py", line 222, in spin
executor.spin_once()
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 712, in spin_once
raise handler.exception()
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/task.py", line 239, in __call__
self._handler.send(None)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 418, in handler
await call_coroutine(entity, arg)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 343, in _execute_subscription
await await_or_execute(sub.callback, msg)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 107, in await_or_execute
return callback(*args)
File "/home/zhangrelay/ros_ws/opencv_ros2-main/opencv_ros2/face_detection.py", line 53, in listener_callback
current_frame = self.br.imgmsg_to_cv2(data, "bgr8")
File "/opt/ros/humble/local/lib/python3.10/dist-packages/cv_bridge/core.py", line 196, in imgmsg_to_cv2
raise CvBridgeError(e)
cv_bridge.core.CvBridgeError: [8UC3] is not a color format. but [bgr8] is. The conversion does not make sense
如何解决呢?
提示:
current_frame = self.br.imgmsg_to_cv2(data, "rgb8")
答案:
[8UC3] is not a color format. but [rgb8] is.
zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 node list
/face_detection
/image_publisher
/rqt_gui_py_node_1492
zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 node info /face_detection
/face_detection
Subscribers:
/image_raw: sensor_msgs/msg/Image
Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
Service Servers:
/face_detection/describe_parameters: rcl_interfaces/srv/DescribeParameters
/face_detection/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/face_detection/get_parameters: rcl_interfaces/srv/GetParameters
/face_detection/list_parameters: rcl_interfaces/srv/ListParameters
/face_detection/set_parameters: rcl_interfaces/srv/SetParameters
/face_detection/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
Service Clients:
Action Servers:
Action Clients:
zhangrelay@LAPTOP-5REQ7K1L:~$
基础概念
什么是人脸检测?
在计算机视觉中,试图解决的一个基本问题是在没有人工干预的情况下自动检测图像中的对象。人脸检测可以被认为是在图像中检测人脸的问题。人类的面部可能存在细微差异,但总的来说,可以肯定地说,某些特征与所有人类面部相关。人脸检测算法有很多种。
人脸检测通常是许多人脸相关技术的第一步,例如人脸识别或验证。然而,人脸检测可以有非常有用的应用。人脸检测最成功的应用可能是拍照。当您为朋友拍照时,数码相机内置的人脸检测算法会检测人脸的位置并相应地调整焦点。
什么是 OpenCV
在人工智能领域,计算机视觉是最有趣和最具挑战性的任务之一。计算机视觉就像计算机软件和我们周围的可视化之间的桥梁。它允许计算机软件理解和了解周围环境的可视化。例如:根据颜色、形状和大小确定水果。这项任务对人脑来说可能很容易,但是在计算机视觉管道中,首先我们收集数据,然后执行数据处理活动,然后训练和教授模型以了解如何根据大小区分水果,水果的形状和颜色。
目前,存在各种软件包来执行机器学习、深度学习和计算机视觉任务。到目前为止,计算机视觉是此类复杂活动的最佳模块。 OpenCV 是一个开源库。它受到各种编程语言的支持,例如 R、Python。它可以在大多数平台上运行,例如 Windows、Linux 和 MacOS。