rect = (0, 0, sizeImg2[1], sizeImg2[0])
    dt = fbc.calculateDelaunayTriangles(rect, hull2)

    if len(dt) == 0:
        quit()

    # 仿射变化
    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]])

        fbc.warpTriangle(img1, img1Warped, t1, t2)

    print("faceswap花费{:.3f}秒".format(time.time() - t))

    tClone = time.time()
    # 在进行无缝拷贝的时候需要先创建一个模板
    mask = np.zeros(img2.shape, dtype=img2.dtype)
    cv2.fillConvexPoly(mask, np.int32(hull2), (255, 255, 255))
    r = cv2.boundingRect(np.array([hull2]))
    center = ((r[0] + int(r[2] / 2), r[1] + int(r[3] / 2)))
    output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center,
                               cv2.NORMAL_CLONE)
    print("无缝拷贝耗时{:.3f}秒".format(time.time() - tClone))
    print("总共耗时{:.3f}秒".format(time.time() - t))
    cv2.imshow("no seamless", np.uint8(img1Warped))
    cv2.imshow("seamless", np.uint8(output))
예제 #2
0
        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]])
            t2.append(featurePoints2[dt[i][j]])

        fbc.warpTriangle(beard, beardWarped, t1, t2)
        fbc.warpTriangle(beardAlphaMask, beardAlphaWarped, t1, t2)

    beardWarpedMask = beardAlphaWarped / 255
    temp1 = np.multiply(targetImage, 1.0 - beardWarpedMask)
    temp2 = np.multiply(beardWarped, beardWarpedMask)

    out = temp1 + temp2
    cv2.imshow("out", out)
    key = cv2.waitKey(0) & 0xFF
    if key == ord('s'):
        cv2.imwrite("results/beardify.jpg", np.uint8(255 * out))
예제 #3
0
                mask1 = np.zeros((warped_img.shape[0], warped_img.shape[1]),
                                 dtype=np.float32)
                mask1 = cv2.merge((mask1, mask1, mask1))
                img1_alpha_mask = cv2.merge(
                    (img1_alpha, img1_alpha, img1_alpha))

                # 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]])

                    fbc.warpTriangle(img1, warped_img, t1, t2)
                    fbc.warpTriangle(img1_alpha_mask, mask1, t1, t2)

                # Blur the mask before blending
                mask1 = cv2.GaussianBlur(mask1, (3, 3), 10)

                mask2 = (255.0, 255.0, 255.0) - mask1

                # Perform alpha blending of the two images
                temp1 = np.multiply(warped_img, (mask1 * (1.0 / 255)))
                temp2 = np.multiply(frame, (mask2 * (1.0 / 255)))
                output = temp1 + temp2
            else:
                dst_points = [
                    points2[int(list(points1.keys())[0])],
                    points2[int(list(points1.keys())[1])]
예제 #4
0
def get_swapped_image(img1, img2):
    try:
        im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
        im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
        img1Warped = np.copy(img2)

        detector = dlib.get_frontal_face_detector()

        # Read array of corresponding points
        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)

        cv2.polylines(imTemp1, np.array(tris1), True, (0, 0, 255), 2)
        cv2.polylines(imTemp2, np.array(tris2), True, (0, 0, 255), 2)

        # 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

        return
    except Exception as e:
        print(repr(e))
        raise (e)
