def fun_util(): emojis = get_emojis() disp_probab, disp_class = 0, 0 while True: img = cam.read()[1] img = cv2.flip(img, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) if len(faces) > 0: for i, face in enumerate(faces): shape_68 = shape_predictor_68(img, face) shape = face_utils.shape_to_np(shape_68) mask = create_mask(shape, img) masked = cv2.bitwise_and(gray, mask) maskAligned = fa.align(mask, gray, face) faceAligned = fa.align(masked, gray, face) (x0, y0, x1, y1) = get_bounding_rect(maskAligned) faceAligned = faceAligned[y0:y1, x0:x1] faceAligned = cv2.resize(faceAligned, (100, 100)) (x, y, w, h) = face_utils.rect_to_bb(face) #cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 0), 2) cv2.imshow('faceAligned', faceAligned) cv2.imshow('face #{}'.format(i), img[y:y + h, x:x + w]) pred_probab, pred_class = keras_predict(cnn_model, faceAligned) img = blend(img, emojis[pred_class], (x, y, w, h)) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): break
def recognize(): disp_probab, disp_class = 0, 0 while True: img = cam.read()[1] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) if len(faces) > 0: face = faces[0] shape_68 = shape_predictor_68(img, face) shape = face_utils.shape_to_np(shape_68) mask = create_mask(shape, img) masked = cv2.bitwise_and(gray, mask) (x, y, w, h) = face_utils.rect_to_bb(face) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2) faceAligned = fa.align(masked, gray, face) cv2.imshow('faceAligned', faceAligned) pred_probab, pred_class = keras_predict(cnn_model, faceAligned) if pred_probab > 0.5: disp_probab = pred_probab disp_class = pred_class cv2.putText(img, str(disp_probab), (50, 50), cv2.FONT_HERSHEY_TRIPLEX, 1.5, (0, 0, 0)) cv2.putText(img, str(disp_class), (50, 100), cv2.FONT_HERSHEY_TRIPLEX, 1.5, (0, 0, 0)) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): break
def emojify(model): emojis = get_emojis() disp_probab, disp_class = 0, 0 while True: img = cam.read()[1] img = cv2.flip(img, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) if len(faces) > 0: for i, face in enumerate(faces): shape_68 = shape_predictor_68(img, face) shape = face_utils.shape_to_np(shape_68) mask = create_mask(shape, img) masked = cv2.bitwise_and(gray, mask) maskAligned = fa.align(mask, gray, face) faceAligned = fa.align(masked, gray, face) (x0, y0, x1, y1) = get_bounding_rect(maskAligned) faceAligned = faceAligned[y0:y1, x0:x1] cv2.imshow('faceAligned', faceAligned) (x, y, w, h) = face_utils.rect_to_bb(face) cv2.imshow('face #{}'.format(i), img[y:y+h, x:x+w]) np_image = load(faceAligned) pred_probab = model.predict(np_image) pred_class = np.argmax(pred_probab,axis = 1) img = blend(img, emojis[int(pred_class)], (x, y, w, h)) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): break
FACES = 'sorted + ours/' folders = os.listdir(FACES) for folder in folders: folder = folder + '/' c = 0 for image in os.listdir(FACES + folder): path = FACES + folder + image img = cv2.imread(path) print(image) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) if len(faces) > 0: for face in faces: shape_68 = shape_predictor_68(img, face) shape = face_utils.shape_to_np(shape_68) mask = create_mask(shape, img) masked = cv2.bitwise_and(gray, mask) maskAligned = fa.align(mask, gray, face) faceAligned = fa.align(masked, gray, face) (x0, y0, x1, y1) = get_bounding_rect(maskAligned) faceAligned = faceAligned[y0:y1, x0:x1] faceAligned = cv2.resize(faceAligned, (128, 128)) (x, y, w, h) = face_utils.rect_to_bb(face) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2) cv2.imshow('faceAligned', faceAligned) if not os.path.isdir(DATASET + folder): os.mkdir(DATASET + folder) cv2.imwrite(DATASET + folder + image, faceAligned) c += 1