小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
4.2.4人脸检测客户端实现错误
-
[INFO] [1712386054.199687907] [face_detect_client]: 等待服务端上线......
Traceback (most recent call last):
File "/home/tingbo/chapt4/chapt4_ws/src/install/demo_python_service/lib/demo_python_service/face_detect_client_node", line 33, in <module>
sys.exit(load_entry_point('demo-python-service==0.0.0', 'console_scripts', 'face_detect_client_node')())
File "/home/tingbo/chapt4/chapt4_ws/src/install/demo_python_service/lib/python3.10/site-packages/demo_python_service/face_detect_client_node.py", line 43, in main
face_detect_client.send_request()
File "/home/tingbo/chapt4/chapt4_ws/src/install/demo_python_service/lib/python3.10/site-packages/demo_python_service/face_detect_client_node.py", line 21, in send_request
request.image = self.bridge.cv2_to_compressed_imgmsg(self.image)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/cv_bridge/core.py", line 228, in cv2_to_compressed_imgmsg
raise TypeError('Your input type is not a numpy array')
TypeError: Your input type is not a numpy array
[ros2run]: Process exited with failure 1客户端代码
import rclpy
from rclpy.node import Node
from chapt4_interfaces.srv import FaceDetector
from sensor_msgs.msg import Image
from ament_index_python.packages import get_package_share_directory
import cv2
from cv_bridge import CvBridgeclass FaceDetectorClient(Node):
def init(self):
super().init('face_detect_client')
self.client = self.create_client(FaceDetector, '/face_detect')
self.bridge = CvBridge()
self.test1_image_path = get_package_share_directory(
'demo_python_service')+'resource/test1.jpg'
self.image = cv2.imread(self.test1_image_path)
def send_request(self):
while self.client.wait_for_service(timeout_sec=1.0) is False:
self.get_logger().info(f'等待服务端上线......')
request = FaceDetector.Request()
request.image = self.bridge.cv2_to_compressed_imgmsg(self.image)
future = self.client.call_async(request)
rclpy.spin_until_future_complete(self, future)
response = future.result()
self.get_logger().info(
f'接收到响应: 图像中共有:{response.number}张脸,耗时{response.use_time}')
self.show_face_locations(response)def show_face_locations(self,response): for i in range(response.number): top = response.top[i] right = response.right[i] bottom = response.bottom[i] left = response.left[i] cv2.rectangle(self.image, (left, top), (right, bottom),(255, 0, 0), 2) cv2.imshow('Face Detection Result',self.image) cv2.waitKey(0)
def main(args=None):
rclpy.init(args=args)
face_detect_client = FaceDetectorClient()
face_detect_client.send_request()
rclpy.shutdown() -
@43996173 确认下图片路径下是否有对应图片,可以加个文件路径打印
-
-
@43996173 Traceback (most recent call last):
File "/home/ros2-ll/chapt4/chapt4_ws/install/demo_python_service/lib/demo_python_service/face_detect_client_node", line 33, in <module>
sys.exit(load_entry_point('demo-python-service==0.0.0', 'console_scripts', 'face_detect_client_node')())
File "/home/ros2-ll/chapt4/chapt4_ws/install/demo_python_service/lib/python3.10/site-packages/demo_python_service/face_detect_client_node.py", line 41, in main
face_detect_client = FaceDetectorClient()
TypeError: Node.init() missing 1 required positional argument: 'node_name'
[ros2run]: Process exited with failure 1
复制了又有这种报错 -