(facecrop, eye_cascade, Lexpect=(0.7,0.4), Rexpect=(0.3,0.4), \
	maxdist=0.2, maxsize=0.3)
return success, [left, right]
Attempts to find eyes in an image of a face, and crops the left
	and the right eye separately. When more than two potential eyes
	are detected, the eyes that are closest to the expected positions
	of the eyes will be selected.
	
	Arguments

	facecrop		-	A numpy.ndarray with unsigned, 8-bit
					integers that reflect the greyscale values
					of a face.
	
	Keyword Arguments
	
	Lexpect		-	A (x,y) tuple that indicates where the left
					eye is expected to be. Note that the
					coordinates are in relative space, where
					(0,0) is the top-left of the image, (0,1)
					is the bottom-left, and (1,1) is the
					bottom-right. Also note that the left eye is
					likely to be on the right side of the image,
					and the right eye is likely to be in the
					left part of the image. Default = (0.7,0.4)
	
	Rexpect		-	A (x,y) tuple that indicates where the right
					eye is expected to be. Note that the
					coordinates are in relative space, where
					(0,0) is the top-left of the image, (0,1)
					is the bottom-left, and (1,1) is the
					bottom-right. Also note that the left eye is
					likely to be on the right side of the image,
					and the right eye is likely to be in the
					left part of the image. Default = (0.3,0.4)
	
	maxdist		-	A float that indicates what the maximal
					allowable distance is between the expected
					eye position, and the position of detected
					potential eye. The maximal distance is
					defined as a proportion of the image height.
					It can also be set to None. Default = (0.2)
	
	maxsize		-	A float that indicates what the maximal
					allowable width is of the detected eyes. The
					maximal size is defined as a proportion of
					the image width. It can also be set to None.
					Default = (0.3)
	
	Returns
	
	success, [left, right]	-	success is a Boolean that indicates 
						whether the eyes could be detected.
						left and right are both a numpy.ndarray
						with unsigned, 8-bit integers that
						reflect the greyscale values of what
						are assumed to be the left and the
						right eye.
Attempts to find eyes in an image of a face, and crops the left
	and the right eye separately. When more than two potential eyes
	are detected, the eyes that are closest to the expected positions
	of the eyes will be selected.
	
	Arguments
Attempts to find eyes in an image of a face and crops the left and the right eye separately . When more than two potential eyes are detected the eyes that are closest to the expected positions of the eyes will be selected . Arguments
def _crop_eyes(facecrop, eye_cascade, Lexpect=(0.7,0.4), Rexpect=(0.3,0.4), \
	maxdist=0.2, maxsize=0.3):
	
	"""Attempts to find eyes in an image of a face, and crops the left
	and the right eye separately. When more than two potential eyes
	are detected, the eyes that are closest to the expected positions
	of the eyes will be selected.
	
	Arguments

	facecrop		-	A numpy.ndarray with unsigned, 8-bit
					integers that reflect the greyscale values
					of a face.
	
	Keyword Arguments
	
	Lexpect		-	A (x,y) tuple that indicates where the left
					eye is expected to be. Note that the
					coordinates are in relative space, where
					(0,0) is the top-left of the image, (0,1)
					is the bottom-left, and (1,1) is the
					bottom-right. Also note that the left eye is
					likely to be on the right side of the image,
					and the right eye is likely to be in the
					left part of the image. Default = (0.7,0.4)
	
	Rexpect		-	A (x,y) tuple that indicates where the right
					eye is expected to be. Note that the
					coordinates are in relative space, where
					(0,0) is the top-left of the image, (0,1)
					is the bottom-left, and (1,1) is the
					bottom-right. Also note that the left eye is
					likely to be on the right side of the image,
					and the right eye is likely to be in the
					left part of the image. Default = (0.3,0.4)
	
	maxdist		-	A float that indicates what the maximal
					allowable distance is between the expected
					eye position, and the position of detected
					potential eye. The maximal distance is
					defined as a proportion of the image height.
					It can also be set to None. Default = (0.2)
	
	maxsize		-	A float that indicates what the maximal
					allowable width is of the detected eyes. The
					maximal size is defined as a proportion of
					the image width. It can also be set to None.
					Default = (0.3)
	
	Returns
	
	success, [left, right]	-	success is a Boolean that indicates 
						whether the eyes could be detected.
						left and right are both a numpy.ndarray
						with unsigned, 8-bit integers that
						reflect the greyscale values of what
						are assumed to be the left and the
						right eye.
	"""
	
	# Return straight away when facecrop==None
	if facecrop is None:
		return False, [None, None]
	
	# DETECT THE EYES
	eyes = eye_cascade.detectMultiScale(facecrop)
	
	# Return if no eyes could be detected.
	if len(eyes) == 0:
		return False, [None, None]
	
	# Remove all the potential eye rects that are too large.
	if maxsize != None:
		eyes = eyes[eyes[:,3] < maxsize * facecrop.shape[0]]
	
	# Calculate the distances between each potential eye and the
	# expected locations. (NOTE: These are not the actual distances,
	# but the squared distances. They need to be compared to the
	# squared maximum distance.)
	cx = (eyes[:,0] + eyes[:,2] / 2) / float(facecrop.shape[1])
	cy = (eyes[:,1] + eyes[:,3] / 2) / float(facecrop.shape[0])
	dl = (cx - Lexpect[0])**2 + (cy - Lexpect[1])**2
	dr = (cx - Rexpect[0])**2 + (cy - Rexpect[1])**2
	# Exclude all potential eyes that are too far from expected eye
	# locations.
	if maxdist != None:
		good = numpy.min([dl, dr], axis=0) < maxdist**2
		eyes = eyes[good]
		dl = dl[good]
		dr = dr[good]
	
	# COUNT THE EYES
	# If no eye was detected, there is no eye index.
	if len(eyes) == 0:
		li = None
		ri = None
	
	# If only one eye is detected, its index is 0.
	elif len(eyes) == 1:
		# Check whether the distance to the left eye is closer. If it
		# is, than only the left eye was recorded. If not, the right
		# eye was recorded.
		if dl[0] < dr[0]:
			li = 0
			ri = None
		else:
			li = None
			ri = 0
	
	# If two or more eyes were detected, choose the potential rects
	# that were closest to the expected eye positions.
	else:
		li = numpy.argmin(dl)
		ri = numpy.argmin(dr)

	# RETURN CROPPED EYES
	# If no eye was detected, return no success.
	success = True
	if (li is None) & (ri is None):
		success = False
	# If the left eye was detected, crop it from the face.
	if li is None:
		left = None
	else:
		x, y, w, h = eyes[li]
		y += h/4
		h /= 2
		left = facecrop[y:y+h, x:x+w]
	# If the right eye was detected, crop it from the face.
	if ri is None:
		right = None
	else:
		x, y, w, h = eyes[ri]
		y += h/4
		h /= 2
		right = facecrop[y:y+h, x:x+w]

	# In DEBUG mode, draw the original frame and the detected eyes.
	if _DEBUG:
		# Display the face in the bottom-left pane.
		_AX[1][0].imshow(facecrop, cmap='gray')
		_AX[1][0].set_title("potential eyes")
		for i in range(len(eyes)):
			r = eyes[i]
			if i == li or i == ri:
				_AX[1][0].add_patch(patches.Rectangle( \
					(r[0],r[1]), r[2], r[3], fill=False, \
					linewidth=3))
			else:
				_AX[1][0].add_patch(patches.Rectangle( \
					(r[0],r[1]), r[2], r[3], fill=False, \
					linewidth=1))

	return success, [left, right]
