-
-
Save chrishamant/4325ca1a497527a8bc3ec5078e68fb45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import necessary libraries | |
import cv2 | |
import numpy as np | |
#capture video from the webcam | |
cap = cv2.VideoCapture(0) | |
#load the face finder | |
face_cascade = cv2.CascadeClassifier('/home/sm/Desktop/haarcascade_frontalface_default.xml') | |
#load the face that will be swapped in | |
face_img = cv2.imread('/home/sm/Desktop/faces/14.jpg') | |
#start loop | |
while True: | |
ret, img = cap.read() #read image | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
faces = face_cascade.detectMultiScale(gray, 1.3, 3) #find faces | |
#for all the faces found in the frame | |
for (x,y,w,h) in faces: | |
#resize and blend the face to be swapped in | |
face = cv2.resize(face_img,(h,w), interpolation = cv2.INTER_CUBIC) | |
face = cv2.addWeighted(img[y:y+h,x:x+w],.5, face,.5,1) | |
#swap faces | |
img[y:y+h,x:x+w] = face | |
#show the image | |
cv2.imshow('img',img) | |
cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment