コード例 #1
0
def warp_face_in_video(facial_mask_fn,
                       video_in_fn,
                       video_out_fn,
                       show_video=False):
    """
    Function to process frames in video file and 'replace' first found face by the the face from the still image.

    :param facial_mask_fn: path to the still image with a face
    :param video_in_fn: path to the input video file
    :param video_out_fn: path to the video file which will have 'replaced' face
    :param show_video: bool flag to show window with processed video frames
    """

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

    video_in = cv2.VideoCapture(video_in_fn)

    video_out = cv2.VideoWriter(
        filename=video_out_fn,
        fourcc=cv2.VideoWriter_fourcc('m', '2', 'v', '1'),
        frameSize=(int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH)),
                   int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT))),
        fps=25.0,
        isColor=True)

    total_frames_in = video_in.get(cv2.CAP_PROP_FRAME_COUNT)
    while True:
        ret, frame_in = video_in.read()
        if ret == True:
            curr_frame = video_in.get(cv2.CAP_PROP_POS_FRAMES)
            frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
            if show_video:
                cv2.imshow('video_in', frame_in)
            else:
                print '{:.2%}\r'.format(curr_frame / total_frames_in),
                sys.stdout.flush()
            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)
            if show_video: cv2.imshow('video_out', frame_out)
            cv2.waitKey(1)
        else:
            video_in.release()
            video_in = None
            video_out.release()
            video_out = None
            cv2.destroyAllWindows()
            break
コード例 #2
0
def warp_face_in_video(facial_mask_fn, video_in_fn, video_out_fn, show_video=False):
    """
    Function to process frames in video file and 'replace' first found face by the the face from the still image.

    :param facial_mask_fn: path to the still image with a face
    :param video_in_fn: path to the input video file
    :param video_out_fn: path to the video file which will have 'replaced' face
    :param show_video: bool flag to show window with processed video frames
    """

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

    video_in = cv2.VideoCapture(video_in_fn)

    video_out = cv2.VideoWriter(
        filename=video_out_fn,
        fourcc=cv2.cv.CV_FOURCC('m', 'p', '4', 'v'),
        frameSize=(int(video_in.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
                   int(video_in.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))),
        fps=25.0,
        isColor=True)

    total_frames_in = video_in.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
    while True:
        ret, frame_in = video_in.read()
        if ret == True:
            curr_frame = video_in.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
            frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
            if show_video:
                cv2.imshow('video_in', frame_in)
            else:
                print '{:.2%}\r'.format(curr_frame/total_frames_in),
                sys.stdout.flush()
            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)
            if show_video: cv2.imshow('video_out', frame_out)
            cv2.waitKey(1)
        else:
            video_in.release()
            video_in = None
            video_out.release()
            video_out = None
            cv2.destroyAllWindows()
            break
