-
Notifications
You must be signed in to change notification settings - Fork 2
/
detection_node.py
51 lines (43 loc) · 1.63 KB
/
detection_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3.9
import rospy
import time
import hardcoded_bridge as wtf
from sensor_msgs.msg import Image
from uchile_msgs.msg import ImageArray
from detector.detect import FaceDetector
class face_detector_node():
def __init__(self):
print('Nodo creado')
self.subscriber_topic = '/maqui/camera/front/image_raw'
self.subscriber = None
self.publisher = rospy.Publisher('/maqui/interactions/face_detection', ImageArray, queue_size=5, latch=True)
self.detector = FaceDetector(keep_top_k = 3, buff_size = 10)
def _callback(self, data):
cv_image = wtf.imgmsg_to_cv2(data)
self.detector.process_img(cv_image)
if self.detector.is_buffer_full():
output = StringArray()
output.header.stamp = rospy.Time.now()
try:
output.data.append(str(self.buffer_faces))
print(f'Sending face image')
self.publisher.publish(output)
except:
with Exception as e:
print(e)
def start_callback(self):
if self.subscriber is None:
self.subscriber = rospy.Subscriber(self.subscriber_topic, Image, self._callback)
def stop_callback(self):
if self.subscriber is not None:
self.subscriber.unregister()
self.subscriber = None
def main():
rospy.init_node('face_detector_node')
node = face_detector_node()
rospy.on_shutdown(node.stop_callback) # This will call the stop_callback method when the node is stopped
time.sleep(0.5)
node.start_callback()
rospy.spin()
if __name__ == '__main__':
main()