{"id":23591213,"url":"https://github.com/socaity/face2face","last_synced_at":"2025-04-04T18:03:39.901Z","repository":{"id":213314169,"uuid":"733562541","full_name":"SocAIty/face2face","owner":"SocAIty","description":"Swap faces in images and videos. Create face embeddings. Enhance face image quality. Deploy as a web api.","archived":false,"fork":false,"pushed_at":"2025-03-19T16:24:51.000Z","size":16232,"stargazers_count":82,"open_issues_count":2,"forks_count":16,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T17:02:01.449Z","etag":null,"topics":["artificial-intelligence","celebrity","deep-fake","deep-learning","face-detection","face-enhancement","face-recognition","face-restoration","face-swapping","gan","insightface","roop","video-processing","webservice"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SocAIty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"SocAIty","patreon":"w4hns1nn","buy_me_a_coffee":"socaity","custom":["paypal.me/SocaityRieger"]}},"created_at":"2023-12-19T15:54:52.000Z","updated_at":"2025-03-24T04:50:14.000Z","dependencies_parsed_at":"2024-02-21T12:23:57.633Z","dependency_job_id":"f4ad64f2-7591-4638-b1e9-01f16d33901b","html_url":"https://github.com/SocAIty/face2face","commit_stats":null,"previous_names":["w4hns1nn/face_swapper_rest","socaity/face_swapper_rest","socaity/face2face"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SocAIty%2Fface2face","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SocAIty%2Fface2face/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SocAIty%2Fface2face/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SocAIty%2Fface2face/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SocAIty","download_url":"https://codeload.github.com/SocAIty/face2face/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226211,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","celebrity","deep-fake","deep-learning","face-detection","face-enhancement","face-recognition","face-restoration","face-swapping","gan","insightface","roop","video-processing","webservice"],"created_at":"2024-12-27T07:34:55.039Z","updated_at":"2025-04-04T18:03:39.869Z","avatar_url":"https://github.com/SocAIty.png","language":"Python","funding_links":["https://github.com/sponsors/SocAIty","https://patreon.com/w4hns1nn","https://buymeacoffee.com/socaity","paypal.me/SocaityRieger"],"categories":[],"sub_categories":[],"readme":"  \u003ch1 align=\"center\" style=\"margin-top:-25px\"\u003eFace2Face\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg align=\"center\" src=\"docs/f2f_icon.png\" height=\"200\" /\u003e\n\u003c/p\u003e\n  \u003ch3 align=\"center\" style=\"margin-top:-10px\"\u003eInstantly swap faces in images and videos\u003c/h3\u003e\n\u003cbr/\u003e\nFace2Face is a generative AI technology to swap faces (aka Deep Fake) in images from one to another. \nFor example, you can swap your face with Mona Lisa or your favorite celebrity.\n\nWith this repository you can:\n\n- [Swap faces from one image to another](#swap-faces-from-one-image-to-another). \n- [Swap faces in images and videos](#face-swapping-with-face-embeddings).\n- [Face embeddings](#face-swapping-with-face-embeddings): Create face embeddings. With these embeddings you can later swap faces just by using the name.\n- [With face recognition](#face-swapping-with-face-embeddings): Swap faces with face recognition.\n- [Face restoration](#face-enhancing): Enhance image quality of a portrait with a face enhancer model.\n- Identify faces with face-recognition\n- [Run face swapping as a service](docs/WebService.md).\n\n\nThis 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.\nThe face swapping model itself was created by [Insightface](https://github.com/deepinsight/insightface)\n\nWe provide the face swapping functionality as SDK and as a convenient [web (openAPI) API](docs/WebService.md) with [FastTaskAPI](https://github.com/SocAIty/FastTaskAPI).\nThe endpoint allows you to easily deploy face swapping, recognition and restoration as a service.\n\n## Example swaps\n| [Face-swap](#swap-faces-from-one-image-to-another)    | [Multi-face Swap](#swap-faces-from-one-image-to-another ) | \n|-------------------------------------------------------|-----------------------------------------------------------|\n| \u003cimg src=\"docs/example_face_swap.jpg\" height=\"250px\"/\u003e | \u003cimg src=\"docs/example_multi_swap.jpg\" height=\"250px\" /\u003e  |\n\n\n| [Video-swapping](#face-swapping-with-face-embeddings)                                                                 | [Video-Swapping with face-recognition](#swap-faces-in-videos)                                                                              | \n|---------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|\n| \u003ca href=\"https://www.youtube.com/watch?v=dE-d8DIndco\"\u003e\u003cimg src=\"docs/caprified.png\" height=\"250\" /\u003e\u003c/a\u003e | \u003ca href=\"https://www.youtube.com/watch?v=7l-3UAWh8Pw\"\u003e\u003cimg src=\"docs/trump_fighting_assassins.png\" height=\"250\" /\u003e\u003c/a\u003e |   \n\n\n| [Face restoration](#face-enhancing)                        | [Face-Swap with face-recognition](#face-swapping-with-face-embeddings) |\n|------------------------------------------------------------|------------------------------------------------------------------------------------|\n| \u003cimg src=\"docs/face_restoration_gpen.PNG\" height=\"250px\"/\u003e | \u003cimg src=\"docs/swap_with_recognition.jpg\" height=\"250px\"/\u003e                         |\n\n\n# Setup\n\n### Use the Socaity SDK\n\nSpeechcraft is available on [socaity.ai](https://socaity.ai) as part of the [socaity sdk](https://github.com/SocAIty/socaity).\nSpare yourself the installation and use the sdk directly. NO GPU required.\n\n```python \nfrom socaity import Face2Face\nswapped_img = Face2Face().swap_img_to_img(\"path/to/src.jpg\", \"path/to/target.jpg\").get_result()\n```\n\n### Or: Install via pip\nDepending on your use case you can install the package with or without the service.\n```bash\n# face2face without service (only for inference from script)\npip install socaity-face2face \n# full package with service\npip install socaity-face2face[full]\n```\nAdditional dependencies:\n- For VideoFile support in the webservice you also need to install [ffmpeg](https://ffmpeg.org/download.html) \n\nRequirements:\n- Python 3.7 or higher\n- Minimum 5GB of RAM (you'll get a \"Killed\" message without further information if you run out of memory)\n- Recommended: GPU with at least 8GB of VRAM for fast-inference. Runs also on CPU though.\n\nNote: Models are downloaded automatically\n\n# Usage\n\nWe provide two ways to use the face swapping functionality.\n1. [Direct module import and inference](#Inference-from-script) \n2. [By deploying and calling the web service](#docs/WebService.md)\n\n\n# Inference from script\nUse the Face2Face class to swap faces from one image to another.\nFirst create an instance of the class.\n\n```python\nfrom face2face import Face2Face\nf2f = Face2Face(device_id=0) \n```\nWith the device_id setting you can set the GPU device id. This also allows to run face2face in multiple processes on\ndifferent GPUs.\n\n## Swap faces from one image to another\n```python\nswapped_img = f2f.swap_img_to_img(\"path/to/src.jpg\", \"path/to/target.jpg\")\n```\n## Face swapping with face embeddings\n\nCreate a face embedding with the add_face function reuse those embeddings later.\n```python\n# create a face embedding and save it to disk. \nembedding = f2f.add_face(\"my_new_face\", \"path/to/my_portrait_image.jpg\", save=True)\n# Swap all faces in the image or video with the face(s) in the face embedding\nswapped = f2f.swap(media=\"path/to/my_img_or_video.jpg\", faces=\"my_new_face\")\n```\nIf 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.\nIn case you provide an image with multiple persons, you can provide a list of names and an embedding for each person from left to right is created. \n\n## Face swapping with face recognition (swap pairs)\n\nAfter an embedding was created, we can recognize / identify those persons.\nThen the identified persons can specifically be swapped with defined swap pairs.\nIf the faces argument is provided as dict, the swap function recognizes and swaps the face-pairs correspondingly.\n\n```python\n# Swap faces with defined swap pairs\n# This function will swap the faces of trump with hagrid and biden with ron.\n# assumption the faces [trump, hagrid, biden, ron] are already added with f2f.add_face\nswapped = f2f.swap(\n  media=\"path/to/my_img_or_video.mp4\", \n  faces={\n      \"trump\": \"hagrid\",\n      \"biden\": \"ron\"\n  }\n)\n```\n\n\n## Face swapping with a generator\nIteratively swapping from a list of images\n\n```python\ndef my_image_generator():\n  for i in range(100):\n    yield cv2.imread(f\"image_{i}.jpg\")\n\n# for swapping to always the same face\nfor swapped_img in f2f.swap_to_face_generator(faces=\"my_embedding\", target_img_generator=my_image_generator()):\n  cv2.imshow(\"swapped\", swapped_img)\n  cv2.waitKey(1)\n\n# including face recognition\nfor swapped_img in f2f.swap_pairs_generator(target_img_generator=my_image_generator(), swap_pairs={\"trump\": \"hagrid\"}):\n  cv2.imshow(\"swapped\", swapped_img)\n  cv2.waitKey(1)\n\n```\n\n## Face enhancing\n\nThe roop (inswapper) model operates on low resolution - what can harm the result face quality. \nHowever, there exist AI models, which can enhance the face quality by upscaling the image.\nWe provide different models for face enhancement: [gfpgan_1.4](https://github.com/TencentARC/GFPGAN), \nand the [gpen](https://github.com/yangxy/GPEN) family.\nCheck model_definitions.py for the available models.\nYou can upscale up to 2048 with the GPEN model --\u003e higher quality + higher runtime.\n```python\nswapped_img = f2f.swap(media=\"path/to/my_img_or_video.mp4\", enhance_face_model='gpen_bfr_512')\n```\nThe corresponding model is automatically downloaded and used when enhance_faces is set to True.\n\n### Face-enhancing without face-swapping\n\nAlternatively you can enhance faces directly without applying a swap. \n```python\n# enhance all faces in the image\nenhanced_img = f2f.enhance_faces(image=\"path/to/my_img.jpg\", enhance_face_model='gpen_bfr_512')\n# enhance a specific face.\ntarget_img = cv2.imread(\"path/to/my_img.jpg\")\ndetected_face = f2f.detect_faces(target_img)[0]  # In this case we simply take the first one\nenhanced_img = f2f.enhance_single_face(target_img, detected_face, enhance_face_model='gpen_bfr_512')\n```\n\n# Disclaimer\n\nThe 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.\nPlease use it responsibly and do not harm others. Do not publish any images without the consent of the people in the images.\nThe credits for face swapping technology go to the great Insightface Team thank you [insightface.ai](https://insightface.ai/). \nThis project uses their pretrained models and parts of their code. Special thanks goes to their work around [ROOP](https://github.com/s0md3v/sd-webui-roop).\nThe author does not claim authorship for this repository. The authors contribution was to provide a convenient API and service around the face swapping.\nA big thank you also goes to the authors of [GPEN](https://github.com/yangxy/GPEN) and [GFPGAN](https://github.com/TencentARC/GFPGAN),\nwho developed the models for face restoration.\n\n\n# Contribute\n\nAny help with maintaining and extending the package is welcome. Feel free to open an issue or a pull request.\n\nToDo:\n- Improve face swap quality\n  - Implement strength factor for applied face\n- Improve inference times\n  - by implementing batching.\n  - by using multi-threading in image_generators\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocaity%2Fface2face","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocaity%2Fface2face","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocaity%2Fface2face/lists"}