예제 #1
0
def get_faces_to_replace(input_data, data):
    good_image_path = data["images"][input_data["image_index"]]["path"]
    good_image = utils.get_image_by_path(good_image_path, config.FGFI_DIR)

    to_replace = []
    for face_to_replace_info in input_data["faces"]:
        group = data["groups"][face_to_replace_info["group_index"]]
        face_to_replace = group[face_to_replace_info["group_face_index"]]
        for face in group:
            if face["image_index"] == input_data["image_index"]:
                face_image = utils.get_cropped_image(data, face,
                                                     config.FGFI_DIR)
                f_width, f_height = face_image.size
                #print(f_width, f_height)
                face_to_replace_image = utils.get_cropped_image(
                    data, face_to_replace, config.FGFI_DIR)
                fr_width, fr_height = face_to_replace_image.size
                #print(fr_width, fr_height)
                width_ratio, height_ratio = calc_ratio(data, face,
                                                       face_to_replace)
                print("Scaling ratio: width {}, height {}".format(
                    width_ratio, height_ratio))
                #face_to_replace_image = face_to_replace_image.resize( (int(face_to_replace_image.size[0] * width_ratio),
                #                                                       int(face_to_replace_image.size[1] * height_ratio)) )
                #print("face_to_replace_image", face_to_replace_image.size)
                face_image_no_bg = bgremove.removebg(face_image)
                face_image_no_bg = face_image_no_bg.resize(face_image.size)

                face_to_replace_image_bg = bgremove.removebg(
                    face_to_replace_image)
                face_to_replace_image_bg = face_to_replace_image_bg.resize(
                    face_to_replace_image.size)

                face_image_mask = creatMask.copy_face_to_image(
                    good_image, face_image_no_bg, data, face["image_index"],
                    face["face_index"])

                to_replace.append({
                    "face": {
                        "data": face,
                        "image": face_image,
                        "nobg": face_image_no_bg,
                        "mask": face_image_mask,
                        "image_index": face["image_index"],
                        "face_index": face["face_index"]
                    },
                    "replacer": {
                        "data": face_to_replace,
                        "image": face_to_replace_image,
                        "nobg": face_to_replace_image_bg
                    },
                    "pos_to_replace":
                    get_pos_to_paste(data, face, face_to_replace)
                })
                break
    return to_replace
def get_faces_to_replace(input_data, data):
    good_image_path = data["images"][input_data["image_index"]]["path"]
    good_image = utils.get_image_by_path(good_image_path, config.FGFI_DIR)

    to_replace = []
    for face_to_replace_info in input_data["faces"]:
        group = data["groups"][face_to_replace_info["group_index"]]
        face_to_replace = group[face_to_replace_info["group_face_index"]]
        for face in group:
            if face["image_index"] == input_data["image_index"]:
                face_image = utils.get_cropped_image(data, face,
                                                     config.FGFI_DIR)
                f_width, f_height = face_image.size
                print(f_width, f_height)
                face_to_replace_image = utils.get_cropped_image(
                    data, face_to_replace, config.FGFI_DIR)
                fr_width, fr_height = face_to_replace_image.size
                print(fr_width, fr_height)
                ratio = f_height / float(fr_height)
                print("Scaling ratio {}".format(ratio))
                face_to_replace_image = face_to_replace_image.resize(
                    [int(ratio * s) for s in face_to_replace_image.size])
                print("face_to_replace_image", face_to_replace_image.size)
                face_image_no_bg = bgremove.removebg(face_image)
                face_to_replace_image_bg = bgremove.removebg(
                    face_to_replace_image)
                print("face_to_replace_image_bg",
                      face_to_replace_image_bg.size)
                face_image_mask = creatMask.copy_face_to_image(
                    good_image, face_image_no_bg, data, face["image_index"],
                    face["face_index"])

                to_replace.append({
                    "face": {
                        "data": face,
                        "image": face_image,
                        "nobg": face_image_no_bg,
                        "mask": face_image_mask,
                        "image_index": face["image_index"],
                        "face_index": face["face_index"]
                    },
                    "replacer": {
                        "data": face_to_replace,
                        "image": face_to_replace_image,
                        "nobg": face_to_replace_image_bg
                    }
                })
                break
    return to_replace
def get_cropped_faces(images_info):
    faces_info = []
    for image_index, image in enumerate(images_info):
        for face_index, face in enumerate(image["faces"]):
            copy = utils.get_image_by_path(image["path"]).copy()
            faces_info.append({
                "image_index":
                image_index,
                "face_index":
                face_index,
                "face":
                utils.crop_image_by_rect(copy,
                                         face["boundingPoly"]["vertices"])
            })
    return faces_info
