Exemple #1
0
	def faceAligner(self, im: np.ndarray):
		points = fbc.getLandmarks(self.faceDetector, self.landmarkDetector, im)
		points = np.array(points)
		im = np.float32(im)/255.0
		h = 600	
		w = 600
		imNorm, points = fbc.normalizeImagesAndLandmarks((h,w),im,points)
		imNorm = np.uint8(imNorm*255)
		alignImage = imNorm[:,:,::-1]
		aligned_img_encoded = cv2.imencode(".jpg", cv2.cvtColor(alignImage, cv2.COLOR_BGR2RGB))
		return aligned_img_encoded
def alignImage(image):
    # Landmark model location
    MODELPATH = "/content/model/"
    PREDICTOR_PATH = MODELPATH + "shape_predictor_5_face_landmarks.dat"
    # Get the face detector
    faceDetector = dlib.get_frontal_face_detector()
    # The landmark detector is implemented in the shape_predictor class
    landmarkDetector = dlib.shape_predictor(PREDICTOR_PATH)
    # Read image
    # Detect landmarks
    points = fbc.getLandmarks(faceDetector, landmarkDetector, image)
    points = np.array(points)
    # Convert image to floating point in the range 0 to 1
    image = np.float32(image) / 255.0
    # Dimension of output image
    h = 600
    w = 600
    # Normalize image to output co-orindates
    imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), image, points)
    imNorm = np.uint8(imNorm * 255)
    return imNorm[:, :, ::-1]
Exemple #3
0
	def faceSwap( self, img_source, img_target):
		points_source = fbc.getLandmarks(self.faceDetector, self.landmarkDetector, img_source)
		points_target = fbc.getLandmarks(self.faceDetector, self.landmarkDetector, img_target)

		img_source_warped = np.copy(img_target)

		#####################Convex Hull#####################################
		# Find convex hull
		hull_index = cv2.convexHull(np.array(points_target), returnPoints=False)

		# Create convex hull lists
		hull_source = []
		hull_target = []
		for i in range(0, len(hull_index)):
		    hull_source.append(points_source[hull_index[i][0]])
		    hull_target.append(points_target[hull_index[i][0]])
		####################Hull Mask#############################################
		# Calculate Mask for Seamless cloning
		hull8U = []
		for i in range(0, len(hull_target)):
		    hull8U.append((hull_target[i][0], hull_target[i][1]))

		mask = np.zeros(img_target.shape, dtype=img_target.dtype) 
		cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255))

		# Find Centroid
		m = cv2.moments(mask[:,:,1])
		center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))

		######################Create triangulation!###############################

		# Find Delaunay traingulation for convex hull points
		img_target_size = img_target.shape    
		rect = (0, 0, img_target_size[1], img_target_size[0])

		dt = fbc.calculateDelaunayTriangles(rect, hull_target)

		# If no Delaunay Triangles were found, quit
		if len(dt) == 0:
		    quit()



		img_source_temp = img_source.copy()
		img_target_temp = img_target.copy()

		tris_source = []
		tris_target = []
		for i in range(0, len(dt)):
		    tri_source = []
		    tri_target = []
		    for j in range(0, 3):
		        tri_source.append(hull_source[dt[i][j]])
		        tri_target.append(hull_target[dt[i][j]])

		    tris_source.append(tri_source)
		    tris_target.append(tri_target)

		cv2.polylines(img_source_temp,np.array(tris_source),True,(0,0,255),2);
		cv2.polylines(img_target_temp,np.array(tris_target),True,(0,0,255),2);


		######################Swap Face################################

		# Simple Alpha Blending
		# Apply affine transformation to Delaunay triangles
		for i in range(0, len(tris_source)):
		    fbc.warpTriangle(img_source, img_source_warped, tris_source[i], tris_target[i])

		# Clone seamlessly.
		output = cv2.seamlessClone(np.uint8(img_source_warped), img_target, mask, center, cv2.NORMAL_CLONE)

		swapped_img = output[:,:,::-1]

		swapped_img_encoded = cv2.imencode(".jpg", cv2.cvtColor(swapped_img, cv2.COLOR_BGR2RGB))
		return swapped_img_encoded
