def _draw_faces(self, image, faces):
        """Take an Image message and a Faces message, draw a boc around the
        faces in the image and publish the result.

        Record *image*'s sequence number in _last_published_seq.
        """

        # Convert image into RGB image
        rgb_im = image_to_array(image)

        # Use OpenCV to draw boxes in imahe
        for face in faces.faces:
            roi = face.roi

            # NB: cv2.rectangle will modify the image it works on.
            cv2.rectangle(rgb_im,
                    (roi.x_offset, roi.y_offset),
                    (roi.x_offset + roi.width, roi.y_offset + roi.height),
                    (0, 255, 0), # green
                    2, # thickness
            )

        # Publish image
        self._image_pub.publish(array_to_image(rgb_im))
        self._last_published_seq = image.header.seq
    def _draw_faces(self, image, faces):
        """Take an Image message and a Faces message, draw a boc around the
        faces in the image and publish the result.

        Record *image*'s sequence number in _last_published_seq.
        """

        # Convert image into RGB image
        rgb_im = image_to_array(image)

        # Use OpenCV to draw boxes in imahe
        for face in faces.faces:
            roi = face.roi

            # NB: cv2.rectangle will modify the image it works on.
            cv2.rectangle(
                rgb_im,
                (roi.x_offset, roi.y_offset),
                (roi.x_offset + roi.width, roi.y_offset + roi.height),
                (0, 255, 0),  # green
                2,  # thickness
            )

        # Publish image
        self._image_pub.publish(array_to_image(rgb_im))
        self._last_published_seq = image.header.seq
    def _detect_faces_callback(self, event):
        """Called periodically to detect faces in an image and publish the
        result.

        """
        # Get the latest image to arrive
        image = self._latest_image

        # Don't do anything if there is no latest image
        if image is None:
            return

        # "Claim" this image by clearing _latest_image
        self._latest_image = None

        # Create the face message we will eventually publish
        faces_msg = Faces()

        # Copy the header from the input image so we know when and what these
        # face detection results relate to.
        faces_msg.header = image.header

        # Parse image message into numpy array
        image_array = image_to_array(image)
        rospy.logdebug('Successfully parsed new image with shape: %s',
                       image_array.shape)

        # Convert image to grayscale
        gray_im = cv2.cvtColor(image_array, cv2.COLOR_RGB2GRAY)

        # Detect faces
        faces = self._detect_faces(gray_im)

        # Create a face bounding box for each face
        for face_idx, face_bbox in enumerate(faces):
            rospy.logdebug('Face #%s at %s', face_idx + 1, face_bbox)

            # Create face message
            face_msg = Face()
            face_msg.header = faces_msg.header

            # Initialise roi
            face_msg.roi.x_offset = face_bbox[0]
            face_msg.roi.y_offset = face_bbox[1]
            face_msg.roi.width = face_bbox[2]
            face_msg.roi.height = face_bbox[3]
            face_msg.roi.do_rectify = False

            # Append to list of faces
            faces_msg.faces.append(face_msg)

        # Publish faces messages
        self._faces_pub.publish(faces_msg)
Beispiel #4
0
    def _detect_faces_callback(self, event):
        """Called periodically to detect faces in an image and publish the
        result.

        """
        # Get the latest image to arrive
        image = self._latest_image

        # Don't do anything if there is no latest image
        if image is None:
            return

        # "Claim" this image by clearing _latest_image
        self._latest_image = None

        # Create the face message we will eventually publish
        faces_msg = Faces()

        # Copy the header from the input image so we know when and what these
        # face detection results relate to.
        faces_msg.header = image.header

        # Parse image message into numpy array
        image_array = image_to_array(image)
        rospy.logdebug('Successfully parsed new image with shape: %s',
            image_array.shape)

        # Convert image to grayscale
        gray_im = cv2.cvtColor(image_array, cv2.COLOR_RGB2GRAY)

        # Detect faces
        faces = self._detect_faces(gray_im)

        # Create a face bounding box for each face
        for face_idx, face_bbox in enumerate(faces):
            rospy.logdebug('Face #%s at %s', face_idx+1, face_bbox)

            # Create face message
            face_msg = Face()
            face_msg.header = faces_msg.header

            # Initialise roi
            face_msg.roi.x_offset = face_bbox[0]
            face_msg.roi.y_offset = face_bbox[1]
            face_msg.roi.width = face_bbox[2]
            face_msg.roi.height = face_bbox[3]
            face_msg.roi.do_rectify = False

            # Append to list of faces
            faces_msg.faces.append(face_msg)

        # Publish faces messages
        self._faces_pub.publish(faces_msg)