Created
May 28, 2015 07:39
-
-
Save dvida/fdf9b2cad89506de22e6 to your computer and use it in GitHub Desktop.
OpenCV camera - replaces all faces on the image with the first detected 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 numpy as np | |
import cv2 | |
baboon = cv2.imread('slike/baboon.bmp') | |
cap = cv2.VideoCapture(0) | |
while(True): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
final = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
cascade_path = './' | |
face_cascade = cv2.CascadeClassifier(cascade_path + 'haarcascade_frontalface_default.xml') | |
faces = face_cascade.detectMultiScale(final, 1.3, 5) | |
print "faces", len(faces) | |
if len(faces) > 1: | |
# replace all faces | |
x1, y1, w1, h1 = faces[0] | |
for (x,y,w,h) in faces[1:]: | |
#cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) | |
temp = cv2.resize(frame[y1:y1+h1,x1:x1+w1], (w,h)) | |
frame[y:y+h,x:x+w] = temp | |
else: | |
#when only one face is present, show static image | |
for (x,y,w,h) in faces: | |
temp = cv2.resize(baboon, (w,h)) | |
frame[y:y+h,x:x+w] = temp | |
# Display the resulting frame | |
cv2.imshow('frame', frame) | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord('q'): | |
break | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment