in reinvent-2019/connected-photo-booth/py_client/selfie_with_filters.py [0:0]
def create_eye_mask_filter(image_path=''):
create_eye_mask_filter_logger = logging.getLogger('selfie_with_filters.create_eye_mask_filter')
create_eye_mask_filter_logger.info("In the create_eye_mask_filter method ...")
filtered_image = ''
# 0. check if there is a valid file
if not image_path:
create_eye_mask_filter_logger.error("Error! No Image provided! Can't create filter if there is no image!!")
return ""
# 1. read image (selfie)
frame = cv2.imread(image_path)
create_eye_mask_filter_logger.info("The image is at: %s" % image_path)
image_height, image_width = frame.shape[:2]
create_eye_mask_filter_logger.info("Image Height: %d, Image Width: %d" % (image_height, image_width))
# 2. now run a detect faces on this image
faces = get_facial_landmarks(image_path=image_path)
for face_idx, face in enumerate(faces):
create_eye_mask_filter_logger.info(face["BoundingBox"])
bb_left = face["BoundingBox"]["Left"]
bb_left_px = int(bb_left*image_width)
bb_top = face["BoundingBox"]["Top"]
bb_top_px = int(bb_top*image_height)
bb_height = face["BoundingBox"]["Height"]
bb_height_px = int(bb_height*image_height)
bb_width = face["BoundingBox"]["Width"]
bb_width_px = int(bb_width*image_width)
create_eye_mask_filter_logger.info("%f, %f" % (bb_left, bb_left_px))
create_eye_mask_filter_logger.info("%f, %f" % (bb_top, bb_top_px))
create_eye_mask_filter_logger.info("%f, %f" % (bb_height, bb_height_px))
create_eye_mask_filter_logger.info("%f, %f" % (bb_width, bb_width_px))
bb_left_top = (bb_left_px, bb_top_px)
bb_bottom_right = (bb_left_px+bb_width_px, bb_top_px+bb_height_px)
cv2.rectangle(frame,bb_left_top, bb_bottom_right, (0,255,0), 2)
# Now get the nose positions for each face
if "Landmarks" in face:
upper_jawline_left = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="upperJawlineLeft", image_width=image_width, image_height=image_height)
upper_jawline_right = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="upperJawlineRight", image_width=image_width, image_height=image_height)
leftEyeBrowLeft = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="leftEyeBrowLeft", image_width=image_width, image_height=image_height)
leftEyeBrowUp = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="leftEyeBrowUp", image_width=image_width, image_height=image_height)
leftEyeBrowRight = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="leftEyeBrowRight", image_width=image_width, image_height=image_height)
rightEyeBrowLeft = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="rightEyeBrowLeft", image_width=image_width, image_height=image_height)
rightEyeBrowUp = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="rightEyeBrowUp", image_width=image_width, image_height=image_height)
rightEyeBrowRight = get_landmark_value(landmarks=face["Landmarks"],
parameter_name="rightEyeBrowRight", image_width=image_width, image_height=image_height)
else:
create_eye_mask_filter_logger.warning("No Landmarks found in face!")
continue
create_eye_mask_filter_logger.info("Retrieved Jaw positions for face: %d" % (face_idx+1))
create_eye_mask_filter_logger.info(upper_jawline_left)
cv2.circle(frame, upper_jawline_left, 3, (255,0,0), -1)
cv2.circle(frame, upper_jawline_right, 3, (255,0,0), -1)
cv2.circle(frame, leftEyeBrowLeft, 3, (0,255,0), -1)
cv2.circle(frame, leftEyeBrowUp, 3, (0,255,0), -1)
cv2.circle(frame, leftEyeBrowRight, 3, (0,255,0), -1)
cv2.circle(frame, rightEyeBrowLeft, 3, (0,0,255), -1)
cv2.circle(frame, rightEyeBrowUp, 3, (0,0,255), -1)
cv2.circle(frame, rightEyeBrowRight, 3, (0,0,255), -1)
eye_mask_width = int(bb_width_px*1.25)
eye_mask_height = int(bb_height_px/2.5)
create_eye_mask_filter_logger.info(eye_mask_width)
create_eye_mask_filter_logger.info(eye_mask_height)
#em_left_top = (int(bb_left_px-eye_mask_width/7), int(bb_top_px-eye_mask_height))
em_left_top = (upper_jawline_left[0],int(upper_jawline_left[1]-eye_mask_height/2))
#em_bottom_right = (int(bb_left_px+(eye_mask_width*1)), int(bb_top_px-eye_mask_height/2))
em_bottom_right = (upper_jawline_right[0],int(upper_jawline_left[1]+eye_mask_height/10))
create_eye_mask_filter_logger.info(em_left_top)
create_eye_mask_filter_logger.info(em_bottom_right)
cv2.rectangle(frame, em_left_top, em_bottom_right, (0,0,255), 2)
# 3. apply effects of flower crown
eyemask_image = cv2.imread(config.__EYE_MASK_FILTER__)
rows, cols, _ = frame.shape
eyemask_mask = np.zeros((rows, cols), np.uint8)
eyemask_mask.fill(0)
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Adding the new crown
eyemask = cv2.resize(eyemask_image, (eye_mask_width, eye_mask_height))
eyemask_gray = cv2.cvtColor(eyemask, cv2.COLOR_BGR2GRAY)
_, eyemask_mask = cv2.threshold(eyemask_gray, 25, 255, cv2.THRESH_BINARY_INV)
eyemask_area = frame[em_left_top[1]: em_left_top[1] + eye_mask_height,
em_left_top[0]: em_left_top[0] + eye_mask_width]
eyemask_area_no_eyemask = cv2.bitwise_and(eyemask_area, eyemask_area, mask=eyemask_mask)
final_eyemask = cv2.add(eyemask_area_no_eyemask, eyemask)
#cv2.imshow("eyemask", final_eyemask)
frame[em_left_top[1]: em_left_top[1] + eye_mask_height,
em_left_top[0]: em_left_top[0] + eye_mask_width] = final_eyemask
create_eye_mask_filter_logger.info("Added the eyemask for face: %d" % (face_idx+1))
'''
eye_mask_image = cv2.imread(__EYE_MASK_FILTER__)
rows, cols, _ = frame.shape
eyemask_mask = np.zeros((rows, cols), np.uint8)
eyemask_mask.fill(0)
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Adding the new crown
eye_mask = cv2.resize(eye_mask_image, (eye_mask_width, eye_mask_height))
eye_mask_gray = cv2.cvtColor(eye_mask, cv2.COLOR_BGR2GRAY)
_, eyemask_mask = cv2.threshold(eye_mask_gray, 25, 255, cv2.THRESH_BINARY_INV)
eyemask_area = frame[em_left_top[1]: em_left_top[1] + eye_mask_height,
em_left_top[0]: em_left_top[0] + eye_mask_width]
eyemask_area_no_eyemask = cv2.bitwise_and(eyemask_area, eyemask_area, mask=eyemask_mask)
final_eyemask = cv2.add(eyemask_area_no_eyemask, eye_mask)
frame[em_left_top[1]: em_left_top[1] + eye_mask_height,
em_left_top[0]: em_left_top[0] + eye_mask_width] = final_eyemask
create_eye_mask_filter_logger.info("Added the eyemask for face: %d" % (face_idx+1))
'''
# 4. display the frame thus computed
cv2.imshow("ImageFrame", frame)
while True:
key = cv2.waitKey(0)
print(key)
print(int(key))
test = int(key) == 27
print(test)
if int(key) == 27:
cv2.destroyAllWindows()
print("Destroyed the Window")
break
return "TESTONLY"
'''
# 5. save the image
filtered_image = "%s/%s" % (__CEREBRO_MEDIA_DIR__, __FILTERED_IMAGE_NAME__)
cv2.imwrite(filtered_image, frame)
'''
return filtered_image