Unbelievable Face Swapping with 5 Lines Code

Rob Mulla · Beginner ·🎨 Image & Video AI ·3y ago

Key Takeaways

The video demonstrates face swapping using the InsightFace library in Python, allowing users to swap faces between images in seconds. It covers the installation of the library, face detection, and face swapping using the swapper model.

Full Transcript

recently I was scrolling through github's trending python packages as one does and I came across a library that looked pretty cool called Insight face and it's for facial recognition now I've done a video before on this channel about deep face which at the time was a state-of-the-art facial recognition python library and this one was getting so popular that I figured I'd give it a try not only does this package do facial detection and Analysis but the thing that really drew me towards it was its ability to do face swapping I'm sure we've all seen face swaps made in Photoshop where you essentially just cut out the person's face and stick it on someone else's but this type of face swapping is not like that it's really amazing so I figured I'd make a short video to show you the code that I wrote In order to get this to work and if anything this video is to show you how you can find open source packages that maybe Implement something that you're interested in and test them out on your local machine and give them a try okay let's start coding here's the website for insight face you can see they link directly to their GitHub page they explain basically why they're the best in the business and they've won some awards that sort of back that up so the first thing I did was took a look at their repo and you could see that they have some documentation that goes along with this code as well as some examples in a quick start guide now I'm interested in writing code in Python so we're going to be using their python package but there was a little bit of a hiccup that I ran into when I was trying to recreate this tutorial that they have and the next thing I did was created a new conda environment where I pip installed the Insight face package I like doing this for new projects because it keeps my environment completely isolated from all the other environments that I'm working with so just to test out this package I created a jupyter notebook and I made sure that I activated the Insight conda environment that I had installed inside face now I'm importing some packages that we'll use here CV2 and matplotlib can be used to read image files and to display them and then of course we're importing Insight face which is the package that we'll use to do the face analysis insightface has this app object for face analysis and this Insight git image is just a helper function that they created for downloading an example image just so you know what package versions I'm running I'll print the versions here for you and the process for doing this face swapping actually takes part in Two Steps step number one is detecting the faces and step number two is doing the swapping in order to detect the faces we're going to use this face analysis object that we imported and we're providing it with the name of the face detection model that we want to use in their documentation they go over the different different types of models that you can use and the one I'm using Buffalo L is one of the larger models so when you run this the first time it will actually download the model weights into this dot Insight face folder in your home directory and let's go ahead and test this out so I'm going to use this Insight get image function just to get their example image from their tutorials and I'm using matplotlib here to display the image now I'm taking the last channels here and I'm reversing the order because matplotlib actually plots channels in a different order than CV2 which was used to read this file I have a whole video about working with image data that goes into detail about that but we can see here this is just an image of all the characters from the TV show Friends and we're going to do some face swapping with this image so let's take this detection app that we created and let's run a DOT get on it and provide this our image this will detect the faces we do get some warnings here but it does run fine but if we look at what this returns it's actually a list of dictionaries and each item in that list is a dictionary for a face that it's detected we can even take a look at the length of the faces and we see that it's six which matches the number of faces we see here so let's take a look at one of the items in the faces and if we look at just the keys to this dictionary we can see we have the bounding box location of the face some key points the detection strength and also a bunch of landmarks as well as predictions for the gender the age and embeddings now these embeddings are going to be used when doing the face swap so it's important that we've run using a good detector to get the detection but just to make sure that it's cropped the correct faces let's go ahead and plot a crop of each face I'll make here a matplotlib subplot with six plots one for each face and we're we're going to fill this in with the faces so I wrote this code that essentially takes these faces and we enumerate over them then we take the bounding Box locations and slice our main image so that we could see what it's detected and you can see it's each of the Six Faces in our main image so this looks good to go we're almost ready to do some face swapping for step two we're gonna do the face swapping in order to do this we need to use Insight faces model zoo and we're going to use this in swapper model this model has a Onyx file that has the model weights and by default this tries to download the model weights but they've removed them luckily for me I found a Reddit post where someone had saved off these weights and I was able to download them I'll link that in the description if you want to give that a try so we can see now we've created our swapper object so let's choose one of these faces that we want to swap for the others so I'm going to take the first face the that we have here and make that our source face and if I plot the first face we can see it's Ross it's Ross's face that will be swapping in for all the others and now we're going to use the swapper so we have our swapper object and we're going to use dot get and Dot get takes the image that we want to get the face from we provide it the target face and the source face and then we're going to actually use paste back equals to True which means it's going to paste over in our image with that new face but we don't want to run this on our original image because we want to keep that intact so I'm going to make a copy of that image and just call it res this is the copy of the image that we're going to use and swap the faces on and we're going to want to do this six times so we're going to say for face in faces and then we're going to run swapper get on this image swapping the original face with the source face that didn't take long at all to run and let's take a look at the swap faces and just like that now we have our image with Ross's face taking over everyone else's just to show you that a little bit more clearly I wrote this code that Loops through each of them and plots them individually I just think it's interesting how his mouth expression is definitely different it even accounts for the face being turned to the side so of course I couldn't let it end there so let's try to do some swapping with my face so when I was doing the live stream I took a screenshot of my face which we'll just call Rob and to show what it looks like when I took the picture now we're going to detect my face from this image and swap it out with all of the friends faces and the steps to do this are just like before I'm detecting my face in the picture that I took I'm copying the original friend's image and then I'm swapping out my face and pasting that back over that image finally here I wrote some code that'll just plot this result and let's see what it looks like oh that's right I need to make Rob face equal to Rob faces just the first one and there we go my face has been swapped out for each of the faces and friends here and of course this doesn't only work for this picture of friends I took this Monty Python image and did the exact same thing to it and with just these few lines of code we're getting the faces from the Monty Python and swapping them out with my face and there we go it's actually swapped out my faces it even has the mouth open here and I don't know tell me what you think about how well it deals with the fact that there's a beard of this guy here in the center the next thing I did was created a function that would do everything in our process from before but make it a little bit more reproducible so I called this function Swap and show and you provided two image file names and then I have these variables that will plot before or after depending on if you have these as true or false this code is very similar to what we just did above it just assumes that there's only one face and it does the swapping to both images with the opposite face so to show how this works Let's test this on two sort of famous people in the machine learning industry and we're gonna swap their faces and see what it looks like I'm going to leave the plotting before and after shown so you can kind of see what the difference is all right here's the before picture and the after picture I also have the function returning the two images which I don't even want to show so I'm going to make that sent to an underscore that's just how you throw away the result of a function in Python now I'm surprised that this even handles the glass as well running it on Obi-Wan and Spock not too bad not too bad so next I wanted to swap faces of two people in the same image I just wrote a function that does it very similar to the last step but it assumed means that there are two faces in a single image and then does the swap now I called this function Swap and show same image and it only takes in a single image and the app and swapper objects I thought it'd be fun to test this out on Celebrity couples so I have four different couples pulled in here and I'm going to pull it through our Swap and show same image function that one's interesting okay before and after and finally so there you go I showed you how I used an open source python library to swap faces and you should be able to do it just as easily too if you've watched until this point hopefully you enjoyed this video so give it a like subscribe and send this to 20 of your favorite friends that you want to swap your face with let me know in the comments if you want to see anything specific in the future I'll see you all next time

