Nirkin Face Swap  0.9.0
Face swap.
face_swap.h
1 #ifndef FACE_SWAP_FACE_SWAP_H
2 #define FACE_SWAP_FACE_SWAP_H
3 
4 #include "face_swap/cnn_3dmm_expr.h"
5 #include "face_swap/basel_3dmm.h"
6 #include "face_swap/face_renderer.h"
7 
8 // OpenCV
9 #include <opencv2/core.hpp>
10 
11 // sfl
12 #include <sfl/sequence_face_landmarks.h>
13 #include <sfl/utilities.h>
14 
15 // face_seg
16 #include <face_seg/face_seg.h>
17 
18 namespace face_swap
19 {
20  class FaceSwap
21  {
22  public:
35  FaceSwap(const std::string& landmarks_path, const std::string& model_3dmm_h5_path,
36  const std::string& model_3dmm_dat_path, const std::string& reg_model_path,
37  const std::string& reg_deploy_path, const std::string& reg_mean_path,
38  bool generic = false, bool with_expr = true, bool with_gpu = true,
39  int gpu_device_id = 0);
40 
48  void setSegmentationModel(const std::string& seg_model_path,
49  const std::string& seg_deploy_path);
50 
55 
59 
62  bool setSource(const cv::Mat& img, const cv::Mat& seg = cv::Mat());
63 
66  bool setTarget(const cv::Mat& img, const cv::Mat& seg = cv::Mat());
67 
70  cv::Mat swap();
71 
75  const Mesh& getSourceMesh() const;
76 
80  const Mesh& getTargetMesh() const;
81 
82  private:
83 
95  bool preprocessImages(const cv::Mat& img, const cv::Mat& seg,
96  std::vector<cv::Point>& landmarks, std::vector<cv::Point>& cropped_landmarks,
97  cv::Mat& cropped_img, cv::Mat& cropped_seg, cv::Rect& bbox = cv::Rect());
98 
111  void generateTexture(const Mesh& mesh, const cv::Mat& img, const cv::Mat& seg,
112  const cv::Mat& vecR, const cv::Mat& vecT, const cv::Mat& K,
113  cv::Mat& tex, cv::Mat& uv);
114 
126  cv::Mat generateTextureCoordinates(const Mesh& mesh, const cv::Size& img_size,
127  const cv::Mat& vecR, const cv::Mat& vecT, const cv::Mat& K);
128 
137  cv::Mat blend(const cv::Mat& src, const cv::Mat& dst,
138  const cv::Mat& dst_seg = cv::Mat());
139 
140  private:
141  std::shared_ptr<sfl::SequenceFaceLandmarks> m_sfl;
142  std::unique_ptr<CNN3DMMExpr> m_cnn_3dmm_expr;
143  std::unique_ptr<Basel3DMM> m_basel_3dmm;
144  std::unique_ptr<FaceRenderer> m_face_renderer;
145  std::unique_ptr<face_seg::FaceSeg> m_face_seg;
146 
147  bool m_with_gpu;
148  int m_gpu_device_id;
149 
150  Mesh m_src_mesh, m_dst_mesh;
151  cv::Mat m_vecR, m_vecT, m_K;
152  cv::Mat m_tex, m_uv;
153  cv::Mat m_tgt_cropped_img, m_tgt_cropped_seg;
154  cv::Mat m_target_img, m_target_seg;
155  cv::Rect m_target_bbox;
156 
158  cv::Mat m_source_img;
159  cv::Mat m_src_cropped_img, m_src_cropped_seg;
160  cv::Mat m_tgt_rendered_img;
161  std::vector<cv::Point> m_src_cropped_landmarks;
162  std::vector<cv::Point> m_tgt_cropped_landmarks;
163  cv::Mat m_src_vecR, m_src_vecT, m_src_K;
164  std::vector<cv::Point> m_src_landmarks, m_tgt_landmarks;
165 
166  public:
167  cv::Mat debugSource();
168  cv::Mat debugTarget();
169  cv::Mat debug();
170  cv::Mat debugSourceMesh();
171  cv::Mat debugTargetMesh();
172  cv::Mat debugMesh(const cv::Mat& img, const cv::Mat& seg,
173  const cv::Mat& uv, const Mesh& mesh,
174  const cv::Mat& vecR, const cv::Mat& vecT, const cv::Mat& K);
175  cv::Mat debugSourceLandmarks();
176  cv::Mat debugTargetLandmarks();
177  cv::Mat debugRender();
179  };
180 } // namespace face_swap
181 
182 #endif // FACE_SWAP_FACE_SWAP_H
183 
void setSegmentationModel(const std::string &seg_model_path, const std::string &seg_deploy_path)
Set segmentation model.
Definition: face_swap.h:20
cv::Mat swap()
Transfer the face from the source image onto the face in the target image.
const Mesh & getTargetMesh() const
Get the 3D reconstruced mesh of the target face.
bool setTarget(const cv::Mat &img, const cv::Mat &seg=cv::Mat())
Set target image and segmentation.
void clearSegmentationModel()
Clear previously set segmentation model.
bool isSegmentationModelInit()
Check whether the segmentation model is initialized.
Represents a 3D renderable shape.
Definition: basel_3dmm.h:13
Definition: basel_3dmm.h:9
const Mesh & getSourceMesh() const
Get the 3D reconstruced mesh of the source face.
FaceSwap(const std::string &landmarks_path, const std::string &model_3dmm_h5_path, const std::string &model_3dmm_dat_path, const std::string &reg_model_path, const std::string &reg_deploy_path, const std::string &reg_mean_path, bool generic=false, bool with_expr=true, bool with_gpu=true, int gpu_device_id=0)
Construct FaceSwap instance.
bool setSource(const cv::Mat &img, const cv::Mat &seg=cv::Mat())
Set source image and segmentation.