def faceSwap(img1, img2):
    img1Warped = np.copy(img2)
    im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    points1 = fbc.getLandmarks(detector, predictor, img1)
    points2 = fbc.getLandmarks(detector, predictor, img2)

    # Find convex hull
    hullIndex = cv2.convexHull(np.array(points2), returnPoints=False)

    # Create convex hull lists
    hull1 = []
    hull2 = []
    for i in range(0, len(hullIndex)):
        hull1.append(points1[hullIndex[i][0]])
        hull2.append(points2[hullIndex[i][0]])

    # Calculate Mask for Seamless cloning
    hull8U = []
    for i in range(0, len(hull2)):
        hull8U.append((hull2[i][0], hull2[i][1]))

    mask = np.zeros(img2.shape, dtype=img2.dtype)
    cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255))

    # Find Centroid
    m = cv2.moments(mask[:, :, 1])
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))

    # Find Delaunay traingulation for convex hull points
    sizeImg2 = img2.shape
    rect = (0, 0, sizeImg2[1], sizeImg2[0])

    dt = fbc.calculateDelaunayTriangles(rect, hull2)

    # If no Delaunay Triangles were found, quit
    if len(dt) == 0:
        quit()

    imTemp1 = im1Display.copy()
    imTemp2 = im2Display.copy()

    tris1 = []
    tris2 = []
    for i in range(0, len(dt)):
        tri1 = []
        tri2 = []
        for j in range(0, 3):
            tri1.append(hull1[dt[i][j]])
            tri2.append(hull2[dt[i][j]])

        tris1.append(tri1)
        tris2.append(tri2)

    # Simple Alpha Blending
    # Apply affine transformation to Delaunay triangles
    for i in range(0, len(tris1)):
        fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i])

    # Clone seamlessly.
    output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center,
                               cv2.NORMAL_CLONE)
    return output[:, :, ::-1]
def faceMask(face):  # Read images
    maskImg = cv2.imread('3M-KN95-9501-Dust-Mask_v1.jpg')  # Mask image

    im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    im3Display = cv2.cvtColor(maskImg, cv2.COLOR_BGR2RGB)

    img1Warped = np.copy(img1)

    # Initialize the dlib facial landmakr detector
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    # Read array of corresponding points
    points1 = fbc.getLandmarks(detector, predictor, img1)
    points3 = fbc.getLandmarks(detector, predictor, maskImg)

    points3_new = points3[2:16]

    # Create convex hull lists
    hull1 = points1[1:16]
    hull3 = points3[1:16]

    # Calculate Mask for Seamless cloning
    hull8U = []
    for i in range(0, len(hull1)):
        hull8U.append((hull1[i][0], hull1[i][1]))

    mask = np.zeros(img1.shape, dtype=img1.dtype)
    cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255))

    # Find Centroid
    m = cv2.moments(mask[:, :, 1])
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))

    # Find Delaunay traingulation for convex hull points
    sizeImg1 = img1.shape
    rect = (0, 0, sizeImg1[1], sizeImg1[0])

    dt = fbc.calculateDelaunayTriangles(rect, hull1)

    # If no Delaunay Triangles were found, quit
    if len(dt) == 0:
        quit()

    imTemp1 = im1Display.copy()
    imTemp3 = im3Display.copy()

    tris1 = []
    tris3 = []
    for i in range(0, len(dt)):
        tri1 = []
        tri3 = []
        for j in range(0, 3):
            tri1.append(hull1[dt[i][j]])
            tri3.append(hull3[dt[i][j]])

        tris1.append(tri1)
        tris3.append(tri3)

    # Simple Alpha Blending
    # Apply affine transformation to Delaunay triangles
    for i in range(0, len(tris3)):
        fbc.warpTriangle(maskImg, img1Warped, tris3[i], tris1[i])

    # Calculate Mask for Seamless cloning
    hull8U = []
    for i in range(0, len(hull1)):
        hull8U.append((hull1[i][0], hull1[i][1]))

    mask = np.zeros(img1.shape, dtype=img1.dtype)
    cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255))

    # Find Centroid
    m = cv2.moments(mask[:, :, 1])
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))

    # Clone seamlessly.
    output = cv2.seamlessClone(np.uint8(img1Warped), img1, mask, center,
                               cv2.NORMAL_CLONE)
    return np.uint8(img1Warped)[:, :, ::-1], output[:, :, ::-1]