Advertisement
dan-masek

Face Recognition - Fixed Tonderai's Script

Mar 27th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. face_cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_alt.xml')
  5.  
  6. face_mask = cv2.imread('images/p2.png')
  7. h_mask, w_mask = face_mask.shape[:2]
  8.  
  9. if face_cascade.empty():
  10.     raise IOError('Unable to load the face cascade classifier xml file')
  11.  
  12. cap = cv2.VideoCapture(0)
  13. scaling_factor = 0.5
  14.  
  15. while True:
  16.     ret, frame = cap.read()
  17.     frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
  18.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  19.  
  20.     face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
  21.     for (x,y,w,h) in face_rects:
  22.         if h > 0 and w > 0:
  23.             h, w = int(2.0*h), int(1.7*w)
  24.             y -= int(0.3*h)
  25.             x -= int(0.2*w)
  26.            
  27.             face_mask_small = cv2.resize(face_mask, (w,h), interpolation=cv2.INTER_AREA)
  28.            
  29.             # Trimming the face mask when it goes beyond frame boundaries
  30.             frame_height, frame_width = frame.shape[:2]
  31.            
  32.             trim_top, trim_bottom = 0, 0
  33.             if (y + h > frame_height):
  34.                 trim_bottom = (y + h) - frame_height
  35.             if (y < 0):
  36.                 trim_top = -y
  37.                
  38.             trim_left, trim_right = 0, 0
  39.             if (x + w > frame_width):
  40.                 trim_right = (x + w) - frame_width
  41.             if (x < 0):
  42.                 trim_left = -x
  43.                
  44.             face_mask_small = face_mask_small[trim_top:(h - trim_bottom),trim_left:(w - trim_right)]
  45.  
  46.             frame_roi = frame[(y+trim_top):(y+h-trim_bottom), (x+trim_left):(x+w-trim_right)]
  47.  
  48.  
  49.             gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY)
  50.             ret, mask = cv2.threshold(gray_mask, 220, 255, cv2.THRESH_BINARY_INV)
  51.             mask_inv = cv2.bitwise_not(mask)
  52.             print("mask_small",face_mask_small.shape)
  53.             print("mask_roi",frame_roi.shape)
  54.            
  55.            
  56.             masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv)
  57.  
  58.             masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask)
  59.            
  60.             frame_roi[:,:] = cv2.add(masked_face, masked_frame)
  61.    
  62.    
  63.     #cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
  64.     #cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
  65.     cv2.imshow('Face Detector', frame)
  66.  
  67.     c = cv2.waitKey(1)
  68.     if c == 27:
  69.         break
  70.  
  71. cap.release()
  72. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement