faceSwap.py
def warpTriangle(self, img1, img2, t1, t2):
        """Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1.

        Parameters
        ----------
        img1 : numpy.ndarray
            output of image1 read by opencv.
        img2 : numpy.ndarray
            output of image2 read by opencv.
        t1 : tuple
            a single triangle points of image1.
        t2 : tuple
            a single triangle points of image2.

        Returns
        -------
            does not return any value.

        """

        # Find bounding rectangle for each triangle
        r1 = cv2.boundingRect(np.array(t1))
        r2 = cv2.boundingRect(np.array(t2))

        # Offset points by left top corner of the respective rectangles
        t1_offset = []
        t2_offset = []

        for i in range(3):
            t1_offset.append(((t1[i][0] - r1[0]), (t1[i][1] - r1[1])))
            t2_offset.append(((t2[i][0] - r2[0]), (t2[i][1] - r2[1])))

        # Apply warpImage to small rectangular patches
        img1_roi = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]

        size = (r2[2], r2[3])  # size = (w, h) or (x, y)
        img2_roi = self.applyAffineTransform(
            img1_roi, t1_offset, t2_offset, size)

        # Get mask by filling triangle
        mask = np.zeros((r2[3], r2[2], 3), dtype=np.float32)
        cv2.fillConvexPoly(mask, np.int32(t2_offset), (1.0, 1.0, 1.0))
        img2_roi = img2_roi * mask

        # Copy triangular region of the rectangular patch to the output image
        img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] = img2[r2[1]:r2[1] + r2[3],
                                                              r2[0]:r2[0] + r2[2]] * ((1.0, 1.0, 1.0) - mask) + img2_roi
Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1.

        Parameters
        ----------
        img1 : numpy.ndarray
            output of image1 read by opencv.
        img2 : numpy.ndarray
            output of image2 read by opencv.
        t1 : tuple
            a single triangle points of image1.
        t2 : tuple
            a single triangle points of image2.

        Returns
        -------
            does not return any value.

        
Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1.
Parameters

img1 : numpy.ndarray
output of image1 read by opencv.
img2 : numpy.ndarray
output of image2 read by opencv.
t1 : tuple
a single triangle points of image1.
t2 : tuple
a single triangle points of image2.

Returns

does not return any value.
Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1 . Parameters img1 : numpy . ndarray output of image1 read by opencv . img2 : numpy . ndarray output of image2 read by opencv . t1 : tuple a single triangle points of image1 . t2 : tuple a single triangle points of image2 . Returns does not return any value .
def warpTriangle(self, img1, img2, t1, t2):
        r1 = cv2.boundingRect(np.array(t1))
        r2 = cv2.boundingRect(np.array(t2))
        t1_offset = []
        t2_offset = []
        for i in range(3):
            t1_offset.append(((t1[i][0] - r1[0]), (t1[i][1] - r1[1])))
            t2_offset.append(((t2[i][0] - r2[0]), (t2[i][1] - r2[1])))
        img1_roi = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]
        size = (r2[2], r2[3])  
        img2_roi = self.applyAffineTransform(
            img1_roi, t1_offset, t2_offset, size)
        mask = np.zeros((r2[3], r2[2], 3), dtype=np.float32)
        cv2.fillConvexPoly(mask, np.int32(t2_offset), (1.0, 1.0, 1.0))
        img2_roi = img2_roi * mask
        img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] = img2[r2[1]:r2[1] + r2[3],
                                                              r2[0]:r2[0] + r2[2]] * ((1.0, 1.0, 1.0) - mask) + img2_roi
def warpTriangle ( self , img1 , img2 , t1 , t2 ) : r1 = cv2 . boundingRect ( np . array ( t1 ) ) r2 = cv2 . boundingRect ( np . array ( t2 ) ) t1_offset = [ ] t2_offset = [ ] for i in range ( 3 ) : t1_offset . append ( ( ( t1 [ i ] [ 0 ] - r1 [ 0 ] ) , ( t1 [ i ] [ 1 ] - r1 [ 1 ] ) ) ) t2_offset . append ( ( ( t2 [ i ] [ 0 ] - r2 [ 0 ] ) , ( t2 [ i ] [ 1 ] - r2 [ 1 ] ) ) ) img1_roi = img1 [ r1 [ 1 ] : r1 [ 1 ] + r1 [ 3 ] , r1 [ 0 ] : r1 [ 0 ] + r1 [ 2 ] ] size = ( r2 [ 2 ] , r2 [ 3 ] ) img2_roi = self . applyAffineTransform ( img1_roi , t1_offset , t2_offset , size ) mask = np . zeros ( ( r2 [ 3 ] , r2 [ 2 ] , 3 ) , dtype = np . float32 ) cv2 . fillConvexPoly ( mask , np . int32 ( t2_offset ) , ( 1.0 , 1.0 , 1.0 ) ) img2_roi = img2_roi * mask img2 [ r2 [ 1 ] : r2 [ 1 ] + r2 [ 3 ] , r2 [ 0 ] : r2 [ 0 ] + r2 [ 2 ] ] = img2 [ r2 [ 1 ] : r2 [ 1 ] + r2 [ 3 ] , r2 [ 0 ] : r2 [ 0 ] + r2 [ 2 ] ] * ( ( 1.0 , 1.0 , 1.0 ) - mask ) + img2_roi
Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1.
Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1 .
"""Warp t1 to t2 then replace triangle 2 portion of img2 by triangle 1 portion of img1.

        Parameters
        ----------
        img1 : numpy.ndarray
            output of image1 read by opencv.
        img2 : numpy.ndarray
            output of image2 read by opencv.
        t1 : tuple
            a single triangle points of image1.
        t2 : tuple
            a single triangle points of image2.

        Returns
        -------
            does not return any value.

        """ # Find bounding rectangle for each triangle # Offset points by left top corner of the respective rectangles # Apply warpImage to small rectangular patches # size = (w, h) or (x, y) # Get mask by filling triangle # Copy triangular region of the rectangular patch to the output image
self
img1
img2
t1
t2