Original Description

Easily swap faces between images using python with this tutorial on the open source python package InsightFace. This new python library allows you to swap faces in seconds. Github: https://github.com/deepinsight/insightface Reddit Post: https://www.reddit.com/r/midjourney/comments/13pnraj/please_reupload_inswapper_128onnx/ 00:00 InsightFace 01:10 Setup 02:40 Face Detection 05:22 Face Swapping 08:10 More Examples Check out my other videos: Data Pipelines: Polars vs PySpark vs Pandas: https://youtube.com/watch?v=mi9f9zOaqM8&feature=shares Polars for Data Science: https://youtube.com/watch?v=VHqn7ufiilE&feature=shares Speed up Pandas Dataframes: https://youtube.com/watch?v=u4rsA5ZiTls&feature=shares Avoid These Pandas Mistakes: https://youtube.com/watch?v=_gaAoJBMJ_Q&feature=shares Links to my stuff: * Youtube: https://youtube.com/@robmulla?sub_confirmation=1 * Discord: https://discord.gg/HZszek7DQc * Twitch: https://www.twitch.tv/medallionstallion_ * Twitter: https://twitter.com/Rob_Mulla * Kaggle: https://www.kaggle.com/robikscube
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Rob Mulla · Rob Mulla · 0 of 60

← Previous Next →
1 A Gentle Introduction to Pandas Data Analysis (on Kaggle)
A Gentle Introduction to Pandas Data Analysis (on Kaggle)
Rob Mulla
2 Exploratory Data Analysis with Pandas Python
Exploratory Data Analysis with Pandas Python
Rob Mulla
3 7 Python Data Visualization Libraries in 15 minutes
7 Python Data Visualization Libraries in 15 minutes
Rob Mulla
4 Kaggle competition starter notebook walkthrough
Kaggle competition starter notebook walkthrough
Rob Mulla
5 Kaggle Competitions: A Beginner's Guide to Winning
Kaggle Competitions: A Beginner's Guide to Winning
Rob Mulla
6 Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Rob Mulla
7 Audio Data Processing in Python
Audio Data Processing in Python
Rob Mulla
8 Complete Data Science Project!
Complete Data Science Project!
Rob Mulla
9 Make Your Pandas Code Lightning Fast
Make Your Pandas Code Lightning Fast
Rob Mulla
10 Image Processing with OpenCV and Python
Image Processing with OpenCV and Python
Rob Mulla
11 Speed Up Your Pandas Dataframes
Speed Up Your Pandas Dataframes
Rob Mulla
12 This INCREDIBLE trick will speed up your data processes.
This INCREDIBLE trick will speed up your data processes.
Rob Mulla
13 Complete Guide to Cross Validation
Complete Guide to Cross Validation
Rob Mulla
14 Easy Python Progress Bars with tqdm
Easy Python Progress Bars with tqdm
Rob Mulla
15 Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Rob Mulla
16 Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Rob Mulla
17 Get Started with Machine Learning and AI in 2023
Get Started with Machine Learning and AI in 2023
Rob Mulla
18 The Trick to Get Unlimited Datasets
The Trick to Get Unlimited Datasets
Rob Mulla
19 Video Data Processing with Python and OpenCV
Video Data Processing with Python and OpenCV
Rob Mulla
20 Object Detection in 10 minutes with YOLOv5 & Python!
Object Detection in 10 minutes with YOLOv5 & Python!
Rob Mulla
21 Pandas for Data Science #shorts
Pandas for Data Science #shorts
Rob Mulla
22 Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Rob Mulla
23 Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Rob Mulla
24 Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Rob Mulla
25 Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Rob Mulla
26 Solving an Impossible Riddle with Code
Solving an Impossible Riddle with Code
Rob Mulla
27 Do these Pandas Alternatives actually work?
Do these Pandas Alternatives actually work?
Rob Mulla
28 Time Series Forecasting with XGBoost - Advanced Methods
Time Series Forecasting with XGBoost - Advanced Methods
Rob Mulla
29 Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Rob Mulla
30 Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Rob Mulla
31 Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Rob Mulla
32 25 Nooby Pandas Coding Mistakes You Should NEVER make.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
Rob Mulla
33 DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
Rob Mulla
34 More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
Rob Mulla
35 Medallion Data Science Live Stream
Medallion Data Science Live Stream
Rob Mulla
36 Community Kaggle Competition Overview - Corn Classification (
Community Kaggle Competition Overview - Corn Classification (
Rob Mulla
37 Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Rob Mulla
38 OpenAI Whisper Demo: Convert Speech to Text in Python
OpenAI Whisper Demo: Convert Speech to Text in Python
Rob Mulla
39 Yolov7 Custom Object Detection in Python Tutorial  - Chess Piece Detection
Yolov7 Custom Object Detection in Python Tutorial - Chess Piece Detection
Rob Mulla
40 Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Rob Mulla
41 Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Rob Mulla
42 Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Rob Mulla
43 Flight Delay Dataset Creation (Data Science Uncut)
Flight Delay Dataset Creation (Data Science Uncut)
Rob Mulla
44 5 Reasons to Kaggle #shorts
5 Reasons to Kaggle #shorts
Rob Mulla
45 ♟️ Data Science - Chess Data Analysis
♟️ Data Science - Chess Data Analysis
Rob Mulla
46 EXTREME PYTHON & DATA SCIENCE LIVE STREAM
EXTREME PYTHON & DATA SCIENCE LIVE STREAM
Rob Mulla
47 What is Clustering in ML?
What is Clustering in ML?
Rob Mulla
48 What is K-Nearest Neighbors?
What is K-Nearest Neighbors?
Rob Mulla
49 LIVE CODING: Flight Data Exploration with Pandas & Python
LIVE CODING: Flight Data Exploration with Pandas & Python
Rob Mulla
50 Kaggle Survey vs. Twitter Sentiment
Kaggle Survey vs. Twitter Sentiment
Rob Mulla
51 If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
Rob Mulla
52 Data Visualization BATTLE!
Data Visualization BATTLE!
Rob Mulla
53 LIVE CODING: Stocks & Sentiment Analysis
LIVE CODING: Stocks & Sentiment Analysis
Rob Mulla
54 Progress Bar in Python with TQDM
Progress Bar in Python with TQDM
Rob Mulla
55 Flight Cancellation Data Analysis
Flight Cancellation Data Analysis
Rob Mulla
56 Synthetic Dataset Creation for Machine Learning - Blender and Python
Synthetic Dataset Creation for Machine Learning - Blender and Python
Rob Mulla
57 The Ultimate Coding Setup for Data Science
The Ultimate Coding Setup for Data Science
Rob Mulla
58 Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Rob Mulla
59 Data Wrangling with Python and Pandas LIVE
Data Wrangling with Python and Pandas LIVE
Rob Mulla
60 Forecasting with the FB Prophet Model
Forecasting with the FB Prophet Model
Rob Mulla

This video teaches how to swap faces between images using the InsightFace library in Python. It covers the installation of the library, face detection, and face swapping using the swapper model. By following this tutorial, users can easily swap faces in seconds.

Key Takeaways
  1. Create a new conda environment for isolated package installation
  2. Install the InsightFace package
  3. Import necessary packages for image reading and display
  4. Run a detection app on an image to detect faces
  5. Create a swapper object using the swapper model
  6. Choose a source face to swap with the others
  7. Use the swapper object to swap faces
💡 The InsightFace library provides a simple and efficient way to swap faces between images, and the swapper model can be used to swap faces in seconds.

Related Reads

📰
How I Built an AI Pet Portrait Generator That Turns Photos Into Art
Learn how to build an AI pet portrait generator that turns photos into art using deep learning techniques and Python libraries
Dev.to · William Li
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Learn how to use Google's Squoosh codecs in the browser to compress images before upload and reduce costs
Medium · Programming
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Learn how to use Google's Squoosh codecs in the browser to compress images before upload, reducing costs and improving performance
Medium · JavaScript
📰
Which AI tools can generate ready-to-use 3D character models for games, animation, or 3D printing?
Learn about AI tools that generate ready-to-use 3D character models for games, animation, or 3D printing and how to evaluate them for different use cases
Reddit r/artificial

Chapters (5)

InsightFace
1:10 Setup
2:40 Face Detection
5:22 Face Swapping
8:10 More Examples
Up next
VIRAL AI Videos 🤩 New INSTAGRAM TREND 🔥 Baby Kids Videos #ai #trending
Raj Photo Editing and Much More
Watch →