Face Swap AI in Python! Live Coding.
Key Takeaways
The video demonstrates face swap AI in Python using the InsightFace library, covering topics such as face detection, face recognition, and image processing. It also explores the use of OpenCV, numpy, and other tools for face swapping and image manipulation.
Full Transcript
hello hello hello can you hear me okay it is Tuesday June 6 2023 and we are back in the building how's everyone doing today we are Live 09 damage welcome to the chat and I hope you're having a good time I need to make sure that I mute everything I'm listening to cuz otherwise I get infinite Loop in my ears okay there we go my favorite is live oh I'm your favorite are you just mentioning that someone else is streaming they're your favorite but I also seem to be streaming Hao we have memes meme Master Pro welcome to the chat let's give people a second to arrive by the way hey if you're watching from YouTube let me know what you think about having the twitch chat appear in your chat is that too much is it confusing I'm your favorite data science slml engineer oh thank you zero n damage we got Steve Bodi we have Jam shed uh we have we have Jamshed in the chat we have someone from Brazil Louise welcome okay so what are we going to do today we are going to do some live coding and we are going to try to test out some open source software now let me show you a little bit of what I've been looking at okay so you might have noticed this from the title uh but I thought today we could spend some time let me get this out of the way hold on hold on cleaning up my screen the thing is I was trying to do this offline beforehand and I was running into some issues so we're going to deal with this together it's going to be a team effort we're going to take it on and we are going to win I know it hey from the Ivory Coast welcome welcome Jamal from Ghana welcome z z z zis what welcome ASP aspiring data science from the Philippines cooled I actually pronounced it well Bodi bosi well I think yeah you've been watching for a while so I I think I've I've got it a little better than if it was my first time just call me Z okay Z all right so I was looking on I don't know if you guys knew this but you can go on to GitHub you can actually look at what is trending it's kind of like Twitter trending but not as cool and you can choose the language like the spoken language you can choose the language which you code in that's python for me and I just picked this past week and I noticed that we've done facial recognition stuff on the stream before and what I would like to do is take a look at this this one because it is just blowing up um from Deep Insight Insight face it's a face analysis project so um uh we have someone from Iran what's up so going over to Insight faces page so if I load this up it looks like they have a website they have been updating this recently so as of 4 days ago go there's been updates to this code base let me turn on the dark mode for you guys you can tell me if this is better oh wait you can't read anything okay so it's been updated four days ago and um it has 14 15,000 Stars that's a lot of stars I think it's because and someone can correct me if not but I think it's because this they have like a Discord bot that you can add using this which is pretty cool um but it's an open- Source library for doing things like face identification it says it does it in 2D and 3D it says it's based off of Pi torch and we can actually go to their website to kind of get a be better idea of what we're going to get ourselves into um so they have a lot of updates maybe this will bring us to their website all right so they need to update this because it has more than 13.9 th000 Stars it has over 15,000 stars now what are you guys saying in chat whater version do I use I just I upgraded not too long ago to 2204 2204 is that right or 06 whatever it was are you part of any kago competitions right now not currently not right right now I was attempting one and I kind of gave up um all right so why Insight face Insight face is a integrated python library for 2D and 3D face an analysis we like when things are python libraries because then we can kind of work with it and write code around it right insight face efficiently implements a rich variety of state-of-the-art algorithms for face recognition face detection and face alignment so we I already on one of my streams looked up deep face we did I actually made a whole uh YouTube video about it if you look here this is my YouTube video about deep face which was at the time a state-of-the-art uh face detection face recognition package but since this new one has come out and it's so popular I guess it's not that new because it has some commits that like a year ago uh but it did win all these Awards look it got first in the E eccv 2022 wcpa challenge what's that Challenge from Face Body and fashion to 3D virtual avatars 2 okay so it's some sort of image-based face detection competition and it got first place in that ranked first in all these things uh going back to 2019 it's also free and open source so uh they do have some web de uh demos here uh the thing that looks the coolest to me honestly is the face swap so I'm going to put that in the chat for you guys so you could see it you can follow along with this but uh the their web demos have insta face swapping and we want to do this locally so I was running into some issues when I tried to set it up locally and maybe you guys can help me with this uh it might be something where I actually have to go into their code and change something so let's go here let's go here and uh actually I'm going to start a new terminal for you guys I'm going to activate my uh what's this thing called Insight cond environment and just to show you a cond list this will show everything that's installed here so I basically just started a new python environment with a 3.9 python in installed so if I go do python version we could see it's 3 3. 9.16 um all the default stuff that's installed but also this Insight face package 0.7.3 maybe that's the issue I was running into but let's see if they have a releases if they have release version releases releases add Buffalo model on April 2nd um is that really version 0.7 I don't think they're maintaining their versioning I also noticed that I was looking at their requirements.txt and this is kind of sparse not only is it sparse but it was updated a year ago so the issue I was running into and we'll see this here in a second was with numpy we'll jump into that in a few seconds okay so what do we need to do to actually try to get this running we need to make sure we've installed face Insight which we have I'm going to just install upgrade face insight all right doesn't seem like that's working let's go to their they they don't have great documentation um at least for what I'm trying to do single code for facial identity swapping in our python package version 0.7 why 0.7 though when I do this it doesn't say it's 0. 7 oh 0.7.3 okay fair enough all right so pip install upgrade it's going to say it's already installed right all right so everything's already installed second download the iner doomx walking model from Google Drive and put put it under Insight face Dash models um from Google Drive but this link doesn't take me to Google Drive it takes us to this exact same page for a detailed code example we need to use this okay let's see what this is doing folks nice haircut oh thank you big Larry you must must have not seen me in a while uh Bel thank you for saying you love this I love you you love me and we're a happy family okay so we're going here what are we doing some imports now here's the problem before is numpy ooh numpy Co core multiarray fail to import so let's let's go I'm in my insight cond directory I'm going to install upgrade numpy just to be sure so I tried downgrading it before and that didn't work but I'm going to upgrade it to the latest version which is 1.4.3 versions are always annoying and then we're going to try this I think it's going to fail still but we'll see all right so here we go so if I print my uh Insite face version I hope this is going to work and we print our numpy version all right let's see if this works so what their example code does is it asserts that Insight face version is greater than 0.7 which it is this is weird though because this is going to be oh this is a string can you do greater than or equals with a string yeah I don't think this line of code is correct right when you need to like wrap these in floats and then you need to like split this on the periods and then do everything up until the second and then you need to this is getting crazy then we need to join this that's not going to work oh wait it does okay so this is I think what they wanted to do but just so that's the version number that's like the base version number all right so that works we got the assert verion uh your English is is perfectly perfect and smooth I can understand every word in other videos I don't understand this love from India all right hey I try to be clear all right so this is the main function of this but we're just going to pull all this stuff out here uh so face analysis it's going to use buffalo L I think it might break here but let's [Music] see there's a problem fail to download this Onyx zip file there is no deep Insight releases V7 face swapper come on we want to do some face swapping let's just go to the releases SL download okay there's no releases uh let's look and see if there's issues Onyx helper err three weeks ago um Onyx is used to like transfer models from different architecture like Pi torch to to tensor flow um so let's see what's going on here what basically what it's trying to do is it's trying to download from this URL I would love to see if this file exists still somewhere in this repo um can't download okay so there's an issue from 28 days ago this is coding in real life guys hi Mr kagler hi n n did I say that right Nal okay so they're trying to use the model Zoo to download this Onyx file cannot download cannot load zip file manually through this ring get Hub respond not found plus one swapping Deno has been moved to Discord bot that's awesome so they're saying that they moved it to their Discord bot only and that's why it's no longer supported through the python version this guy's trying to do the exact same as me that's awesome but I want that model to test my photo locally can you share the model somewhere 128 does not perform as well as it a Discord version at present the ability to run locally see seems like a good reason let's give this a a thumbs up there we go we thumbed that up but seems like Discord app is down at the moment the mean uh you can te test your image by using the Discord service oh okay so maybe they closed all right so as of yesterday they closed this issue or they closed it last week we might not be able to run this locally we might only be able to set this I wish someone had just saved it are we just not going to be able to do it has anyone out there saved it so much for the open source version should we try to find the Discord version all right let's see what this swapping Discord model is about all these licenses okay so what they're saying is if you wanted to do a face swap with mid Journey you couldn't really do it because you're not a celebrity I'm assuming none of you watching are celebrities so you couldn't just ask mid journey to create you looking like Cleopatra whoever that is but they're saying what you can do is use mid journey and then swap your face in uh we have to find so now now we're just like adding a bot to our server uh that's not as fun let's search the internet for this for this Onyx zip file people on Reddit are asking for it did someone download the in thing oh look 10 days ago someone saved it to their they saved it to their local version thank you guy on Reddit thank you to to 2D everyone should go and up vote that guy on here on Reddit hello Kai welcome I'm here for first time your shorts video so so what about what time do you teach live I want to join your live lecture well this is it this is it belal we're doing it on Tuesday nights sometimes Thursday nights some sometimes Sunday nights Nando welcome from Brazil you say that's awesome I wish I could get this little chat thing to be bigger on my screen if I stretch it out it just does that but I appreciate you all right so we were able to hack our way around that so what I did was if I go into my repositories to my twitch I like this music it's getting me pumped up and then I go to I already made this Insite face directory I'm going to move from the downloads this Onyx file this ins wrapper Onyx file to here it's 529 megabytes hopefully it was done downloading there all right so now we have the swapping Onyx file and we should be able to follow this so we were able to create we were able to create this app with the face an analysis using Buffalo L I guess that's face recognition prepare it the detection size I think that's in pixels 640x 640 then we're doing uh this git model download is false now download zip is going to be faults because we're downloading as faults we we can actually look at this code base to see what it does and maybe maybe it'll load it locally all right so it it I think that worked we're going to make our swapper object there we go so we followed everything up until here this has been done okay we have two objects we have the app which does the face analysis we have the we prepared it and what's this CTX ID uh I don't know it looks like there's also a detection threshold which we could set but we did not um so let's go to let's go to the next stuff so image get image so it's going to P it pulls down this image which is of shape 886 by 1280 does m do we have map plot lib installed import map plot lib P plot as PLT I am show this this image all right so it's got the channels it's got the channels will this work yeah so the the red green blue channels by default are different when you import it with CV2 so we just flip that around but we can see it's a picture of friends you guys are probably too young to know this show in chat tell me anyone just write y or n for yes or no y means you have seen friends live like when it was released when it was released not like a re repeat but when it was actually on TV all right we have some y's and N would mean no you've never seen it all right I'm expecting to see more ends all right so this is our image delete those put this into my imports let's see what it is n y and can someone tell me what the n27 does where the where's the n27 where where are we talking about the n27 I need to find where you're talking about oh are you just talking about the cell number so confused well I'm surprise a lot of wise so some people have seen uh they're they're actually older bunch here we're we're a bunch of old fogies here um so we pulled this images all right so let's see if the app can actually get the faces all right it it it succeeded so what did the faces app actually do so what we're doing here is detecting the faces we're using Buffalo L model let's also look up Buffalo L face detection it's just going to show me a buffalo's face yeah why is don't waste money on face recognition none of these actually have Buffalo l in it can someone in chat look up Buffalo L I guess it's a large Buffalo model Medallion stallion pasta Carana or pasta balen B the second one second one you're ordering dinner I'm assuming all right so what does our faces have it has a bounding box uh so it's a l with the length of six so it found 1 2 3 4 5 six faces let's go to faces the first example let's go to the bounding box it has a score of 0.9 that's pretty good it has all the landmarks it looks for pose and landmark 2D okay so it does it looks for landmark in 3D this is kind of like U has it looks like that has 6 d8 and in 2D it has 106 this is very similar to um face mesh which we've used before right media pipes face mesh kind of does this um 500 some 400 some whatever it is it's a lot of dete Ed points and it they also detect them in 3D um but this might be a faster model I don't really know difference is doesn't really matter um all right so here's the embedding also it's wow it's detecting age and gender so this looks like it's doing a lot what is this model app um when it downloaded this model Buffalo l I would be interested to see what this model is H it's just a someone complaining about Onyx eror all right so we detected faces I could write all the code to take this and try to like put a box around the faces manually do you guys want me to do that like we could go through each one take the bounding box uh we could take this image no you don't want to see me do it yeah I feel like I've done that on stream a lot before for other stuff like we could take this image now watch me like start to do it um you could I think we would have to take the find the upper left corner or something and then could compute a height and a width and then we draw the boxes and basically what we would get out of it is what this Insight face does when it detects these I would love it if they showed how they displayed the faces um but let's just skip that let's just ass assume it's perfect apply mask oh some people do want me to do okay so let's do it like this I guess the easy way to just plot part of it would be all right so way the way this image is if we know understand the shape right it's um height WID and the number channels so what we want to do is basically take this example take all of the channels but for the faces for face in faces and we'll break out of this we'll take the face bbx and make this our bounding box we will take the bounding box and we'll take it from see this this is where I don't know which is the like it could be this this slice indices must be integers or none wait this is these should all be integers zero okay so they no they're not integers now I converted them to be integers and let's just keep this name bbox not beatbox but bbox all right so this returns nothing probably because I'm mixing up the that and that no it also could be like this oh this the two and one two three bro that is not right that's someone's leg that's someone leg someone's leg so I think what we need to do is this there we go after this session please upload this video I'm not able to join this time because it's oh it's it's early in India yes absolutely I'll I'll keep this up on YouTube all right so we found that was painstakingly hard and I probably should have just thought about it more but I was able to convert the detected uh I don't know if you understood what was going on but oh we also want to invert this so we converted this from a float the bounding box is were in floats but when we slice the and these are the pixel values within the image where it's detected the face uh face detection other object detection does this a lot right um so we're going to make it really cool we have we have six faces so fig axis uh we're going to make a subplots of one by six oh this song is crazy we're GNA put it for index in how's the music level by the way all right so we are nue numerating over the faces now we have an index location we can now take this axes and we can make sure it goes into the Associated location um um I forget if this like axis off yeah there we go all right there we go do you see how pretty that is when it finally works all right so what do we do so this detected faces from our image then we looped over each face detection we pulled out the bounding box of what it detected it wasn't a float so it wasn't specifically exact pixels I don't know why it wouldn't do that because it's it's not like it knows anything more it maybe because it does by percent of the height and width and it does some multiplication anyways we just converted that into integers then we sliced our images in order to plot them now they're not all exactly Square you can see when it does the detection sometimes it's um sometimes it's oval or rectangular sometimes it's Square um now let's see what they're doing so now they're taking oh this is cool this is cool what are they doing here sorted faces Okay so all they're doing here is they're sorting by faces I think left to right cuz they're looking at the bbx location of each so they're just kind of looking at this uh and I think this is just sorts sorts left to right all right so let's let's call this face swapping face swapping time we have our faces now that should be sorted yeah so if we look at like the bbox zero um this is just going to confirm what we're what this line of code does so if we run this above code now we have our faces which are not sorted and then I run this line of code which will just print the bbox zero location which um I think is the left how far it is on the left of the image like how far to the left it is and if I print it beforehand I mean it is incrementing and anyways so then when we do this it just does it just keeps it the same I don't know I don't get it maybe because they want this to be reproducible and they want to make sure that for some reason it doesn't change uh music is low okay I could turn up a little bit it's kind of intense music all right so we got the faces we now we're going to make we're swapping time so this this line doesn't really do anything so now we just take this image and They're copying this image like this makes sense if you keep in mind in Python all you new python programmers out there why why use copy here it's because when you assign a new uh variable name from something else like if we take the image and we're going to make it called res if we didn't add this copy and then we modified Rez like we were going to pretty soon and swap the faces it would actually have modified the original image which wasn't wasn't our attention so we're this is basically ensuring that we don't mess up the initial code all right they're just asserting that the it there are six faces which we know it's already true um then they're making the source face face number two and if we take the source face and we plot it we can confirm what it is so this is the source face which is Ross right isn't that Ross correct me if I'm wrong all you friends fans out there uh that stmp you for a silly amount of time yeah it's a good thing to learn at first especially when you're working with things like numpy arrays pandas data frames a lot of data in ensive stuff you think if you're a newbie you think oh I can just make something else equal to this and then now I have a new thing to work with it's not true unless you make a copy and um by default yeah this will return a copy of the array okay so whoa this this music is all right so we're going to take Ross's face and we're going to swap it out with everyone else's face so for each face in the face swapper or sorry for each face in this image we're going to use the swapper to get that face and swap it with the source face let's look at the documentation there's no dock string for this but we can look in the Code bait for INS swapper whoa this is intense music okay so swapper get so there should be a get method get method in here I can't complain that they didn't write a doc string because I don't write doc strings a lot of time but um so they're doing this normalized crop not sure what this does come on GitHub I don't want all this stuff uh reshaping it um this is where they get the embedding of the face whoa changing the do product of the latent space divide the latent session run image fake it almost be interesting to see exactly what's going on in each of these all right so if paste back equals true equals false then it will just return this otherwise it does a lot of stuff gaussian blur wow I mean I don't know what's going on there that's why this has so many likes on GitHub okay so we are doing faceback equals true and let's see all right after SW swapper doget this is our result we're going to show this all right we want to do this it's already looking hilarious um all right so it looks like they're all swapped right they're all Ross they have been swapped and it's surprisingly good that is that is actually really impressive it's got the smiles still but it has it it has has his face for sure it has like his facial features I am very impressed by this but then what is it doing here it's writing it and then I don't understand what this version does so now they're making a second version of the swapped and we can see how this works so this is swapping one by one and then concatenating the results by the way is this running on the GPU that's just OBS using it so I don't know if it's running on my CPU maybe all right so let's show this second version okay so this is just showing this is just uh paste back paste back just Returns the face that detected so this is just for them to show that example of the faces being swapped put your face face on it you were one step ahead of me sir let's do it let's put my face on it uh so how do we want to do this we can use CV2 here here's the here's the simple way to do it let's just do this this is me uh I got to take my glasses off to make it more fair I'm going to take a smile I can't really see my screen right now uh go to screenshot GNA select look at the camera all right now go back to this you can see my screenshot of myself I'm going to save it to my desktop and then I'm going to move it now I got to put my glasses on so I can see man it sucks getting old I'm going to go to here I'm going to move from my desktop uh screenshot here and now if I go to uh CV2 I am read all right let's also delete this stuff below it because that was just the example code put my face on it I'm going to read I'm going to read this file um screenshot I'm G to call this Rob right so we're going to do this and that's picture of me it's got the chat even in it no it's got the other stuff uh time to do it Rob's gonna rob their face yeah get ready get robbed get ready to get robbed so what do we do here what do we do to f swap me onto the friends faces we first need to detect my face so first we're gonna detect and save my face so my face all my in Bings are going to be on Rob faces Rob faces should just have a length of one we could make this assert that it's only one face for me right so then just like they they created this Source face it's just going to be Rob face um so if I if I plot Rob face this is what it's going to look like so I'm going to make this Rob face Rob face equals Rob faces I know this seems kind of weird but it's just because there could have been multiple faces I could wait no wait no no no no no oh yeah that's cuz I'm showing it I'm not showing it on my face there we go there's that guy we all see him what if it detects Small Faces in chat that would be impressive that would be impressive all right so my face is there now what do we do I mean it's pretty simple we just do this swap the faces all right so now oh sorry it's not that simple number two we're going to detect the friends faces again just to have this complete uh we need to run this right now we have the friends faces and then we're going to uh swap my face for theirs on the image so then what what we need to do first is make res as the image and remember we're going to do copy here right cuz copy helps and we're going to Loop through their faces and we're going to get their face and replace it with Rob face paste back is true and then we're going to actually show it all right chat I you guys have a you guys have a um an assignment right now and that's to go find some good images out there for us to do face swap on it's all I'm asking of you whoa he who's that Chandler yeah he uh he didn't change that much it looks a little bit chandery but these are pretty good face swaps oh my goodness let's do this version of it let's do this version of it so if I delete this and then we do the [Music] same close up view right so we're going to do this it's going to concatenate the results and we're going to show all of these this one is like how did they get my mouth to open how are they getting these Expressions out of just a singleshot view of my face now this one got to admit is that Rachel it's not the best this this one's not the best like the forehead's kind of huge this one definitely looks like um like they phoned it in but I can't maybe you're just very chandlery yeah maybe maybe all right someone's requesting this oh this is great uh let's put this on my desktop too oh this is Av if file I thought that looked too high quality what the what is a AV if file hey by the way you guys have the new Google search with generative AI is anyone out there have that this or am I the only one an avif file is an image file that use Avi image [Music] format it's like the chat gbt right in your search sometimes it's pretty bad though watch this write a function to M multiply two numbers and subract three subtract two all right so now it doesn't want to give me generative a AI it just gives me results did they remove the code part of it maybe that's something they decided like python function add two numbers okay so they showed it here so this this works but I've seen some some results that are pretty bad from it yeah one you weeks ago I use it sometimes when searching for a coding term nice yeah okay so we have this avif file that we want to convert to a let's just open this folder what's this going to open in let's export this export as JPEG now we've got it as a JPEG which I know open CV can handle man I'm really happy that person provided the Onyx file for this because otherwise we wouldn't have been able to do this thank you guy un Reddit um so swap with Monty Python all right we're going to read in the jpeg version of it Monty we're going to show it all right so this is the existing one let's see if in a real world example it works that well how do you like the Google lab search results so far me personally I'm not impressed I'm just not used to it like even the font changes get to me like confused me a little bit all right guys I finished my blue moon now it's time to go full summer white claw then I'm done then I'm cutting myself off all right switching with Monty wait I already said that all right so we have that image we already have Rob face so now we basically just need to get the faces I could write functions for these we're just doing it quick and dirty so we're going to take the Monty Python image get the faces from that oh let's assert that the length length of faces equals okay this is going to be interesting cuz how many faces is it going to going to detect in this image there's actually one two 3 four five main faces but then we have like a face back here we have these little ones over here so we just kind of want to see what these faces look like so let's do this in a separate cell um so it detected the faces now if we look at the length of the faces there's nine so which nine did it decide to um to do let's uh let's do this this code again let's do this code again which we wrote that can plot all the just the faces pre swapped pre- swapped faces we're not going to do this because that's not our image and this is going to be on the Monty image um and oh yeah our length of faces is nine it's not six so this needs to be nine and let's see okay so it detected the main faces that we care about it also detected this guy's face what are these These are these three three guys that's pretty impressive it found all the it found even these faces it it decided to ignore the Angels up high but I'll go with it well we can even try to swap our faces on like these five pixels back here I haven't had a white H Keenan says I haven't had a white claw in a while you should really you should really give it a try swallow your pride and enjoy it it's some it's summertime I mean if this was December and it was snowing outside I could not drink a white claw but it's sunny till like 8:00 it's warm outside granted it's night time now but you know I just you got to do it all right so we're not going to swap yeah now we're going to swap faces so now we get the Monty Python faces we're going to copy this here we're going to swap the faces paste back and we're going to show it should also time it and I should also make sure is it running on the GPU it it's running on my CPU you see this there's got to be a way to make sure it runs on my GPU there we go pretty darn good it even deals with the beard it deals with the beard magnificant magnificant I can't speak it could have been better but hey yeah look at this it's got my mouth open in awe this is I mean that's fun face swapping people we're face swapping all right let's take two people who do not seem like they they would go together two people that don't go together and swap their faces any ideas we could I don't want to go polit too political but we could like pick political rivals Insight face is very strong I have a such difficult time fine-tuning it to my data um what are you trying to do specifically I'm I'm interested to know Kenan try Tyrese and Emma Stones I mean that's going to be we that's going to be weird I don't know is there some his like is there some is there some drama between those two or did you just pick people yeah the skin color difference is going to be interesting Kanye and Taylor Swift H I don't want to get too controversial here Kanye's been in the news for a lot of stuff I don't want to delve into that territory um but let's do yeah I mean I don't really know Tyrese Gibson or Emma Stone for that matter lron oh him vers Gary Marcus Mike Tyson and Brad Pit that's a good one all right so Yan is like very uh bullish I guess on like AI future is Gary leso let's do this I know they're a thought leader guy [Music] lron both prominent figures field of artificial intelligence have made significant contributions however have different perspectives their areas all right so CNN's versus being critical of deep learning okay let's do it let's take the images from here uh let's just find this directory Insight face it says licensed image oh shoot I didn't know it does that when you download it makes me feel bad I don't want to download the full HTML no I don't want the full HTML going to call this Gary Lun going to call this okay how would we s how would we swap two faces and retain the two images forest riter and Bruce Lee oh that's a good one or just yeah Spock and Obi-Wan oh that would be good back in college I had a friend who was really good actually have a friend now who's really good at doing the face swap and like editing stuff that will be out hanging out and he'll take a picture of something we were just talking about and like paste my face on it and text it to a group CH chat yeah so I think we should make this into like a generic function but let's just make sure it works make it more wide than tall show the first image turn that AIS off and then we'll do the same thing but to the second image and we'll show this that's right we need to when we show these invert the red green blue channels there we go so now we have image one and image two we're going to swap these faces we're going to swap these faces by first detecting the two faces right so we need to take our we need to take our detector app to get the face one let's just assume it's only going to find one face in these examples and we'll give it image one and image two that's going to be our face one and phas two uh we won't worry about plotting them and all that stuff we're just going to go ahead and swap we're going to make a image one underscore and an image two underscore with this will be where we swap the images so let's think about how we do this we take image one and we're gonna remove face one and we're replace it with face two and then we're going to do the opposite right we're going to take image two take face two and swap it with face one and then we're going to go ahead and do this plotting function again but with our underscores there we go there we go people we have successfully swapped two faces am hey Rob hey what's up am I like your username some of these names I don't recognize like MBA in hland but I like the Spock in Obi-Wan because they're two iconic people you know and that people get frustrated when you mix up if you're really into that thing so let's save this as Spock oh this is a web B image do like that image though so we'll save Spock uh you're interested about the skin colors let's try that one too let's try that one too Obi-Wan Kenobi you're my only hope well this this is like the young Obi-Wan do you want to go with the old Obi-Wan feel like this is the fake obi-1 they have like no pictures of the OG all right we'll go with it I guess that's what people think of when they think of Obi-Wan these days all right so the problem here is that I'm G to have to convert this web P image so there's some easy way to convert this online con web P2 jpeg image converter uh repost repos stream projects no I clicked on the wrong one now it's trying to convert uh notebook which doesn't make sense okay here's the Spock one Spock upload let's go convert download Spock go back here to our code and now we're going to run it on Spock versus ob1 and we're going to make this into a function just to make it easier Swap and show we'll call it swap swap and show image one file name image two file name so now we're going to just move these out then we'll say like plot PL before and default that to true if plot B4 we'll do all this then we'll do the swap do the swap then we'll [Music] and then we'll if plot after is true we'll do this plotting and then we're going to return the images that have been swapped now there's one thing here this assumes that there's only one face in each image uh this function only works if there's one face please add dock string oh just for this quick function that we're writing okay uses face swapper to swap two image phes in two different images plot before if true shows the images before the swap plot after if true shows the images after the swap returns images with swapped faces assumes one face per image all right so Swap and show uh we're going to do image one file name which is ob1 image two file name which is Spock baby Spock baby and we're going to show them oh man that's weird that is weird oh yeah and then we're also return this as underscore because we don't care about that I should also have it provide our app I mean because that wasn't created in this function and we should also provide it the swapper just to be just to be nice and I didn't talk about that in my dock string I apologize what is the library for face whopping read the title of the stream not to be mean but I think it's in the title of the stream it's called Insight face and it's uh open source library now it's not really open source weights because they decided to take those down but some guy in Reddit posted the model weights and thank you guy in Reddit we downloaded those and we were able to run the old code yeah so here we are we swapped some faces now someone wants to do it with different complexions it would be interesting to see how it deals with different complexions now before I do this I want to I want to just put this out there we're adults I hope we're adults you need you know if you're a kid turn this off but we're adults here we're mature we're not going to like be immature about the fact that we're trying to swap faces with two people with different complexions we're just doing it as an experiment to see how the model performs okay I hope you add the link from our Reddit guide to YouTube post yes the other thing too is I now have that those models downloaded locally and I ain't deleting them I'm going to keep them I don't whose model whose model even is it I don't know um okay uh I just realized you guys can't you guys can't see him let's see how it works all right so let's swap let's swap um I don't know I feel like it is bad territory but I feel bad if this guy uses his primary Google account isn't there a bandwidth limit on Google Drive now I mean I probably not probably but also there's not that many people who are downloading this thing maybe there are um back in the day I had a podcast which I hosted on Dropbox before Google Drive existed and Dropbox it got popular enough that Dropbox eventually shut it down but it took them a few months it's like they never they never thought that I would host publicly like something like like that can can it swap animal faces yeah that might be well I don't think it's going to detect an animal face CU these models were trained to DET detect all right let's just call this swap faces try again can you feel how un comfortable I am um so who should we do Chris Tucker and oh yeah that's a good that's a good one that's a good one because those guys are in the same move movie together and Jackie Chan these are my guys the these movies are hilarious um Rush Hour wow there's a fourth one when I say these movies were hilarious I only know of the first one all right so this is a web p so we might have to modify our code to [Music] handle two people in the same IM image so we'll do like a single image swap um let's open this directory oh yeah we need use our little swapper tool our web P to image converter let's go back to the main page of this Rush Hour upload I guess I could have just given it the URL choose file Rush Hour Rush Hour select upload there we go Jackie chin Chris Tucker uh and this is Rush Hour um going to our code all right so we're going to have to mod modify this Swap and [Music] show same image the same image found this channel for my YouTube video which hey welcome Master student electrical engineering Computer Engineering I I got my masters in electrical engineering we're like two P's in a pod oh but you also did Computer Engineering and you're doing machine learning that's awesome what video did you watch that brought you here uh I still feel kind of uncomfortable about this okay okay so we're reading image one image one now we don't need to do this anymore if plot before we're just going to make this one by one and then this will just be the single axis we plot it in then we're going to get the faces in here and then this is going to be face one and face two equal faces zero and Fa faces one we copy the first image we're going to swap face one with face Two Face two with face one but we're going to do it always on image one then we are going to plot this like that a little bit of this a little bit of that all right rush hour okay so this also needs the app and the swapper key errors zero oh that's right cuz right here this isn't faces this is just a single face what it didn't didn't swap them so I did something wrong image one oh oh it's plotting the wrong one Swap and show same image okay like it's trying to it's trying to deal with it watching the video about cross validation nice you're watching the video that didn't didn't get a lot of views but I I'm very proud of I'm proud of that video yeah I don't know what to make of this it's kind of strange gotta be honest I this makes me uncomfortable I mean the same goes with like swapping yeah guys and girls hey Rob hey what's up Mr Gabriel how you doing it really helped you I I'm glad cross validation one of the most important important things in machine learning came here from the cross validation video too nice let's see if cross validation video comes up if we Google it doesn't it's way down here but yeah this video I guess it's it's like a slow burn starting to get more views I guess but right after I released it like no one really liked it right away I guess it's sort of thing you need to be learning about uh HTI what's up welcome to the chat um all right let's do one more swap feel like I could swap like my wife and I's face or something um top celebrity couples all right here's some celebrity couples can we swap their faces thanks for these videos thanks for watching subscribe tell a friend they'll tell another friend we really need to test the animal swap I'm fairly certain it's not going to 09 we can test it but I'm fairly certain that the face detection is just not not going to it's going to say that's a cow it's not like a cow face uh image of a cow like if I save this please be a JPEG all right if I save this here cow and I do the test on cow it would be hilarious if it was like I guess that's what you could fine-tune the model on um but it would be hilarious if it could get animal faces so this is going to be our cow right no way it finds a face on here so app app. detect let's see what the function is that we detect app.get yeah it returns it empty list so just to just to prove it to you I was wondering if cross validation is used when fine-tuning a model so I don't know it kind of depends like for a lot of the fine-tuning of models you have like a base model that you know goes really well and then you're it's usually deep learning stuff where you have a lot of labeled data um but you could the thing about cross validation 2 is the larger your data set the less necessary it is because as long as you believe you can do splits that are representative of what your model will see worth a try yeah it was worth a try Okay swap faces of celebrity couples let's take some images of celebrity couples this is going to be great who is this and I'm going to need some help because I don't know all these celebrity couples like who who are these people I wish the image name had their names in it I'm going to call this couples one oh you got to get wait Jennifer Lopez and and uh P Diddy had a thing H it's a web P can I enforce that it's a JPEG oh yeah let's let's save this no it's webp got to get their full face too who's Bieber with couples to would serve a great meme yeah I need to get a good one of um need to get a good one of these two they're always kissing though these two are always kissing like do you think this is a good enough one to get a it's also definitely a webp yeah I like I could just start taking screenshots um what's that guy's name hi Rob big question I was thinking pursue msse in AI because I like industry application instead of the academic world the thing is my teachers recommend me that it's better apply for a PhD even if you want to dedicate my life to the industry what do you think about based on your experience I don't know I think that doing a PhD should be more about like you really want to do a PhD more than what you're worried about what comes after because you really have to devote your life to it what's going on here David J Z Jackson we're swapp faces look at this ah shoot I just closed the tab speaking of which let's let's do like a TBL Blitz here and delete all these tabs we're using this library to swap people's faces you can see here in the friends example everyone's going to become a Ross in this example now they're all Rosses we took my face we put me on all the friends people there we go look at that look I even got my mouth open here we took Monty Python we dropped my face on all these guys actually really good result then we took these two guys who who disagree a lot on AI and there's their faces swapped Obi-Wan and Spock we did the rush hour to see what the different in complexion would look like it kind of found a happy medium I guess I it did change like the whole face complexion which is which is interesting um we tried it on a cow and it didn't work and now we're trying to find celebrity couple images um there we go there's a good picture of them too please be JB yes all right this will be the last thing we do tonight all right let's go for it we have these couple images and and now that we wrote a function they should be pretty easy for for couple in couples we're going to swap and show each couple all right celeb couple guy from The Office girl from other stuff there we go there we go I could make a whole blog post about this this could be like a like a viral Trend the beard here it didn't do great with all right the probably the best looking couple of the whole thing oh my that is disgusting I think the better looking you are when your face is is swapped you then become the least best looking it's like there's a reason you look good that's because that face is on your body this is that the eyebrows are just killing me here all right uh Spider-Man and what's she from I don't know oh my gosh this one actually kind of works it kind of works it kind of works and uh Bieber and his celebrity girlfriend who I don't know the name of just because I'm not in the pop culture ah it looks like it always splits the difference and just decides to uh to keep the mustaches or the beard because we got a mustache kind of on both sides there we go are you entertained those are celebrity couple face swap um I think someone asked that I make this video public I will afterwards so thank you all for hanging out tonight I hope you had fun let me know in the chat if you enjoyed this and the cow swap didn't work cow swap didn't work it did it needs to be a human face it would be hilarious if I could take a kaggle of geese and put my face on that but I don't think that would look that would work looking forward to the YouTube post so the YouTube post goes up immediately it's just going to be this video so just check it out thanks for the videos on YouTube I appreciate it yeah I hope you guys had fun I had fun with you put a big fat F in chat for fun uh I'm going to also promote some things so youtube.com/ robob Mulla will bring you to my YouTube page if you're not already subscribed you should be I'm going to show you it right there exclamation point Discord will bring you to a link and I'm going to paste this for you guys in uh in YouTube That's the link to our Discord that you can join and it's uh for people who are interested in data science so definitely subscribe on YouTube do that thanks um and then we got me on Mulla not uh on Twitter not Super Active but yeah you should definitely join the Discord um and I think that's it we're not runni
Original Description
Live Coding - checking out the open source face swap library InsightFace
Library: https://insightface.ai/
Reddit Post with Model Weights: https://www.reddit.com/r/midjourney/comments/13pnraj/please_reupload_inswapper_128onnx/
Code: https://github.com/RobMulla/twitch-stream-projects/blob/main/060-insightface/insight-face.ipynb
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
Timeline:
00:00 Getting Setup
03:39 What is Insightface?
08:24 Environment Setup
15:51 Finding the Model Weights on Reddit
23:04 Loading Face Swap Models
26:30 Detecting Faces and Plotting
37:24 Swapping Friends Faces
48:51 Swapping in My Face on Friends
57:59 Monty Python Face Swap
1:05:15 Swapping More Faces
1:37:14 Celebrity Couple Face Swap
1:47:34 Couple Face Swap Results
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A Gentle Introduction to Pandas Data Analysis (on Kaggle)
Rob Mulla
Exploratory Data Analysis with Pandas Python
Rob Mulla
7 Python Data Visualization Libraries in 15 minutes
Rob Mulla
Kaggle competition starter notebook walkthrough
Rob Mulla
Kaggle Competitions: A Beginner's Guide to Winning
Rob Mulla
Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Rob Mulla
Audio Data Processing in Python
Rob Mulla
Complete Data Science Project!
Rob Mulla
Make Your Pandas Code Lightning Fast
Rob Mulla
Image Processing with OpenCV and Python
Rob Mulla
Speed Up Your Pandas Dataframes
Rob Mulla
This INCREDIBLE trick will speed up your data processes.
Rob Mulla
Complete Guide to Cross Validation
Rob Mulla
Easy Python Progress Bars with tqdm
Rob Mulla
Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Rob Mulla
Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Rob Mulla
Get Started with Machine Learning and AI in 2023
Rob Mulla
The Trick to Get Unlimited Datasets
Rob Mulla
Video Data Processing with Python and OpenCV
Rob Mulla
Object Detection in 10 minutes with YOLOv5 & Python!
Rob Mulla
Pandas for Data Science #shorts
Rob Mulla
Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Rob Mulla
Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Rob Mulla
Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Rob Mulla
Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Rob Mulla
Solving an Impossible Riddle with Code
Rob Mulla
Do these Pandas Alternatives actually work?
Rob Mulla
Time Series Forecasting with XGBoost - Advanced Methods
Rob Mulla
Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Rob Mulla
Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Rob Mulla
Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Rob Mulla
25 Nooby Pandas Coding Mistakes You Should NEVER make.
Rob Mulla
DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
Rob Mulla
More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
Rob Mulla
Medallion Data Science Live Stream
Rob Mulla
Community Kaggle Competition Overview - Corn Classification (
Rob Mulla
Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Rob Mulla
OpenAI Whisper Demo: Convert Speech to Text in Python
Rob Mulla
Yolov7 Custom Object Detection in Python Tutorial - Chess Piece Detection
Rob Mulla
Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Rob Mulla
Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Rob Mulla
Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Rob Mulla
Flight Delay Dataset Creation (Data Science Uncut)
Rob Mulla
5 Reasons to Kaggle #shorts
Rob Mulla
♟️ Data Science - Chess Data Analysis
Rob Mulla
EXTREME PYTHON & DATA SCIENCE LIVE STREAM
Rob Mulla
What is Clustering in ML?
Rob Mulla
What is K-Nearest Neighbors?
Rob Mulla
LIVE CODING: Flight Data Exploration with Pandas & Python
Rob Mulla
Kaggle Survey vs. Twitter Sentiment
Rob Mulla
If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
Rob Mulla
Data Visualization BATTLE!
Rob Mulla
LIVE CODING: Stocks & Sentiment Analysis
Rob Mulla
Progress Bar in Python with TQDM
Rob Mulla
Flight Cancellation Data Analysis
Rob Mulla
Synthetic Dataset Creation for Machine Learning - Blender and Python
Rob Mulla
The Ultimate Coding Setup for Data Science
Rob Mulla
Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Rob Mulla
Data Wrangling with Python and Pandas LIVE
Rob Mulla
Forecasting with the FB Prophet Model
Rob Mulla
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
How To Use Gemini 2.5 Flash For Image Generation
Dev.to AI
Why PixelToolsPro is About to Become Your Next Go-To Image Editor
Medium · AI
I Couldn't Find a Good Image Metadata Tool, So I Built One
Dev.to · Robin Hood
Building a Browser-Based Image Resizer with Step-Down Scaling and Crop
Dev.to · Arhan Ahmad
Chapters (12)
Getting Setup
3:39
What is Insightface?
8:24
Environment Setup
15:51
Finding the Model Weights on Reddit
23:04
Loading Face Swap Models
26:30
Detecting Faces and Plotting
37:24
Swapping Friends Faces
48:51
Swapping in My Face on Friends
57:59
Monty Python Face Swap
1:05:15
Swapping More Faces
1:37:14
Celebrity Couple Face Swap
1:47:34
Couple Face Swap Results
🎓
Tutor Explanation
DeepCamp AI