Program Listing for File face.hpp

Return to documentation for file (include/hri/face.hpp)

// Copyright (c) 2023 PAL Robotics S.L. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef HRI__FACE_HPP_
#define HRI__FACE_HPP_

#include <memory>
#include <optional>
#include <string>

#include "geometry_msgs/msg/transform_stamped.hpp"
#include "hri_msgs/msg/expression.hpp"
#include "hri_msgs/msg/facial_action_units.hpp"
#include "hri_msgs/msg/facial_landmarks.hpp"
#include "hri_msgs/msg/normalized_region_of_interest2_d.hpp"
#include "hri_msgs/msg/soft_biometrics.hpp"
#include "opencv2/core.hpp"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "tf2_ros/buffer.h"

#include "hri/feature_tracker.hpp"
#include "hri/types.hpp"

namespace hri
{

// TODO(LJU): possibly subscribe also to the /frontalized sub-topic
class Face : public FeatureTracker, public std::enable_shared_from_this<Face>
{
  friend class HRIListener;  // for invalidate()

public:
  Face(
    ID id,
    NodeInterfaces node_interfaces,
    rclcpp::CallbackGroup::SharedPtr callback_group,
    const tf2::BufferCore & tf_buffer,
    const std::string & reference_frame);

  virtual ~Face();

  std::string gazeFrame() const {return kGazeFrame_;}

  std::optional<cv::Rect2f> roi() const {return roi_;}

  std::optional<cv::Mat> cropped() const {return cropped_;}

  std::optional<cv::Mat> aligned() const {return aligned_;}

  std::optional<FacialLandmarks> facialLandmarks() const {return landmarks_;}

  std::optional<FacialActionUnits> facialActionUnits() const {return facial_action_units_;}

  std::optional<float> age() const {return age_;}

  std::optional<Gender> gender() const {return gender_;}

  std::optional<Expression> expression() const {return expression_;}

  std::optional<ExpressionVA> expressionVA() const {return expression_va_;}

  std::optional<float> expressionConfidence() const {return expression_confidence_;}

  std::optional<geometry_msgs::msg::TransformStamped> gazeTransform() const;

private:
  void onRoI(hri_msgs::msg::NormalizedRegionOfInterest2D::ConstSharedPtr msg);
  void onCropped(sensor_msgs::msg::Image::ConstSharedPtr msg);
  void onAligned(sensor_msgs::msg::Image::ConstSharedPtr msg);
  void onLandmarks(hri_msgs::msg::FacialLandmarks::ConstSharedPtr msg);
  void onSoftBiometrics(hri_msgs::msg::SoftBiometrics::ConstSharedPtr msg);
  void onFacs(hri_msgs::msg::FacialActionUnits::ConstSharedPtr msg);
  void onExpression(hri_msgs::msg::Expression::ConstSharedPtr msg);

  void invalidate();

  std::optional<cv::Rect2f> roi_;
  std::optional<cv::Mat> cropped_;
  std::optional<cv::Mat> aligned_;
  std::optional<FacialLandmarks> landmarks_;
  std::optional<float> age_;
  std::optional<Gender> gender_;
  std::optional<FacialActionUnits> facial_action_units_;
  std::optional<Expression> expression_;
  std::optional<ExpressionVA> expression_va_;
  std::optional<float> expression_confidence_;

  rclcpp::Subscription<hri_msgs::msg::NormalizedRegionOfInterest2D>::SharedPtr roi_subscriber_;
  rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr cropped_subscriber_;
  rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr aligned_subscriber_;
  rclcpp::Subscription<hri_msgs::msg::FacialLandmarks>::SharedPtr landmarks_subscriber_;
  rclcpp::Subscription<hri_msgs::msg::SoftBiometrics>::SharedPtr softbiometrics_subscriber_;
  rclcpp::Subscription<hri_msgs::msg::FacialActionUnits>::SharedPtr facial_action_units_subscriber_;
  rclcpp::Subscription<hri_msgs::msg::Expression>::SharedPtr expression_subscriber_;

  const std::string kGazeFrame_;
};

typedef std::shared_ptr<Face> FacePtr;
typedef std::shared_ptr<const Face> ConstFacePtr;

}  // namespace hri

#endif  // HRI__FACE_HPP_