Skip to content

Instantly share code, notes, and snippets.

@anistark
Forked from smeschke/Faceswap
Created April 29, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anistark/efa4686dd80386fe3d4e730710915b11 to your computer and use it in GitHub Desktop.
Save anistark/efa4686dd80386fe3d4e730710915b11 to your computer and use it in GitHub Desktop.
#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