예제 #5
0
def classify_image(event, context):
    try:
        content_type_header = event['headers']['content-type']
        body = base64.b64decode(event["body"])

        picture = decoder.MultipartDecoder(body, content_type_header)

        im_arr1 = np.frombuffer(picture.parts[0].content, dtype=np.uint8)
        img1 = cv2.imdecode(im_arr1, flags=cv2.IMREAD_COLOR)
        im_arr2 = np.frombuffer(picture.parts[1].content, dtype=np.uint8)
        img2 = cv2.imdecode(im_arr2, flags=cv2.IMREAD_COLOR)

        im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
        im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
        img1Warped = np.copy(img2)
        print('step1')

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

        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]])
        print('step3')

        # 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))
        print('step4')

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

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

        dt = fbc.calculateDelaunayTriangles(rect, hull2)

        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)

        cv2.polylines(imTemp1, np.array(tris1), True, (0, 0, 255), 2)
        cv2.polylines(imTemp2, np.array(tris2), True, (0, 0, 255), 2)
        print('step6')

        for i in range(0, len(tris1)):
            fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i])
        print('step7')

        output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center,
                                   cv2.NORMAL_CLONE)

        print('all done')

        return {
            "statusCode":
            200,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True
            },
            "body":
            json.dumps({
                'swaped_image':
                str(base64.b64encode(cv2.imencode('.jpg', output)[1]))
            })
        }
    except Exception as e:
        print(repr(e))
        return {
            "statusCode": 500,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True
            },
            "body": json.dumps({"error": repr(e)})
        }
예제 #6
0
def get_face_swap(image_bytes1, image_bytes2):
    img1 = transform_image(image_bytes=image_bytes1)
    img2 = transform_image(image_bytes=image_bytes2)

    img1Warped = np.copy(img2)

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

    print('Landmarks detected')

    # 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))

    print(f'Calculated Mask for Seamless cloning')

    # 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:
        print("ERROR: No Delaunay Triangles were found")
        #quit()

    print(f'Found Delaunay Triangles')

    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)

    print(f'Cloned seamlessly')

    # This is swapped image
    return output
예제 #7
0
def face_swap(img1, img2,predictor68):
    # Read images
    # img2 = cv2.imread('assets/amit.jpg')
    # img1 = cv2.imread('assets/arvind.jpg')

    im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    img1Warped = np.copy(img2)

    # # Display Images 
    # plt.figure(figsize = (20,10))
    # plt.subplot(121); plt.imshow(im1Display); plt.axis('off');
    # plt.subplot(122); plt.imshow(im2Display); plt.axis('off');

    # !wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
    # !bzip2 -dk shape_predictor_68_face_landmarks.dat.bz2

    # Initialize the dlib facial landmakr detector
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(predictor68)
    # Read array of corresponding points
    points1 = fbc.getLandmarks(detector, predictor, img1)
    points2 = fbc.getLandmarks(detector, predictor, img2)

    # # Display Landmarks
    # imTemp = im2Display.copy()
    # for p in points2:
    #     cv2.circle(imTemp, p, 5, (255,0,0), -1)

    # plt.figure(figsize = (20,10)); plt.imshow(imTemp); plt.axis('off');

    # 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]])

    # # Display Convex Hull
    # imTemp = im2Display.copy()
    # numPoints = len(hull2)
    # for i in range(0, numPoints):
    #     cv2.line(imTemp, hull2[i], hull2[(i+1)%numPoints], (255,0,0), 3)
    #     cv2.circle(imTemp, hull2[i], 5, (0,0,255), -1)
    # plt.figure(figsize = (20,10)); plt.imshow(imTemp); plt.axis('off');

    # 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']))

    # # Display Mask
    # plt.figure(figsize = (20,10)); plt.imshow(mask); plt.axis('off');

    # 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)

    cv2.polylines(imTemp1,np.array(tris1),True,(0,0,255),2)
    cv2.polylines(imTemp2,np.array(tris2),True,(0,0,255),2)

    # # Display Triangulation
    # plt.figure(figsize = (20,10)); 
    # plt.subplot(121); plt.imshow(imTemp1); plt.axis('off');
    # plt.subplot(122); plt.imshow(imTemp2); plt.axis('off');

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

    # plt.figure(figsize=(20,10));
    # plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off');

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

    # plt.figure(figsize=(20,10))
    # plt.subplot((121)); plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off');
    # plt.subplot((122)); plt.imshow(output[:,:,::-1]); plt.axis('off');

    # plt.figure(figsize=(20,10))
    # plt.subplot((121)); plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off');
    # plt.subplot((122)); plt.imshow(output[:,:,::-1]); plt.axis('off');
    return output
예제 #8
0
def run_face_swap(from_image, to_image, output_filename):
    """Switch faces between two input images using dlib and OpenCV."""
    # Credits to https://github.com/spmallick/
    try:
        img1 = read_url_or_local_image(from_image, im_format='cv2')
        img2 = read_url_or_local_image(to_image, im_format='cv2')
        img1Warped = np.copy(img2)
        # Initialize the dlib facial landmark 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)
        points2 = fbc.getLandmarks(detector, predictor, img2)
        # Find convex hull
        hullIndex = cv2.convexHull(
            np.array(points2).astype(np.int32), returnPoints=False
        )  # add .astype(np.int32) to fix TypeError: data type = 9 not supported
        # 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()
        # Continue triangulation
        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)
        # Apply affine transformation to Delaunay triangles
        for i in range(0, len(tris1)):
            fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i])
        # Seamless Cloning using OpenCV
        output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center,
                                   cv2.NORMAL_CLONE)
        # Write output image
        cv2.imwrite(output_filename, output)
    except Exception as e:
        print(e.message, e.args)
예제 #9
0
파일: handler.py 프로젝트: amitkayal/EVA4P2
def execute_face_swap(img1, img2):
    im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    img1Warped = np.copy(img2)
    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)
    points2 = fbc.getLandmarks(detector, predictor, img2)
    hullIndex = cv2.convexHull(np.array(points2), returnPoints=False)
    if(len(points1) == 0 or len(points2) == 0):
        print("Landmark detection failed for selected Images Source:{} Dest:{}".format(len(points1), len(points)))

    # 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]])
    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']))
    
    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()
        print("No Delaunay Triangles were found!")
        return None
    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)

    cv2.polylines(imTemp1,np.array(tris1),True,(0,0,255),2);
    cv2.polylines(imTemp2,np.array(tris2),True,(0,0,255),2);
    for i in range(0, len(tris1)):
        fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i])
    output = cv2.seamlessClone(np.uint8(img1Warped[:,:,::-1]), img2, mask, center,cv2.NORMAL_CLONE)
    ### Default scaling to 25 percent
    scale_percent = 25
    width = int(output.shape[1] * scale_percent / 100)
    height = int(output.shape[0] * scale_percent / 100)

    # dsize
    dsize = (width, height)

    # resize image
    output = cv2.resize(output, dsize)

    return output
예제 #10
0
# Get all 68 facepoints for both the images
face1_points = fbc.getLandmarks(faceDetector, landmarkDetector, face1)
face2_points = fbc.getLandmarks(faceDetector, landmarkDetector, face2)

# Based on the Dlib face points template, identify 4 points around the right and left cheeks.
# The area inside these 4 points is where the blush will be applied.
face1_Rcheek = (face1_points[35], face1_points[42], face1_points[15],
                face1_points[12])
face1_Lcheek = (face1_points[31], face1_points[39], face1_points[1],
                face1_points[4])

# Right cheek area is divided into 2 triangles i.e upper right and lower right
# Warp upper right triangle from face2 to face1
face1_tU_R = (face1_points[35], face1_points[42], face1_points[15])
face2_tU_R = (face2_points[35], face2_points[42], face2_points[15])
fbc.warpTriangle(face2, face1, face2_tU_R, face1_tU_R)
# Warp lower right triangle from face2 to face1
face1_tL_R = (face1_points[35], face1_points[15], face1_points[12])
face2_tL_R = (face2_points[35], face2_points[15], face2_points[12])
fbc.warpTriangle(face2, face1, face2_tL_R, face1_tL_R)

# Similarly left cheek area is divided into 2 triangles i.e upper left and lower left
# Warp upper left triangle from face2 to face1
face1_tU_L = (face1_points[31], face1_points[39], face1_points[1])
face2_tU_L = (face2_points[31], face2_points[39], face2_points[1])
fbc.warpTriangle(face2, face1, face2_tU_L, face1_tU_L)
# Warp lower left triangle from face2 to face1
face1_tL_L = (face1_points[31], face1_points[1], face1_points[4])
face2_tL_L = (face2_points[31], face2_points[1], face2_points[4])
fbc.warpTriangle(face2, face1, face2_tL_L, face1_tL_L)