def _crop_eyes ( facecrop , eye_cascade , Lexpect = ( 0.7 , 0.4 ) , Rexpect = ( 0.3 , 0.4 ) , maxdist = 0.2 , maxsize = 0.3 ) : # Return straight away when facecrop==None if facecrop is None : return False , [ None , None ] # DETECT THE EYES eyes = eye_cascade . detectMultiScale ( facecrop ) # Return if no eyes could be detected. if len ( eyes ) == 0 : return False , [ None , None ] # Remove all the potential eye rects that are too large. if maxsize != None : eyes = eyes [ eyes [ : , 3 ] < maxsize * facecrop . shape [ 0 ] ] # Calculate the distances between each potential eye and the # expected locations. (NOTE: These are not the actual distances, # but the squared distances. They need to be compared to the # squared maximum distance.) cx = ( eyes [ : , 0 ] + eyes [ : , 2 ] / 2 ) / float ( facecrop . shape [ 1 ] ) cy = ( eyes [ : , 1 ] + eyes [ : , 3 ] / 2 ) / float ( facecrop . shape [ 0 ] ) dl = ( cx - Lexpect [ 0 ] ) ** 2 + ( cy - Lexpect [ 1 ] ) ** 2 dr = ( cx - Rexpect [ 0 ] ) ** 2 + ( cy - Rexpect [ 1 ] ) ** 2 # Exclude all potential eyes that are too far from expected eye # locations. if maxdist != None : good = numpy . min ( [ dl , dr ] , axis = 0 ) < maxdist ** 2 eyes = eyes [ good ] dl = dl [ good ] dr = dr [ good ] # COUNT THE EYES # If no eye was detected, there is no eye index. if len ( eyes ) == 0 : li = None ri = None # If only one eye is detected, its index is 0. elif len ( eyes ) == 1 : # Check whether the distance to the left eye is closer. If it # is, than only the left eye was recorded. If not, the right # eye was recorded. if dl [ 0 ] < dr [ 0 ] : li = 0 ri = None else : li = None ri = 0 # If two or more eyes were detected, choose the potential rects # that were closest to the expected eye positions. else : li = numpy . argmin ( dl ) ri = numpy . argmin ( dr ) # RETURN CROPPED EYES # If no eye was detected, return no success. success = True if ( li is None ) & ( ri is None ) : success = False # If the left eye was detected, crop it from the face. if li is None : left = None else : x , y , w , h = eyes [ li ] y += h / 4 h /= 2 left = facecrop [ y : y + h , x : x + w ] # If the right eye was detected, crop it from the face. if ri is None : right = None else : x , y , w , h = eyes [ ri ] y += h / 4 h /= 2 right = facecrop [ y : y + h , x : x + w ] # In DEBUG mode, draw the original frame and the detected eyes. if _DEBUG : # Display the face in the bottom-left pane. _AX [ 1 ] [ 0 ] . imshow ( facecrop , cmap = 'gray' ) _AX [ 1 ] [ 0 ] . set_title ( "potential eyes" ) for i in range ( len ( eyes ) ) : r = eyes [ i ] if i == li or i == ri : _AX [ 1 ] [ 0 ] . add_patch ( patches . Rectangle ( ( r [ 0 ] , r [ 1 ] ) , r [ 2 ] , r [ 3 ] , fill = False , linewidth = 3 ) ) else : _AX [ 1 ] [ 0 ] . add_patch ( patches . Rectangle ( ( r [ 0 ] , r [ 1 ] ) , r [ 2 ] , r [ 3 ] , fill = False , linewidth = 1 ) ) return success , [ left , right ]