예제 #4
0
def main():
    data = utils.from_json_file(os.path.join(JSON_DIR, "data.json"))
    input_data = utils.from_json_file(os.path.join(INPUT_DIR, "input.json"))

    good_image_path = data["images"][input_data["image_index"]]["path"]
    good_image = utils.get_image_by_path(good_image_path, config.FGFI_DIR)

    faces_to_replace = get_faces_to_replace(input_data, data)

    for i, face_to_replace in enumerate(faces_to_replace):
        #print(face_to_replace["face"]["image"].size)
        #print(face_to_replace["replacer"]["image"].size)
        #print(face_to_replace["face"]["nobg"].size)
        #print(face_to_replace["replacer"]["nobg"].size)

        width, height = face_to_replace["face"]["image"].size
        radius = math.floor(min(width, height) * 0.018)
        #print("width = {}, height = {}, radius = {}".format(width, height, radius))

        face_data = data["images"][
            face_to_replace["face"]["data"]["image_index"]]["faces"][
                face_to_replace["face"]["data"]["face_index"]]

        good_image, inpainted_mask_image = inpaint(
            good_image, face_to_replace["face"]["mask"].convert("L"),
            face_data, radius)
        mask_image = face_to_replace["face"]["mask"]

        mask_image.save(os.path.join(RESULT_DIR, "mask_{}.png".format(i)))
        good_image.save(os.path.join(RESULT_DIR, "in_{}.png".format(i)))

        x, y = face_to_replace["pos_to_replace"]["x"], face_to_replace[
            "pos_to_replace"]["y"]

        face_image_mask = creatMask.copy_face_to_image_v2(
            good_image, face_to_replace["replacer"]["nobg"], x, y,
            border=True).filter(ImageFilter.GaussianBlur(radius=1))
        face_image = creatMask.copy_face_to_image_v2(
            good_image, face_to_replace["replacer"]["nobg"], x, y, False)

        face_image_mask.save(os.path.join(RESULT_DIR, "fm_{}.png".format(i)))
        face_image.save(os.path.join(RESULT_DIR, "f_{}.png".format(i)))

        good_image = Image.composite(face_image, good_image,
                                     face_image_mask.convert("L"))
        good_image.save(os.path.join(RESULT_DIR, "comp_{}.png".format(i)))
    good_image.show()
def get_cropped_faces(images_info):
    faces_info = []
    for image_index, image in enumerate(images_info):
        for face_index, face in enumerate(image["faces"]):
            copy = utils.get_image_by_path(image["path"]).copy()
            cropped_face = utils.crop_image_by_rect(
                copy, face["boundingPoly"]["vertices"])
            thumbnail_face = cropped_face.copy()
            thumbnail_face.thumbnail((128, 128))
            faces_info.append({
                "image_index": image_index,
                "face_index": face_index,
                "face": cropped_face,
                "small": thumbnail_face
            })
    #for face in faces_info:
    #    face["face"].save(os.path.join(OUTPUT_DIR, "{}_{}.jpeg".format(face["image_index"], face["face_index"])))
    #exit()
    return faces_info
def main():
    data = utils.from_json_file(os.path.join(JSON_DIR, "data.json"))
    input_data = utils.from_json_file(os.path.join(INPUT_DIR, "input.json"))

    good_image_path = data["images"][input_data["image_index"]]["path"]
    good_image = utils.get_image_by_path(good_image_path, config.FGFI_DIR)

    faces_to_replace = get_faces_to_replace(input_data, data)

    for i, face_to_replace in enumerate(faces_to_replace):
        inpainted_array, mask_array = inpaintTest.inpaint_image(
            good_image, face_to_replace["face"]["mask"])
        inpainted_image = Image.fromarray(
            skimage.util.img_as_ubyte(inpainted_array))
        mask_image = Image.fromarray(skimage.util.img_as_ubyte(mask_array))
        mask_image.save(os.path.join(RESULT_DIR, "mask_{}.png".format(i)))
        inpainted_image = inpainted_image.resize(good_image.size)

        print(mask_image.size)
        print(good_image.size)
        print(inpainted_image.size)

        good_image = Image.composite(inpainted_image, good_image,
                                     mask_image.convert("L"))
        good_image.save(os.path.join(RESULT_DIR, "in_{}.png".format(i)))

        face_image_mask = creatMask.copy_face_to_image(
            good_image,
            face_to_replace["replacer"]["nobg"],
            data,
            face_to_replace["face"]["image_index"],
            face_to_replace["face"]["face_index"],
            border=True).filter(ImageFilter.GaussianBlur(radius=2))
        face_image = creatMask.copy_face_to_image(
            good_image, face_to_replace["replacer"]["nobg"], data,
            face_to_replace["face"]["image_index"],
            face_to_replace["face"]["face_index"], False)
        face_image_mask.save(os.path.join(RESULT_DIR, "fm_{}.png".format(i)))
        face_image.save(os.path.join(RESULT_DIR, "f_{}.png".format(i)))
        good_image = Image.composite(face_image, good_image,
                                     face_image_mask.convert("L"))
        good_image.save(os.path.join(RESULT_DIR, "comp_{}.png".format(i)))
예제 #7
0
    img2.paste(without_bg_img, top_left)

    return img2


if __name__ == "__main__":
    OUTPUT_DIR = os.path.join(config.IHS_DIR, "output")
    DEBUG_DIR = os.path.join(config.IHS_DIR, "debug")
    FGFI_OUTPUT__DIR = os.path.join(config.FGFI_DIR, "debug")

    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    nobg = utils.from_json_file(os.path.join(DEBUG_DIR, "nobg.json"))

    #get path for original image
    image_index = nobg[1]["info"]["image_index"]
    face_index = nobg[1]["info"]["face_index"]
    data = utils.from_json_file(os.path.join(FGFI_OUTPUT__DIR, "data.json"))
    path_for_image = data["images"][image_index]["path"]
    img = utils.get_image_by_path(path_for_image, config.FGFI_DIR)
    img = img.convert("RGBA")
    width, height = img.size

    ##get image after removed bg
    nobg = utils.from_json_file(os.path.join(DEBUG_DIR, "nobg.json"))
    image_from = utils.base64_image_to_image(nobg[1]["image_data"])

    creat_mask(img, image_from, data)