Ejemplo n.º 1
0
        lk_params = dict(winSize=(101, 101),
                         maxLevel=15,
                         criteria=(cv2.TERM_CRITERIA_EPS
                                   | cv2.TERM_CRITERIA_COUNT, 20, 0.001))
        points2Next, st, err = cv2.calcOpticalFlowPyrLK(
            img2GrayPrev, img2Gray, points2Prev, np.array(points2, np.float32),
            **lk_params)

        # Final landmark points are a weighted average of detected landmarks and tracked landmarks

        for k in range(0, len(points2)):
            d = cv2.norm(np.array(points2[k]) - points2Next[k])
            alpha = math.exp(-d * d / sigma)
            points2[k] = (1 - alpha) * np.array(
                points2[k]) + alpha * points2Next[k]
            points2[k] = fbc.constrainPoint(points2[k], frame.shape[1],
                                            frame.shape[0])
            points2[k] = (int(points2[k][0]), int(points2[k][1]))

        # Update variables for next pass
        points2Prev = np.array(points2, np.float32)
        img2GrayPrev = img2Gray
        ################ End of Optical Flow and Stabilization Code ###############

        if VISUALIZE_FACE_POINTS:
            for idx, point in enumerate(points2):
                cv2.circle(frame, point, 2, (255, 0, 0), -1)
                cv2.putText(frame, str(idx), point, cv2.FONT_HERSHEY_SIMPLEX,
                            .3, (255, 255, 255), 1)
            cv2.imshow("landmarks", frame)

        for idx, filter in enumerate(filters):
Ejemplo n.º 2
0
    targetImage = cv2.imread(imageFile)
    height, width = targetImage.shape[:2]
    IMAGE_RESIZE = np.float32(height) / RESIZE_HEIGHT
    targetImage = cv2.resize(targetImage,
                             None,
                             fx=1.0 / IMAGE_RESIZE,
                             fy=1.0 / IMAGE_RESIZE,
                             interpolation=cv2.INTER_LINEAR)

    points2 = fbc.getLandmarks(detector, predictor,
                               cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB),
                               FACE_DOWNSAMPLE_RATIO)
    featurePoints2 = []
    for p in selectedIndex:
        pt = points2[p]
        pt = fbc.constrainPoint(pt, width, height)
        featurePoints2.append(pt)

    targetImage = np.float32(targetImage) / 255

    beardWarped = np.zeros(targetImage.shape)
    beardAlphaWarped = np.zeros(targetImage.shape)

    # Apply affine transformation to Delaunay triangles
    for i in range(0, len(dt)):
        t1 = []
        t2 = []

        #get points for img1, img2 corresponding to the triangles
        for j in range(0, 3):
            t1.append(featurePoints1[dt[i][j]])
      img2Gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

      if(isFirstFrame == False):
        isFirstFrame = True
        hull2Prev = np.array(hull2, np.float32)
        img2GrayPrev = np.copy(img2Gray)

      lk_params = dict( winSize  = (101,101),maxLevel = 5,criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 0.001))
      hull2Next, st , err = cv2.calcOpticalFlowPyrLK(img2GrayPrev, img2Gray, hull2Prev, np.array(hull2,np.float32),**lk_params)

      # Final landmark points are a weighted average of detected landmarks and tracked landmarks
      for k in range(0,len(hull2)):
        d = cv2.norm(np.array(hull2[k]) - hull2Next[k])
        alpha = math.exp(-d*d/sigma)
        hull2[k] = (1 - alpha) * np.array(hull2[k]) + alpha * hull2Next[k]
        hull2[k] = fbc.constrainPoint(hull2[k], img2.shape[1], img2.shape[0])

      # Update varibales for next pass
      hull2Prev = np.array(hull2, np.float32)
      img2GrayPrev = img2Gray
      ################ End of Optical Flow and Stabilization Code ###############

      # Warp the triangles
      for i in range(0, len(dt)):
        t1 = []
        t2 = []

        for j in range(0, 3):
          t1.append(hull1[dt[i][j]])
          t2.append(hull2[dt[i][j]])
Ejemplo n.º 4
0
        lk_params = dict(winSize=(101, 101),
                         maxLevel=5,
                         criteria=(cv2.TERM_CRITERIA_EPS
                                   | cv2.TERM_CRITERIA_COUNT, 20, 0.001))
        featurePoints2Next, st, err = cv2.calcOpticalFlowPyrLK(
            targetGrayPrev, targetGray, featurePoints2Prev,
            np.array(featurePoints2, np.float32), **lk_params)

        # Final landmark points are a weighted average of detected landmarks and tracked landmarks
        for k in range(0, len(featurePoints2)):
            d = cv2.norm(np.array(featurePoints2[k]) - featurePoints2Next[k])
            alpha = math.exp(-d * d / sigma)
            featurePoints2[k] = (1 - alpha) * np.array(
                featurePoints2[k]) + alpha * featurePoints2Next[k]
            featurePoints2[k] = fbc.constrainPoint(featurePoints2[k],
                                                   targetImage.shape[1],
                                                   targetImage.shape[0])

        featurePoints2Prev = np.array(featurePoints2, np.float32)
        targetGrayPrev = targetGray

        #######################End of Stabilization code ##########################################################

        targetImage = np.float32(targetImage) / 255

        beardWarped = np.zeros(targetImage.shape)
        beardAlphaWarped = np.zeros(targetImage.shape)

        # Apply affine transformation to Delaunay triangles
        for i in range(0, len(dt)):
            t1 = []
Ejemplo n.º 5
0
    lk_params = dict(winSize=(101, 101),
                     maxLevel=5,
                     criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                               20, 0.001))
    landmarksNext, st, err = cv2.calcOpticalFlowPyrLK(
        srcGrayPrev, srcGray, landmarksPrev, np.array(landmarks, np.float32),
        **lk_params)

    # Final landmark points are a weighted average of detected landmarks and tracked landmarks
    for k in range(0, len(landmarks)):
        d = cv2.norm(np.array(landmarks[k]) - landmarksNext[k])
        alpha = math.exp(-d * d / sigma)
        landmarks[k] = (1 - alpha) * np.array(
            landmarks[k]) + alpha * landmarksNext[k]
        landmarks[k] = fbc.constrainPoint(landmarks[k], src.shape[1],
                                          src.shape[0])

    # Update varibales for next pass
    landmarksPrev = np.array(landmarks, np.float32)
    srcGrayPrev = srcGray
    ################ End of Optical Flow and Stabilization Code ###############

    # Set the center to tip of chin
    center1x, center1y = landmarks[8][0], landmarks[8][1]
    # Set the center to point on nose
    center2x, center2y = landmarks[28][0], landmarks[28][1]

    # Variables for storing the original and deformed points
    srcPoints = []
    dstPoints = []