def warp_face_from_webcam(facial_mask_fn, video_out_fn):
    """
    Function to read video frames from the web cam, replace first found face by the face from the still image
    and show processed frames in a window. Also all processed frames will be save as a video.

    :param facial_mask_fn: path to the still image with a face
    :param video_out_fn: path to the video file which will have 'replaced' face
    """

    facial_mask = cv2.cvtColor(cv2.imread(facial_mask_fn), cv2.COLOR_BGR2GRAY)
    facial_mask_lm = faceWarp.find_landmarks(facial_mask, faceWarp.predictor)

    cam = cv2.VideoCapture(0)
    frame_size = (
        420, 240
    )  # downsample size, without downsampling too many frames dropped

    video_out = cv2.VideoWriter(
        filename=video_out_fn,
        fourcc=cv2.VideoWriter_fourcc(
            'm', '2', 'v',
            '1'),  # works good on OSX, for other OS maybe try other codecs
        frameSize=frame_size,
        fps=25.0,
        isColor=True)

    while True:
        ret, frame_in = cam.read()
        # Downsample frame - otherwise processing is too slow
        frame_in = cv2.resize(frame_in, dsize=frame_size)

        frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
        frame_out = faceWarp.face_warp(facial_mask, facial_mask_lm, frame_in)
        frame_out = cv2.cvtColor(frame_out, cv2.COLOR_GRAY2BGR)
        video_out.write(frame_out)
        faceWarp.draw_str(frame_out, (20, 20),
                          'ESC: stop recording  Space: stop & save video')
        cv2.imshow('webcam', frame_out)
        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break
        if ch == ord(' '):
            break

    cam.release()
    cv2.destroyAllWindows()
def warp_face_from_webcam(facial_mask_fn, video_out_fn):
    """
    Function to read video frames from the web cam, replace first found face by the face from the still image
    and show processed frames in a window. Also all processed frames will be save as a video.

    :param facial_mask_fn: path to the still image with a face
    :param video_out_fn: path to the video file which will have 'replaced' face
    """

    facial_mask = cv2.cvtColor(cv2.imread(facial_mask_fn), cv2.COLOR_BGR2GRAY)
    facial_mask_lm = faceWarp.find_landmarks(facial_mask, faceWarp.predictor)

    cam = cv2.VideoCapture(0)
    frame_size = (420, 240) # downsample size, without downsampling too many frames dropped

    video_out = cv2.VideoWriter(
        filename=video_out_fn,
        fourcc=cv2.cv.CV_FOURCC('m', 'p', '4', 'v'), # works good on OSX, for other OS maybe try other codecs
        frameSize=frame_size,
        fps=25.0,
        isColor=True)

    while True:
        ret, frame_in = cam.read()
        # Downsample frame - otherwise processing is too slow
        frame_in = cv2.resize(frame_in, dsize=frame_size)

        frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
        frame_out = faceWarp.face_warp(facial_mask, facial_mask_lm, frame_in)
        frame_out = cv2.cvtColor(frame_out, cv2.COLOR_GRAY2BGR)
        video_out.write(frame_out)
        faceWarp.draw_str(frame_out, (20, 20), 'ESC: stop recording  Space: stop & save video')
        cv2.imshow('webcam', frame_out)
        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break
        if ch == ord(' '):
            break

    cam.release()
    cv2.destroyAllWindows()