Last active
March 7, 2018 23:28
-
-
Save ilan1987/3bebc3d9f52400bc02c3276b79a21eb7 to your computer and use it in GitHub Desktop.
This is my script for changing the hightechzone video by replacing all faces with my face.
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 cv2 | |
import numpy as np | |
from scipy.spatial.distance import pdist,squareform | |
from matplotlib import pyplot as plt | |
from skimage.transform import resize | |
if __name__=='__main__': | |
picture_with_face_path=r'IlanAtCampNou.jpg' | |
video_path=r'Hightechzone_video.mp4' | |
face_cascade1 = cv2.CascadeClassifier(r'lbpcascade_frontalface_improved.xml') | |
face_cascade2=cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml') | |
capture = cv2.VideoCapture(video_path) | |
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) | |
height =capture.get(cv2.CAP_PROP_FRAME_HEIGHT) | |
fps = capture.get(cv2.CAP_PROP_FPS) | |
fourcc = cv2.VideoWriter_fourcc(*'XVID') | |
out = cv2.VideoWriter('Output_video.avi',fourcc , fps, (int(width), int(height))) | |
image=cv2.imread(picture_with_face_path) | |
distance_threshold = 50 | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
faces = face_cascade1.detectMultiScale(gray, 1.5, 5) | |
for (x, y, w, h) in faces: | |
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2) | |
Ilan_color = image[y:y + h, x:x + w] | |
ret,im = capture.read() | |
while(ret): | |
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) | |
if np.any(gray): | |
faces1 = face_cascade1.detectMultiScale(gray, 1.05,15) | |
faces2 = face_cascade2.detectMultiScale(gray, 1.05,12) | |
if (len(faces1)>0) & (len (faces2)>0): | |
xs_ys_lengths_width=np.vstack((faces1,faces2)) | |
mean_points=np.array((xs_ys_lengths_width[:,0]+xs_ys_lengths_width[:,2]/2,xs_ys_lengths_width[:,1]+xs_ys_lengths_width[:,3]/2)).T | |
distances=squareform(pdist(mean_points)) | |
if np.all(distances<distance_threshold): | |
faces=faces1 if len(faces1) >len(faces2) else faces2 | |
else: | |
needed_to_be_deleted=np.bitwise_xor(distances<distance_threshold,np.eye(distances.shape[0]).astype(np.bool)) | |
indices=np.argwhere(needed_to_be_deleted) | |
if len(indices)>0: | |
try: | |
indices=indices[indices[:,0]>indices[:,1]] | |
for ind in indices[:,0]: | |
xs_ys_lengths_width=np.delete(xs_ys_lengths_width,ind,axis=0) | |
except: | |
print('Error is here') | |
faces=xs_ys_lengths_width | |
else: | |
faces=faces1 if len(faces1) else faces2 | |
for (x, y, w, h) in faces: | |
face_1=resize(Ilan_color,(h,w)) | |
im[y:y + h, x:x + w]=(255*face_1).astype(np.uint8) | |
# for (x, y, w, h) in faces_at_profile: | |
# face_1=resize(Ilan_color,(h,w)) | |
# im[y:y + h, x:x + w]=(255*face_1).astype(np.uint8) | |
out.write(im) | |
ret, im = capture.read() | |
capture.release() | |
out.release() | |
cv2.imshow('img', image) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment