|
13 | 13 | # See the License for the specific language governing permissions and
|
14 | 14 | # limitations under the License.
|
15 | 15 |
|
16 |
| -import rospy |
| 16 | +import argparse |
17 | 17 | import mxnet as mx
|
| 18 | + |
| 19 | +import rospy |
18 | 20 | from vision_msgs.msg import Detection2DArray
|
19 | 21 | from sensor_msgs.msg import Image as ROS_Image
|
20 | 22 | from opendr_bridge import ROSBridge
|
| 23 | + |
| 24 | +from opendr.engine.data import Image |
21 | 25 | from opendr.perception.object_detection_2d import RetinaFaceLearner
|
22 | 26 | from opendr.perception.object_detection_2d import draw_bounding_boxes
|
23 |
| -from opendr.engine.data import Image |
24 | 27 |
|
25 | 28 |
|
26 | 29 | class FaceDetectionNode:
|
27 |
| - def __init__(self, input_image_topic="/usb_cam/image_raw", output_image_topic="/opendr/image_boxes_annotated", |
28 |
| - face_detections_topic="/opendr/faces", device="cuda", backbone="resnet"): |
| 30 | + |
| 31 | + def __init__(self, input_rgb_image_topic="/usb_cam/image_raw", |
| 32 | + output_rgb_image_topic="/opendr/image_faces_annotated", detections_topic="/opendr/faces", |
| 33 | + device="cuda", backbone="resnet"): |
29 | 34 | """
|
30 |
| - Creates a ROS Node for face detection |
31 |
| - :param input_image_topic: Topic from which we are reading the input image |
32 |
| - :type input_image_topic: str |
33 |
| - :param output_image_topic: Topic to which we are publishing the annotated image (if None, we are not publishing |
34 |
| - annotated image) |
35 |
| - :type output_image_topic: str |
36 |
| - :param face_detections_topic: Topic to which we are publishing the annotations (if None, we are not publishing |
37 |
| - annotated pose annotations) |
38 |
| - :type face_detections_topic: str |
| 35 | + Creates a ROS Node for face detection with Retinaface. |
| 36 | + :param input_rgb_image_topic: Topic from which we are reading the input image |
| 37 | + :type input_rgb_image_topic: str |
| 38 | + :param output_rgb_image_topic: Topic to which we are publishing the annotated image (if None, no annotated |
| 39 | + image is published) |
| 40 | + :type output_rgb_image_topic: str |
| 41 | + :param detections_topic: Topic to which we are publishing the annotations (if None, no face detection message |
| 42 | + is published) |
| 43 | + :type detections_topic: str |
39 | 44 | :param device: device on which we are running inference ('cpu' or 'cuda')
|
40 | 45 | :type device: str
|
41 |
| - :param backbone: retinaface backbone, options are ('mnet' and 'resnet'), where 'mnet' detects masked faces as well |
| 46 | + :param backbone: retinaface backbone, options are either 'mnet' or 'resnet', |
| 47 | + where 'mnet' detects masked faces as well |
42 | 48 | :type backbone: str
|
43 | 49 | """
|
| 50 | + self.input_rgb_image_topic = input_rgb_image_topic |
44 | 51 |
|
45 |
| - # Initialize the face detector |
46 |
| - self.face_detector = RetinaFaceLearner(backbone=backbone, device=device) |
47 |
| - self.face_detector.download(path=".", verbose=True) |
48 |
| - self.face_detector.load("retinaface_{}".format(backbone)) |
49 |
| - self.class_names = ["face", "masked_face"] |
50 |
| - |
51 |
| - # Initialize OpenDR ROSBridge object |
52 |
| - self.bridge = ROSBridge() |
53 |
| - |
54 |
| - # setup communications |
55 |
| - if output_image_topic is not None: |
56 |
| - self.image_publisher = rospy.Publisher(output_image_topic, ROS_Image, queue_size=10) |
| 52 | + if output_rgb_image_topic is not None: |
| 53 | + self.image_publisher = rospy.Publisher(output_rgb_image_topic, ROS_Image, queue_size=1) |
57 | 54 | else:
|
58 | 55 | self.image_publisher = None
|
59 | 56 |
|
60 |
| - if face_detections_topic is not None: |
61 |
| - self.face_publisher = rospy.Publisher(face_detections_topic, Detection2DArray, queue_size=10) |
| 57 | + if detections_topic is not None: |
| 58 | + self.face_publisher = rospy.Publisher(detections_topic, Detection2DArray, queue_size=1) |
62 | 59 | else:
|
63 | 60 | self.face_publisher = None
|
64 | 61 |
|
65 |
| - rospy.Subscriber(input_image_topic, ROS_Image, self.callback) |
| 62 | + self.bridge = ROSBridge() |
| 63 | + |
| 64 | + # Initialize the face detector |
| 65 | + self.face_detector = RetinaFaceLearner(backbone=backbone, device=device) |
| 66 | + self.face_detector.download(path=".", verbose=True) |
| 67 | + self.face_detector.load("retinaface_{}".format(backbone)) |
| 68 | + self.class_names = ["face", "masked_face"] |
| 69 | + |
| 70 | + def listen(self): |
| 71 | + """ |
| 72 | + Start the node and begin processing input data. |
| 73 | + """ |
| 74 | + rospy.init_node('face_detection_node', anonymous=True) |
| 75 | + rospy.Subscriber(self.input_rgb_image_topic, ROS_Image, self.callback, queue_size=1, buff_size=10000000) |
| 76 | + rospy.loginfo("Face detection RetinaFace node started.") |
| 77 | + rospy.spin() |
66 | 78 |
|
67 | 79 | def callback(self, data):
|
68 | 80 | """
|
69 |
| - Callback that process the input data and publishes to the corresponding topics |
70 |
| - :param data: input message |
| 81 | + Callback that processes the input data and publishes to the corresponding topics. |
| 82 | + :param data: Input image message |
71 | 83 | :type data: sensor_msgs.msg.Image
|
72 | 84 | """
|
73 |
| - |
74 | 85 | # Convert sensor_msgs.msg.Image into OpenDR Image
|
75 | 86 | image = self.bridge.from_ros_image(data, encoding='bgr8')
|
76 | 87 |
|
77 |
| - # Run pose estimation |
| 88 | + # Run face detection |
78 | 89 | boxes = self.face_detector.infer(image)
|
79 | 90 |
|
80 | 91 | # Get an OpenCV image back
|
81 | 92 | image = image.opencv()
|
82 |
| - |
83 |
| - # Convert detected boxes to ROS type and publish |
84 |
| - ros_boxes = self.bridge.to_ros_boxes(boxes) |
| 93 | + # Publish detections in ROS message |
| 94 | + ros_boxes = self.bridge.to_ros_boxes(boxes) # Convert to ROS boxes |
85 | 95 | if self.face_publisher is not None:
|
86 | 96 | self.face_publisher.publish(ros_boxes)
|
87 |
| - rospy.loginfo("Published face boxes") |
88 | 97 |
|
89 |
| - # Annotate image and publish result |
90 |
| - # NOTE: converting back to OpenDR BoundingBoxList is unnecessary here, |
91 |
| - # only used to test the corresponding bridge methods |
92 |
| - odr_boxes = self.bridge.from_ros_boxes(ros_boxes) |
93 |
| - image = draw_bounding_boxes(image, odr_boxes, class_names=self.class_names) |
94 | 98 | if self.image_publisher is not None:
|
95 |
| - message = self.bridge.to_ros_image(Image(image), encoding='bgr8') |
96 |
| - self.image_publisher.publish(message) |
97 |
| - rospy.loginfo("Published annotated image") |
| 99 | + # Annotate image with face detection boxes |
| 100 | + image = draw_bounding_boxes(image, boxes, class_names=self.class_names) |
| 101 | + # Convert the annotated OpenDR image to ROS2 image message using bridge and publish it |
| 102 | + self.image_publisher.publish(self.bridge.to_ros_image(Image(image), encoding='bgr8')) |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser() |
| 107 | + parser.add_argument("-i", "--input_rgb_image_topic", help="Topic name for input rgb image", |
| 108 | + type=str, default="/usb_cam/image_raw") |
| 109 | + parser.add_argument("-o", "--output_rgb_image_topic", help="Topic name for output annotated rgb image", |
| 110 | + type=str, default="/opendr/image_faces_annotated") |
| 111 | + parser.add_argument("-d", "--detections_topic", help="Topic name for detection messages", |
| 112 | + type=str, default="/opendr/faces") |
| 113 | + parser.add_argument("--device", help="Device to use, either \"cpu\" or \"cuda\", defaults to \"cuda\"", |
| 114 | + type=str, default="cuda", choices=["cuda", "cpu"]) |
| 115 | + parser.add_argument("--backbone", |
| 116 | + help="Retinaface backbone, options are either 'mnet' or 'resnet', where 'mnet' detects " |
| 117 | + "masked faces as well", |
| 118 | + type=str, default="resnet", choices=["resnet", "mnet"]) |
| 119 | + args = parser.parse_args() |
98 | 120 |
|
99 |
| - |
100 |
| -if __name__ == '__main__': |
101 |
| - # Automatically run on GPU/CPU |
102 | 121 | try:
|
103 |
| - if mx.context.num_gpus() > 0: |
104 |
| - print("GPU found.") |
105 |
| - device = 'cuda' |
106 |
| - else: |
| 122 | + if args.device == "cuda" and mx.context.num_gpus() > 0: |
| 123 | + device = "cuda" |
| 124 | + elif args.device == "cuda": |
107 | 125 | print("GPU not found. Using CPU instead.")
|
108 |
| - device = 'cpu' |
| 126 | + device = "cpu" |
| 127 | + else: |
| 128 | + print("Using CPU.") |
| 129 | + device = "cpu" |
109 | 130 | except:
|
110 |
| - device = 'cpu' |
| 131 | + print("Using CPU.") |
| 132 | + device = "cpu" |
111 | 133 |
|
112 |
| - # initialize ROS node |
113 |
| - rospy.init_node('opendr_face_detection', anonymous=True) |
114 |
| - rospy.loginfo("Face detection node started!") |
| 134 | + face_detection_node = FaceDetectionNode(device=device, backbone=args.backbone, |
| 135 | + input_rgb_image_topic=args.input_rgb_image_topic, |
| 136 | + output_rgb_image_topic=args.output_rgb_image_topic, |
| 137 | + detections_topic=args.detections_topic) |
| 138 | + face_detection_node.listen() |
115 | 139 |
|
116 |
| - # get network backbone ("mnet" detects masked faces as well) |
117 |
| - backbone = rospy.get_param("~backbone", "resnet") |
118 |
| - input_image_topic = rospy.get_param("~input_image_topic", "/videofile/image_raw") |
119 | 140 |
|
120 |
| - rospy.loginfo("Using backbone: {}".format(backbone)) |
121 |
| - assert backbone in ["resnet", "mnet"], "backbone should be one of ['resnet', 'mnet']" |
122 |
| - |
123 |
| - # created node object |
124 |
| - face_detection_node = FaceDetectionNode(device=device, backbone=backbone, |
125 |
| - input_image_topic=input_image_topic) |
126 |
| - # begin ROS communications |
127 |
| - rospy.spin() |
| 141 | +if __name__ == '__main__': |
| 142 | + main() |
0 commit comments