GithubHelp home page GithubHelp logo

socaity / face2face Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 2.0 15.68 MB

Swap faces in images and videos. Create face embeddings. Enhance face image quality. Deploy as a web api.

License: MIT License

Python 99.88% Batchfile 0.12%
face-swapping insightface roop deep-fake face-detection video-processing artificial-intelligence celebrity deep-learning face-recognition

face2face's Introduction

Face2Face

Instantly swap faces in images and videos


Face2Face is a generative AI technology to swap faces (aka Deep Fake) in images from one to another. For example, you can swap your face with Mona Lisa or your favorite celebrity.

With this repository you can:

This is a one shot face-swap model; for this reason only one face is needed to swap. It should work for all kinds of content, also for anime. The face swapping model itself was created by Insightface

We provide the face swapping functionality as SDK and as a convenient web (openAPI) API with FastTaskAPI. The endpoint allows you to easily deploy face swapping, recognition and restoration as a service.

Example swaps

Face-swap Multi-face Swap
Video-swapping Video-Swapping with face-recognition
Face restoration Face-Swap with face-recognition

Setup

Install via pip

Depending on your use case you can install the package with or without the service.

# face2face without service (only for inference from script)
pip install socaity-face2face 
# full package with service
pip install socaity-face2face[full]

Additional dependencies:

  • For VideoFile support in the webservice you also need to install ffmpeg

