eyecrop.py 640 Bytes
import cv2
import dlib
import numpy as np

IMG_SIZE = (34, 26)

def crop_eye(img, eye_points):
  x1, y1 = np.amin(eye_points, axis=0)
  x2, y2 = np.amax(eye_points, axis=0)
  cx, cy = (x1 + x2) / 2, (y1 + y2) / 2

  w = (x2 - x1) * 1.2
  h = w * IMG_SIZE[1] / IMG_SIZE[0]

  margin_x, margin_y = w / 2, h / 2

  min_x, min_y = int(cx - margin_x), int(cy - margin_y)
  max_x, max_y = int(cx + margin_x), int(cy + margin_y)

  eye_rect = np.rint([min_x, min_y, max_x, max_y]).astype(np.int)

  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)




  eye_img = gray[eye_rect[1]:eye_rect[3], eye_rect[0]:eye_rect[2]]




  return eye_img, eye_rect