Skip to content

Instantly share code, notes, and snippets.

@smeschke
Created April 29, 2016 04:32
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save smeschke/5926ec3c952ab506cb8f4c73366adca0 to your computer and use it in GitHub Desktop.
Save smeschke/5926ec3c952ab506cb8f4c73366adca0 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)
@smeschke
Copy link
Author

14

@smeschke
Copy link
Author

Link to the xml file needed to detect the face: https://github.com/opencv/opencv/tree/master/data/haarcascades
get frontalface_default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment