Beispiel #1
0
def load_filter(filter_name="dog"):

    filters = filters_config[filter_name]

    multi_filter_runtime = []

    for filter in filters:
        temp_dict = {}

        img1, img1_alpha = load_filter_img(filter['path'], filter['has_alpha'])

        temp_dict['img'] = img1
        temp_dict['img_a'] = img1_alpha

        points = load_landmarks(filter['anno_path'])

        temp_dict['points'] = points

        if filter['morph']:
            # Find convex hull for delaunay triangulation using the landmark points
            hull, hullIndex = find_convex_hull(points)

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

            temp_dict['hull'] = hull
            temp_dict['hullIndex'] = hullIndex
            temp_dict['dt'] = dt

            if len(dt) == 0:
                continue

        if filter['animated']:
            filter_cap = cv2.VideoCapture(filter['path'])
            temp_dict['cap'] = filter_cap

        multi_filter_runtime.append(temp_dict)

    return filters, multi_filter_runtime
        cv2.imshow("{}".format(i), img)

        pointsAvg = pointsAvg + (points/(1.0*numImages))

        # 添加边界点
        points = np.concatenate((points, boundaryPts), axis=0)

        pointsNorm.append(points)
        imagesNorm.append(img)

    pointsAvg = np.concatenate((pointsAvg, boundaryPts), axis=0)
    
    # 计算delaunay三角形
    rect = (0, 0, w, h)
    dt = fbc.calculateDelaunayTriangles(rect, pointsAvg)
    
    # 输出图像
    output = np.zeros((h, w, 3), dtype=np.float)
    
    for i in range(0, numImages):
        imWarp = fbc.warpImage(imagesNorm[i], pointsNorm[i], pointsAvg.tolist(), dt)

        output = output + imWarp
    
    output = output/(1.0*numImages)
    cv2.imshow("Result", output)
    cv2.waitKey(0)
    

    # 关键点检测
    points1 = fbc.getLandmarks(detector, predictor, img1)
    points2 = fbc.getLandmarks(detector, predictor, img2)

    # 检测外框
    hull1 = []
    hull2 = []

    hullIndex = cv2.convexHull(np.array(points2), returnPoints=False)
    for i in range(0, len(hullIndex)):
        hull1.append(points1[hullIndex[i][0]])
        hull2.append(points2[hullIndex[i][0]])

    sizeImg2 = img2.shape
    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)
Beispiel #4
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)})
        }
Beispiel #5
0
    imgWithMask = cv2.imread(overlayFile, cv2.IMREAD_UNCHANGED)
    b, g, r, a = cv2.split(imgWithMask)

    beard = cv2.merge((b, g, r))
    beard = np.float32(beard) / 255

    beardAlphaMask = cv2.merge((a, a, a))
    beardAlphaMask = np.float32(beardAlphaMask)

    featurePoints1 = getSavedPoints(overlayFile + ".txt")

    # Find delanauy traingulation for convex hull points
    sizeImg1 = beard.shape
    rect = (0, 0, sizeImg1[1], sizeImg1[0])
    dt = fbc.calculateDelaunayTriangles(rect, featurePoints1)

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

    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),
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)
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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)
Beispiel #10
0
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