Sabab

FaceWithPupil

Nov 2nd, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. # multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
  5.  
  6. # https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
  7. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  8. # https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
  9. eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
  10.  
  11. cap = cv2.VideoCapture(0)
  12.  
  13. while 1:
  14.     ret, img = cap.read()
  15.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  16.     faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  17.  
  18.     for (x, y, w, h) in faces:
  19.         cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  20.         roi_gray = gray[y:y + h, x:x + w]
  21.         roi_color = img[y:y + h, x:x + w]
  22.         #cv2.imshow("roi_color",roi_color)
  23.         eyes = eye_cascade.detectMultiScale(roi_gray)
  24.         for (ex, ey, ew, eh) in eyes:
  25.  
  26.             cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
  27.  
  28.             eye_color = roi_color[ey:ey + eh, ex:ex + ew]
  29.  
  30.             cloneImage = cv2.cvtColor(eye_color, cv2.COLOR_BGR2GRAY)
  31.             cloneImage = cv2.medianBlur(cloneImage, 5)
  32.  
  33.  
  34.             cimg = cv2.cvtColor(cloneImage, cv2.COLOR_GRAY2BGR)
  35.             try:
  36.                 circles = cv2.HoughCircles(cloneImage, cv2.HOUGH_GRADIENT, 1, int(ew / 8),
  37.                                            param1=20, param2=15, minRadius=int(eh / 8), maxRadius=int(eh / 3))
  38.                 circles = np.uint16(np.around(circles))
  39.                 counter = 0
  40.                 for i in circles[0, :]:
  41.                     counter = 1
  42.                     # draw the outer circle
  43.                     cv2.circle(eye_color, (i[0], i[1]), i[2], (0,0, 255), 2)
  44.  
  45.                     # draw the center of the circle
  46.                     cv2.circle(eye_color, (i[0], i[1]), 2, (100, 125, 255), 3)
  47.                     if counter == 1:
  48.                         break
  49.  
  50.             except:
  51.                 print("exception:")
  52.  
  53.  
  54.     cv2.imshow('img', img)
  55.     k = cv2.waitKey(30) & 0xff
  56.     if k == 27:
  57.         break
  58.  
  59. cap.release()
  60. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment