Git Product home page Git Product logo

headswap's Introduction

HeadSwap

This is a HeadSwap project, mainly inspired by HeSer.Pytorch
It includes two stages

  1. One stage deeply copy from PIRender
  2. The other stage is from HeSer.Pytorch

!!!!! NEW !!!!

Colab notebooks:

  • single image infer: single image infer

Some examples

the pictures is from 小红书 and baidu

Reference

super resolution -> CF-Net
face parsing -> face-parsing.PyTorch
3dmm -> Deep3DFaceRecon_pytorch

How to Run

  1. envrionment
    follow PIRender and Heser
    LVT in this project is follow here
  2. download model
    a. follow Deep3DFaceRecon_pytorch(https://github.com/sicxu/Deep3DFaceRecon_pytorch#prepare-prerequisite-models), download BFM files and epoch_20.pth in pretrained_models
    b. follow PIRender, put epoch_00190_iteration_000400000_checkpoint.pt to pretrained_models
    c.
    cd process
    bash download_weight.sh
    
  3. run
    # set your own image path in inference.py
    python inference.py

Credits

HeSer.Pytorch model and implementation: https://github.com/LeslieZhoa/HeSer.Pytorch Copyright © 2022,LeslieZhoa License https://github.com/LeslieZhoa/HeSer.Pytorch/blob/main/LICENSE

PIRender model and implementation: https://github.com/RenYurui/PIRender Copyright © 2021,RenYurui. License https://github.com/RenYurui/PIRender/blob/main/LICENSE.md

CF-Net model and implementation: https://github.com/ytZhang99/CF-Net Copyright © 2021,ytZhang99.

Deep3DFaceRecon_pytorch model and implementation: https://github.com/sicxu/Deep3DFaceRecon_pytorch Copyright © 2021,sicxu. License https://github.com/sicxu/Deep3DFaceRecon_pytorch/blob/master/LICENSE

arcface pytorch model pytorch model and implementation: https://github.com/ronghuaiyang/arcface-pytorch Copyright © 2018, ronghuaiyang.

LVT model and implementation: https://github.com/LeslieZhoa/LVT Copyright © 2022, LeslieZhoa.

face-parsing model and implementation: https://github.com/zllrunning/face-parsing.PyTorch Copyright © 2019, zllrunning. License https://github.com/zllrunning/face-parsing.PyTorch/blob/master/LICENSE

headswap's People

Contributors

lesliezhoa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

headswap's Issues

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.

Does anyone meet this problem? I've done everything before the final step and finally met this.

ValueError Traceback (most recent call last)
Cell In[5], line 1
----> 1 oup = model.run_single(src,tgt,crop_align=True,cat=True)
2 cv2.imshow("",oup)

Cell In[2], line 50
47 src_inp = self.preprocess(src_align)
48 tgt_inp = self.preprocess(tgt_align)
---> 50 tgt_params = self.get_params(cv2.resize(tgt_align,(256,256)),
51 info['rotated_lmk']/2.0).unsqueeze(0)
53 gen = self.forward(src_inp,tgt_inp,tgt_params)
55 gen = self.postprocess(gen[0])

File d:\HeadSwap\process\process_func.py:45, in Process.get_params(self, img, lmk)
43 def get_params(self,img,lmk):
---> 45 img,_,crop_param = self.preprocess_params(img,lmk)
46 with torch.no_grad():
47 coeff_3dmm = self.ParamsModel(img)

File d:\HeadSwap\process\process_func.py:73, in Process.preprocess_params(self, img, lm)
69 W,H = images.size
71 lm[:, -1] = H - 1 - lm[:, -1]
---> 73 trans_params, img, lm, _ = align_img(images, lm, self.lm3d_std)
74 img = torch.tensor(np.array(img)/255., dtype=torch.float32).permute(2, 0, 1)
...
76 img_new, lm_new, mask_new = resize_n_crop_img(img, lm, t, s, target_size=target_size, mask=mask)
---> 77 trans_params = np.array([w0, h0, s, t[0], t[1]],dtype=np.float32)
79 return trans_params, img_new, lm_new, mask_new

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.

What alignment/cropping/format?

Hello,

I tried your repo, but the results didn't work. What preprocessing do I need to do on the images before running them through inference.py?

Thanks!

Where is Pre-Trained Models folder?

I guess I'm not understanding the directions... i don't see this folder in any of the repos mentioned. Unless you mean for us to create it manually in this repo?

Improve color and style transfer from the source

I tried to swap between a source image with blonde hair and pale skin, and a target with brown hair and tanned skin. The outcome has the shape of the source image but the color of the underlying target image (brown hair instead of blonde hair).

This is similar to one of your examples: https://github.com/LeslieZhoa/HeadSwap/blob/main/assets/1-e6de879a-f3c2-47f8-9588-feab95df7b9e.png
The source image has blonde hair but the output has black hair like the target.

I'm hoping for improvements that allows me to choose from keeping the color of the target or using the color from source. Would this require further model training? If this does, you might release improved code and models since I don't have the hardware to train anything yet.

Face light adjustment?

I find that in those samples the result face light is affected by the source face but not the target one, any way to fix that?

Pretrained models missing file needed for BFM

After I set up /pretrained_models/BFM folder and run inference.py I got this error:

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\envs\simswap\lib\site-packages\scipy\io\matlab\mio.py", line 39, in _open_file
    return open(file_like, mode), True
FileNotFoundError: [Errno 2] No such file or directory: 'pretrained_models/BFM\\similarity_Lm3D_all.mat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "inference.py", line 172, in <module>
    'pretrained_models/BFM')
  File "inference.py", line 20, in __init__
    Process.__init__(self,params_path,bfm_folder)
  File "D:\HeadSwap\process\process_func.py", line 24, in __init__
    self.lm3d_std = load_lm3d(bfm_folder)
  File "D:\HeadSwap\model\third\Deep3dRec\preprocess.py", line 8, in load_lm3d
    Lm3D = loadmat(osp.join(bfm_folder, 'similarity_Lm3D_all.mat'))
  File "C:\Users\User\anaconda3\envs\simswap\lib\site-packages\scipy\io\matlab\mio.py", line 222, in loadmat
    with _open_file_context(file_name, appendmat) as f:
  File "C:\Users\User\anaconda3\envs\simswap\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\User\anaconda3\envs\simswap\lib\site-packages\scipy\io\matlab\mio.py", line 17, in _open_file_context
    f, opened = _open_file(file_like, appendmat, mode)
  File "C:\Users\User\anaconda3\envs\simswap\lib\site-packages\scipy\io\matlab\mio.py", line 45, in _open_file
    return open(file_like, mode), True
FileNotFoundError: [Errno 2] No such file or directory: 'pretrained_models/BFM\\similarity_Lm3D_all.mat'

In the distribution of Basel Face Model (241,347 kB) there is no similarity_Lm3D_all.mat. Please specify which files are needed in BFM folder other than 01_MorphableModel.mat and Exp_Pca.bin from Deep3DFaceRecon_pytorch

Trouble with model init

I run code in colab and when I executed this code
"model = Infer('pretrained_models/epoch_00190_iteration_000400000_checkpoint.pt',
'pretrained_models/Blender-401-00012900.pth',
'pretrained_models/parsing.pth',
'pretrained_models/epoch_20.pth',
'pretrained_models/BFM')"

There appeared an error below
" ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in <cell line: 1>()
----> 1 model = Infer('pretrained_models/epoch_00190_iteration_000400000_checkpoint.pt',
2 'pretrained_models/Blender-401-00012900.pth',
3 'pretrained_models/parsing.pth',
4 'pretrained_models/epoch_20.pth',
5 'pretrained_models/BFM')

2 frames
/usr/lib/python3.10/enum.py in getattr(cls, name)
435 return cls.member_map[name]
436 except KeyError:
--> 437 raise AttributeError(name) from None
438
439 def getitem(cls, name):

AttributeError: _3D"

Please help me fix it

Does this project have certain requirements for the input images?

Hi, I tried to use your project to do head swapping, but I found the output result not very satisfying. For example, after the swap, the resulting face is completely distorted, especially the eyes. I wonder if there is any reason for this problem since from your demo pictures, the results look promising.
image

Google Colab

Hello, can you create a google colab version of this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.