コード例 #3
0
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()
コード例 #4
0
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()
コード例 #5
0
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
	"""

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(
        "C:/Users/sonupc/Downloads/facial-landmarks/facial-landmarks/shape_predictor_68_face_landmarks.dat"
    )

    facial_mask = cv2.cvtColor(cv2.imread(facial_mask_fn), cv2.COLOR_BGR2RGB)
    facial_mask_lm = faceWarp.find_landmarks(facial_mask, faceWarp.predictor)
    #print(facial_mask.shape)

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

    ################
    frame_in = cam.read()
    frame_in = cv2.resize(frame_in, dsize=frame_size)
    frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
    print(frame_in.shape, " FRAME IN")
    print(facial_mask.shape, 'facial mask')
    print(frame_in.shape[:2], 'face_warp dst_frame dimensions')
    print(skimage.img_as_ubyte(frame_in), "skimage transform")

    #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	##function unrolled code
    src_face, src_face_lm, dst_face = facial_mask, facial_mask_lm, frame_in
    output_shape = dst_face.shape[:
                                  3]  # dimensions of our final image (from webcam eg)

    # Get the landmarks/parts for the face.
    try:
        ##dst_face_lm = find_landmarks(dst_face, predictor, opencv_facedetector=False)
        image, predictor, visualise, opencv_facedetector = dst_face, predictor, False, False
        start = timer()
        dets = detector(image, 1)
        end = timer()
        print(end - start, "dst_image detection time")
        try:
            start = timer()
            shape = predictor(image, dets[0])
            end = timer()
            print(end - start, "dst_image detection time")
            i = 0
            if visualise:
                while i < shape.num_parts:
                    p = shape.part(i)
                    cv2.circle(image, (p.x, p.y), 2, (0, 255, 0), 2)
                    i += 1
        except:
            shape = None
        dst_face_lm = shape
        # src_face_coord = _shape_to_array(src_face_lm)
        # dst_face_coord = _shape_to_array(dst_face_lm)
        src_face_coord = face_utils.shape_to_np(src_face_lm)
        dst_face_coord = face_utils.shape_to_np(dst_face_lm)
        warp_trans = skimage.transform.PiecewiseAffineTransform()
        warp_trans.estimate(dst_face_coord, src_face_coord)
        warped_face = skimage.transform.warp(src_face,
                                             warp_trans,
                                             output_shape=output_shape)
    except:
        warped_face = dst_face

    # Merge two images: new warped face and background of dst image
    # through using a mask (pixel level value is 0)
    ##frame_out = _merge_images(warped_face, dst_face)
    img_top, img_bottom, mask = warped_face, dst_face, 0
    img_top = skimage.img_as_ubyte(img_top)
    img_bottom = skimage.img_as_ubyte(img_bottom)
    merge_layer = img_top == mask
    img_top[merge_layer] = img_bottom[merge_layer]
    frame_out = img_top
    #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ################
    # 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:
        start = timer()
        frame_in = cam.read()
        # Downsample frame - otherwise processing is too slow
        #frame_in = cv2.resize(frame_in, dsize=frame_size)
        frame_in = imutils.resize(frame_in, width=400)

        frame_in = cv2.cvtColor(frame_in, cv2.COLOR_BGR2RGB)

        #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        # gray = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY)
        # # detect faces in the grayscale frame
        # rects = detector(gray, 0)
        # 	# loop over the face detections
        # for rect in rects:
        # 	# determine the facial landmarks for the face region, then
        # 	# convert the facial landmark (x, y)-coordinates to a NumPy
        # 	# array
        # 	shape = predictor(gray, rect)
        # 	shape = face_utils.shape_to_np(shape)

        # 	# loop over the (x, y)-coordinates for the facial landmarks
        # 	# and draw them on the image
        # 	for (x, y) in shape:
        # 		cv2.circle(frame_in, (x, y), 1, (0, 0, 255), -1)

        # # show the frame
        # cv2.imshow("Frame", frame_in)
        #key = cv2.waitKey(1) & 0xFF
        #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

        #frame_out = faceWarp.face_warp(facial_mask, facial_mask_lm, frame_in)
        #frame_out = cv2.cvtColor(frame_out, cv2.COLOR_RGB2BGR)

        #video_out.write(frame_out)
        #faceWarp.draw_str(frame_out, (20, 20), 'ESC: stop recording  Space: stop & save video')

        #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	##function unrolled code
        src_face, src_face_lm, dst_face = facial_mask, facial_mask_lm, frame_in
        output_shape = dst_face.shape[:
                                      3]  # dimensions of our final image (from webcam eg)

        # Get the landmarks/parts for the face.
        try:
            ##dst_face_lm = find_landmarks(dst_face, predictor, opencv_facedetector=False)
            image, predictor, visualise, opencv_facedetector = dst_face, predictor, False, False
            dets = detector(image, 1)
            try:
                shape = predictor(image, dets[0])
                i = 0
                if visualise:
                    while i < shape.num_parts:
                        p = shape.part(i)
                        cv2.circle(image, (p.x, p.y), 2, (0, 255, 0), 2)
                        i += 1
            except:
                shape = None
            dst_face_lm = shape
            # src_face_coord = _shape_to_array(src_face_lm)
            # dst_face_coord = _shape_to_array(dst_face_lm)
            src_face_coord = face_utils.shape_to_np(src_face_lm)
            dst_face_coord = face_utils.shape_to_np(dst_face_lm)
            warp_trans = skimage.transform.PiecewiseAffineTransform()
            warp_trans.estimate(dst_face_coord, src_face_coord)
            warped_face = skimage.transform.warp(src_face,
                                                 warp_trans,
                                                 output_shape=output_shape)

        except:
            warped_face = dst_face

        # Merge two images: new warped face and background of dst image
        # through using a mask (pixel level value is 0)
        ##frame_out = _merge_images(warped_face, dst_face)
        img_top, img_bottom, mask = warped_face, dst_face, 0
        img_top = skimage.img_as_ubyte(img_top)
        img_bottom = skimage.img_as_ubyte(img_bottom)
        merge_layer = img_top == mask
        img_top[merge_layer] = img_bottom[merge_layer]
        frame_out = img_top
        frame_out = cv2.cvtColor(frame_out, cv2.COLOR_RGB2BGR)
        #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        cv2.imshow('webcam', frame_out)

        end = timer()
        print("FPS: ", 1.0 / (end - start))

        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break
        if ch == ord(' '):
            break

    #cam.release()
    cv2.destroyAllWindows()
    cam.stop()
コード例 #6
0
    def warp_face_from_webcam(self):

        previousNum = 0
        newNumCount = 0

        video_out_fn = './demo/demo_arni.mov'
        facial_mask_fn = './demo/' + self.current_filter
        """
        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_HSV2RGB)
        facial_mask = cv2.imread(facial_mask_fn)
        facial_mask_lm = faceWarp.find_landmarks(facial_mask,
                                                 faceWarp.predictor)

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

        frame_width = int(cam.get(3))
        frame_height = int(cam.get(4))

        # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        #out = cv2.VideoWriter('a.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 20, (frame_width,frame_height))

        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=50.0,
            isColor=True)

        def nothing():
            pass

        cv2.namedWindow('Sliders')
        cv2.createTrackbar("erode", "Sliders", 2, 10, nothing)
        cv2.createTrackbar("thresh", "Sliders", 70, 249, nothing)

        cv2.createTrackbar('R', 'Sliders', 0, 255, nothing)
        cv2.createTrackbar('G', 'Sliders', 90, 255, nothing)
        cv2.createTrackbar('B', 'Sliders', 0, 255, nothing)
        while True:
            erode = cv2.getTrackbarPos("erode", "Sliders")
            thresh = cv2.getTrackbarPos("thresh", "Sliders")

            r = cv2.getTrackbarPos("R", "Sliders")
            g = cv2.getTrackbarPos("G", "Sliders")
            b = cv2.getTrackbarPos("B", "Sliders")

            ret, frame_in = cam.read()
            frame_in = cv2.flip(frame_in, 1)
            kernel = np.ones((3, 3), np.uint8)
            roi = frame_in[0:900, 0:900]

            #cv2.rectangle(frame_in,(0,0),(900,900),(0,255,0),0)
            hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

            lower_skin = np.array([r, g, b], dtype=np.uint8)
            upper_skin = np.array([20, 255, 255], dtype=np.uint8)

            mask = cv2.inRange(hsv, lower_skin, upper_skin)

            mask = cv2.erode(mask, kernel, iterations=erode)

            mask = cv2.GaussianBlur(mask, (5, 5), 200)

            ret, thresh = cv2.threshold(mask, thresh, 255, 0)

            mask = cv2.resize(mask, (80, 60))
            #mask = cv2.cvtColor(mask,cv2.COLOR_GRAY2RGB)
            cv2.namedWindow("mask", cv2.WINDOW_NORMAL)
            cv2.resizeWindow('mask', 320, 240)
            cv2.moveWindow('mask', 980, 0)
            cv2.imshow('mask', mask)
            mask = mask.astype("float") / 255.0
            mask = img_to_array(mask)
            mask = np.expand_dims(mask, axis=0)

            (none, dabL, dabR, hitL, hitR, whipL, whipR, tPose, shoot,
             keke) = model.predict(mask)[0]
            values = [
                none, dabL, dabR, hitL, hitR, whipL, whipR, tPose, shoot, keke
            ]
            currentNum = values.index(max(values))

            if (currentNum == previousNum):
                if (newNumCount < 7):
                    newNumCount = newNumCount + 1
                print(str(newNumCount) + " occurences of " + str(currentNum))
            else:
                newNumCount = 0
                previousNum = currentNum
                print("New number: " + str(currentNum) + ", " +
                      str(previousNum))

            if (newNumCount == 3):
                print("Play")
                if currentNum == 1:
                    mMusicPlayer.PlaySound(0)
                if currentNum == 2:
                    mMusicPlayer.PlaySound(1)
                if currentNum == 3:
                    mMusicPlayer.PlaySound(2)
                if currentNum == 4:
                    mMusicPlayer.PlaySound(3)
                if currentNum == 5:
                    mMusicPlayer.PlaySound(4)
                if currentNum == 6:
                    mMusicPlayer.PlaySound(5)
                if currentNum == 7:
                    mMusicPlayer.PlaySound(6)
                if currentNum == 8:
                    mMusicPlayer.PlaySound(7)
                if currentNum == 9:
                    mMusicPlayer.PlaySound(8)

            # Downsample frame - otherwise processing is too slow
            frame_in = cv2.resize(frame_in, dsize=frame_size)
            #frame_in = cv2.cvtColor(frame_in, cv2.COLOR_HSV2RGB)
            frame_out = faceWarp.face_warp(facial_mask, facial_mask_lm,
                                           frame_in)
            if self.recording:
                video_out.write(frame_out)
            cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
            cv2.resizeWindow('Recording', 700, 525)
            cv2.moveWindow("Recording", 280, 0)
            cv2.imshow('Recording', frame_out)
            ch = 0xFF & cv2.waitKey(1)
            if ch == 27:
                break
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            if ch == ord(' '):
                break

        cam.release()
        video_out.release()
        cv2.destroyAllWindows()