Requirements:

  • Python 3.7 or higher
  • Minimum 5GB of RAM (you'll get a "Killed" message without further information if you run out of memory)
  • Recommended: GPU with at least 8GB of VRAM for fast-inference. Runs also on CPU though.

Note: Models are downloaded automatically

Usage

We provide two ways to use the face swapping functionality.

  1. Direct module import and inference
  2. By deploying and calling the web service

Inference from script

Use the Face2Face class to swap faces from one image to another. First create an instance of the class.

from face2face import Face2Face
f2f = Face2Face(device_id=0) 

With the device_id setting you can set the GPU device id. This also allows to run face2face in multiple processes on different GPUs.

Swap faces from one image to another

swapped_img = f2f.swap_img_to_img("path/to/src.jpg", "path/to/target.jpg")

Face swapping with face embeddings

Create a face embedding with the add_face function reuse those embeddings later.

# create a face embedding and save it to disk
embedding = f2f.add_face("my_new_face", "path/to/my_img_or_video.mp4", save=True)
# Swap all faces in the image or video with the face(s) in the face embedding
swapped = f2f.swap(media="path/to/my_img_or_video.jpg", faces="my_new_face")

If argument save=true is set, the face embedding is persisted and the f2f.swap function can be used later with the same face_name, even after restarting the project.

Face swapping with face recognition (swap pairs)

After an embedding was created, we can recognize / identify those persons. Then the identified persons can specifically be swapped with defined swap pairs. If the faces argument is provided as dict, the swap function recognizes and swaps the face-pairs correspondingly.

# Swap faces with defined swap pairs
# This function will swap the faces of trump with hagrid and biden with ron.
# assumption the faces [trump, hagrid, biden, ron] are already added with f2f.add_face
swapped = f2f.swap(
  media="path/to/my_img_or_video.mp4", 
  faces={
      "trump": "hagrid",
      "biden": "ron"
  }
)

Face swapping with a generator

Iteratively swapping from a list of images

def my_image_generator():
  for i in range(100):
    yield cv2.imread(f"image_{i}.jpg")

# for swapping to always the same face
for swapped_img in f2f.swap_to_face_generator(faces="my_embedding", target_img_generator=my_image_generator()):
  cv2.imshow("swapped", swapped_img)
  cv2.waitKey(1)

# including face recognition
for swapped_img in f2f.swap_pairs_generator(target_img_generator=my_image_generator(), swap_pairs={"trump": "hagrid"}):
  cv2.imshow("swapped", swapped_img)
  cv2.waitKey(1)

Face enhancing

The roop (inswapper) model operates on low resolution - what can harm the result face quality. However, there exist AI models, which can enhance the face quality by upscaling the image. We provide different models for face enhancement: gfpgan_1.4, and the gpen family. Check model_definitions.py for the available models. You can upscale up to 2048 with the GPEN model --> higher quality + higher runtime.

swapped_img = f2f.swap(media="path/to/my_img_or_video.mp4", enhance_face_model='gpen_bfr_512')

The corresponding model is automatically downloaded and used when enhance_faces is set to True.

Face-enhancing without face-swapping

Alternatively you can enhance faces directly without applying a swap.

# enhance all faces in the image
enhanced_img = f2f.enhance_faces(image="path/to/my_img.jpg", enhance_face_model='gpen_bfr_512')
# enhance a specific face.
target_img = cv2.imread("path/to/my_img.jpg")
detected_face = f2f.detect_faces(target_img)[0]  # In this case we simply take the first one
enhanced_img = f2f.enhance_single_face(target_img, detected_face, enhance_face_model='gpen_bfr_512')

Disclaimer

The author is not responsible for any misuse of the repository. Face swapping is a powerful technology that can be used for good and bad purposes. Please use it responsibly and do not harm others. Do not publish any images without the consent of the people in the images. The credits for face swapping technology go to the great Insightface Team thank you insightface.ai. This project uses their pretrained models and parts of their code. Special thanks goes to their work around ROOP. The author does not claim authorship for this repository. The authors contribution was to provide a convenient API and service around the face swapping. A big thank you also goes to the authors of GPEN and GFPGAN, who developed the models for face restoration.

Contribute

Any help with maintaining and extending the package is welcome. Feel free to open an issue or a pull request.

ToDo:

  • Improve face swap quality
    • Implement strength factor for applied face
  • Improve inference times
    • by implementing batching.
    • by using multi-threading in image_generators
  • remove insightface dependency and update onnx version
  • streaming for the webserver

face2face's People

Contributors

sauravgarg540 avatar w4hns1nn avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

face2face's Issues

multiface swap

Like the swap_one method there should be an intuitive way to swap many faces 2 many faces.

Compatibility issues and dependency issues

I tried to install on fresh server and get this. It want media_toolkit, but there is one.

$ python3 face2face/server.py --port 8020
Traceback (most recent call last):
  File "/home/vasya/face2face/face2face/server.py", line 4, in <module>
    from fast_task_api import FastTaskAPI, ImageFile, JobProgress, MediaFile, VideoFile
  File "/home/vasya/.local/lib/python3.10/site-packages/fast_task_api/__init__.py", line 1, in <module>
    from fast_task_api.fast_task_api import FastTaskAPI
  File "/home/vasya/.local/lib/python3.10/site-packages/fast_task_api/fast_task_api.py", line 5, in <module>
    from fast_task_api.core.routers._fastapi_router import SocaityFastAPIRouter
  File "/home/vasya/.local/lib/python3.10/site-packages/fast_task_api/core/routers/_fastapi_router.py", line 5, in <module>
    from media_toolkit.file_conversion import convert_to_upload_file_type
ModuleNotFoundError: No module named 'media_toolkit.file_conversion'
$ pip3 install media_toolkit
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: media_toolkit in /home/vasya/.local/lib/python3.10/site-packages (0.0.6)

Also you have in

https://github.com/SocAIty/face2face/blob/main/pyproject.toml

this mistake

media-tookit[VideoFile] >= 0.0.6

Not "tookit", "toolkit", you forget "l" letter.

This package don't help:

socaity-face2face[service]

Mistake in requirements.txt/pyproject.toml

image_2024-07-15_16-39-39

Please give instruction how to setup this project. It's imposible, in any case I have a dependency mistake..
Also please define Python version. E.g. Onnxryntime 1.9.0 requires Python 3.6-3.9

If you can please share Dockerfile example with simple python script without gpu.

Thank you very much!

create pip package for face2face

As the title says.

  • For that refactor the init.py files.
  • Add intuitive python methods for usage instead only of server based fastapi method.

Update documentation.

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.