def crop(face): """ Crop the image to remove as much unneeded information as possible. This works by applying PCA to find the torso and then find the nose. The result is a view that is centered at the nose. """ # Reset image first to make sure we take all of the image face.reset() # Calculate the position of the image masked_z = numpy.ma.masked_array(face.Z, numpy.isnan(face.Z)) x, y, covariance = intertial_axis(masked_z) # Center at the point overscan_x = face.width * 0.25 overscan_y = face.height * 0.25 face.center_at(x, y, overscan_x, overscan_y) # Calculate max Z-value x and y x, y = max_xy(face.Z) # Set view to center of nose face.center_at(x, y, face.width / 2.0, face.height / 2.0)
def crop(face): """ Crop the image to remove as much unneeded information as possible. This works by applying PCA to find the torso and then find the nose. The result is a view that is centered at the nose. """ # Reset image first to make sure we take all of the image face.reset() # Calculate the position of the image masked_z = numpy.ma.masked_array(face.Z, numpy.isnan(face.Z)) x, y, covariance = intertial_axis(masked_z) # Center at the point overscan_x = face.width * 0.25 overscan_y = face.height * 0.25 face.center_at(x, y, overscan_x, overscan_y) # Calculate max Z-value x and y x, y = max_xy(face.Z) # Set view to center of nose face.center_at(x, y, 240 / 2.0, 320 / 2.0)
def zoom(face): """ Move everything such that nose is at depth 0 """ # Correct the nose tip to be at 0 point = max_xy(face.Z) face.abs_file.data['X'] = face.abs_file.data['X'] + abs(face.X[point]) face.abs_file.data['Y'] = face.abs_file.data['Y'] + abs(face.Y[point]) face.abs_file.data['Z'] = face.abs_file.data['Z'] + abs(face.Z[point])
def key_points(face, d_nose_x1=30, d_nose_x2=5, d_nose_y=5, d_lip_y1=25, d_lip_y2=70, d_lip_y3=4, d_lip_x1=45, d_chin_x=3, d_chin_y1=10, d_chin_y2=75, d_eye_x=2, d_eye_y=50): """ Rotate and zoom the face to create a full frame face. This is based on the fact that the nose is the highest point of the picture """ # We apply surfature to calculate the first and second derivates K, H, Pmax, Pmin = surfature(face) # Remove all key points face.key_points.clear() # # Nose # nose_x, nose_y = max_xy(face.Z) face.key_points["nose"] = (nose_x, nose_y) # # Nose left and right # nose_left = Pmin[(nose_y - d_nose_y):(nose_y + d_nose_y), (nose_x - d_nose_x1):(nose_x - d_nose_x2)] nose_right = Pmin[(nose_y - d_nose_y):(nose_y + d_nose_y), (nose_x + d_nose_x2):(nose_x + d_nose_x1)] nose_left_x, nose_left_y = min_xy(nose_left, offset_x=(nose_x - d_nose_x1), offset_y=(nose_y - d_nose_y)) nose_right_x, nose_right_y = min_xy(nose_right, offset_x=(nose_x + d_nose_x2), offset_y=(nose_y - d_nose_y)) face.key_points["nose_left"] = (nose_left_x, nose_left_y) face.key_points["nose_right"] = (nose_right_x, nose_right_y) # # Upper, lower, left right lip # lip_y = numpy.nanargmax(Pmax[(nose_y + d_lip_y1):(nose_y + d_lip_y2), nose_x]) + (nose_y + d_lip_y1) lip_left = Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), (nose_x - d_lip_x1):nose_x] lip_right = Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), nose_x:(nose_x + d_lip_x1)] lip_left_x = find_peak_start(numpy.sum(lip_left, axis=0)) + (nose_x - d_lip_x1) lip_left_y = numpy.nanargmax(Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), lip_left_x]) + (lip_y - d_lip_y3) lip_right_x = find_peak_stop(numpy.sum(lip_right, axis=0)) + nose_x lip_right_y = numpy.nanargmax(Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), lip_right_x]) + (lip_y - d_lip_y3) face.key_points['lip'] = (nose_x, lip_y) face.key_points['lip_left'] = (lip_left_x, lip_left_y) face.key_points['lip_right'] = (lip_right_x, lip_right_y) # # Chin # chin = numpy.gradient( signal.bspline(face.Z[(lip_y + d_chin_y1):, nose_x], 25)) chin_x, chin_y = nose_x, numpy.nanargmin(chin) + (lip_y + d_chin_y1) face.key_points["chin"] = (chin_x, chin_y) # # Eyes # eye_left = Pmax[nose_left_y - d_eye_y:nose_left_y + d_eye_y, nose_left_x - d_eye_x:nose_left_x + d_eye_x] eye_right = Pmax[nose_right_y - d_eye_y:nose_right_y + d_eye_y, nose_right_x - d_eye_x:nose_right_x + d_eye_x] eye_left_x, eye_left_y = max_xy(eye_left, nose_left_x - d_eye_x, d_eye_y) eye_right_x, eye_right_y = max_xy(eye_right, nose_right_x - d_eye_x, d_eye_y) face.key_points["eye_left"] = (eye_left_x, eye_left_y) face.key_points["eye_right"] = (eye_right_x, eye_right_y) # # Nose face border # nose_line = numpy.gradient(face.Z[nose_y, :]) border_nose_left_x, border_nose_left_y = numpy.nanargmax( nose_line[:lip_left_x - 10]), nose_y border_nose_right_x, border_nose_right_y = numpy.nanargmin( nose_line[lip_right_x:]) + lip_right_x + 10, nose_y face.key_points["border_nose_left"] = (border_nose_left_x, border_nose_left_y) face.key_points["border_nose_right"] = (border_nose_right_x, border_nose_right_y) # # Lip face border # lip_line = numpy.gradient(face.Z[lip_y, :]) border_lip_left_x, border_lip_left_y = numpy.nanargmax( lip_line[:lip_left_x - 10]), lip_y border_lip_right_x, border_lip_right_y = numpy.nanargmin( lip_line[lip_right_x:]) + lip_right_x + 10, lip_y face.key_points["border_lip_left"] = (border_lip_left_x, border_lip_left_y) face.key_points["border_lip_right"] = (border_lip_right_x, border_lip_right_y) # # Forehead border # forehead_line = numpy.gradient(face.Z[nose_y - (chin_y - nose_y), :]) border_forehead_left_x, border_forehead_left_y = numpy.nanargmax( forehead_line[:lip_left_x - 10]), nose_y - (chin_y - nose_y) border_forehead_right_x, border_forehead_right_y = numpy.nanargmin( forehead_line[lip_right_x:]) + lip_right_x + 10, nose_y - (chin_y - nose_y) face.key_points["border_forehead_left"] = (border_forehead_left_x, border_forehead_left_y) face.key_points["border_forehead_right"] = (border_forehead_right_x, border_forehead_right_y)
def key_points(face, d_nose_x1=30, d_nose_x2=5, d_nose_y=5, d_lip_y1=25, d_lip_y2=70, d_lip_y3=4, d_lip_x1=50, d_chin_x=3, d_chin_y1=50, d_chin_y2=75, d_eye_x=2, d_eye_y=50): """ Rotate and zoom the face to create a full frame face. This is based on the fact that the nose is the highest point of the picture """ # We apply surfature to calculate the first and second derivates K, H, Pmax, Pmin = surfature(face) # Remove all key points face.key_points.clear() # # Nose # nose_x, nose_y = max_xy(face.Z) face.key_points["nose"] = (nose_x, nose_y) # # Nose left and right # nose_left = Pmin[(nose_y - d_nose_y):(nose_y + d_nose_y), (nose_x - d_nose_x1):(nose_x - d_nose_x2)] nose_right = Pmin[(nose_y - d_nose_y):(nose_y + d_nose_y), (nose_x + d_nose_x2):(nose_x + d_nose_x1)] nose_left_x, nose_left_y = min_xy(nose_left, offset_x=(nose_x - d_nose_x1), offset_y=(nose_y - d_nose_y)) nose_right_x, nose_right_y = min_xy(nose_right, offset_x=(nose_x + d_nose_x2), offset_y=(nose_y - d_nose_y)) face.key_points["nose_left"] = (nose_left_x, nose_left_y) face.key_points["nose_right"] = (nose_right_x, nose_right_y) # # Upper, lower, left right lip # lip_y = numpy.nanargmax(Pmax[(nose_y + d_lip_y1):(nose_y + d_lip_y2), nose_x]) + (nose_y + d_lip_y1) lip_left = Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), (nose_x - d_lip_x1):nose_x] lip_right = Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), nose_x:(nose_x + d_lip_x1)] lip_left_x = find_peak_start(numpy.sum(lip_left, axis=0)) + (nose_x - d_lip_x1) lip_left_y = numpy.nanargmax(Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), lip_left_x]) + (lip_y - d_lip_y3) lip_right_x = find_peak_stop(numpy.sum(lip_right, axis=0)) + nose_x lip_right_y = numpy.nanargmax(Pmax[(lip_y - d_lip_y3):(lip_y + d_lip_y3), lip_right_x]) + (lip_y - d_lip_y3) face.key_points['lip'] = (nose_x, lip_y) face.key_points['lip_left'] = (lip_left_x, lip_left_y) face.key_points['lip_right'] = (lip_right_x, lip_right_y) # # Chin # chin = numpy.gradient(signal.bspline(face.Z[(lip_y + d_chin_y1):, nose_x], 25)) chin_x, chin_y = nose_x, numpy.nanargmin(chin) + (lip_y + d_chin_y1) face.key_points["chin"] = (chin_x, chin_y) # # Eyes # eye_left = Pmax[d_eye_y:nose_left_y - d_eye_y, nose_left_x - d_eye_x:nose_left_x + d_eye_x] eye_right = Pmax[d_eye_y:nose_right_y - d_eye_y, nose_right_x - d_eye_x:nose_right_x + d_eye_x] eye_left_x, eye_left_y = max_xy(eye_left, nose_left_x - d_eye_x, d_eye_y) eye_right_x, eye_right_y = max_xy(eye_right, nose_right_x - d_eye_x, d_eye_y) face.key_points["eye_left"] = (eye_left_x, eye_left_y) face.key_points["eye_right"] = (eye_right_x, eye_right_y) # # Nose face border # nose_line = numpy.gradient(face.Z[nose_y, :]) border_nose_left_x, border_nose_left_y = numpy.nanargmax(nose_line[:lip_left_x - 10]), nose_y border_nose_right_x, border_nose_right_y = numpy.nanargmin(nose_line[lip_right_x + 10:]) + lip_right_x + 10, nose_y face.key_points["border_nose_left"] = (border_nose_left_x, border_nose_left_y) face.key_points["border_nose_right"] = (border_nose_right_x, border_nose_right_y) # # Lip face border # lip_line = numpy.gradient(face.Z[lip_y, :]) border_lip_left_x, border_lip_left_y = numpy.nanargmax(lip_line[:lip_left_x - 10]), lip_y border_lip_right_x, border_lip_right_y = numpy.nanargmin(lip_line[lip_right_x + 10:]) + lip_right_x + 10, lip_y face.key_points["border_lip_left"] = (border_lip_left_x, border_lip_left_y) face.key_points["border_lip_right"] = (border_lip_right_x, border_lip_right_y) # # Forehead border # forehead_line = numpy.gradient(face.Z[nose_y - (chin_y - nose_y), :]) border_forehead_left_x, border_forehead_left_y = numpy.nanargmax(forehead_line[:lip_left_x - 10]), nose_y - (chin_y - nose_y) border_forehead_right_x, border_forehead_right_y = numpy.nanargmin(forehead_line[lip_right_x + 10:]) + lip_right_x + 10, nose_y - (chin_y - nose_y) face.key_points["border_forehead_left"] = (border_forehead_left_x, border_forehead_left_y) face.key_points["border_forehead_right"] = (border_forehead_right_x, border_forehead_right_y)