Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/examples/amit/masked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/examples/amit/pose.pose
Binary file not shown.
Binary file modified assets/examples/flux/masked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/examples/flux/pose.pose
Binary file not shown.
Binary file modified assets/examples/stock/masked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/examples/stock/pose.pose
Binary file not shown.
35 changes: 32 additions & 3 deletions human_avatar/image_to_avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from pathlib import Path

import numpy as np
import torch
from PIL import Image
from pose_format import Pose
from pose_format.utils.generic import pose_normalization_info
from pose_format.utils.holistic import load_holistic
from transformers import pipeline
from torchvision import transforms
from transformers import AutoModelForImageSegmentation, pipeline

CROP_RESOLUTION = 512
RMBG_INPUT_SIZE = (1024, 1024)


def extract_pose(image: Image):
Expand Down Expand Up @@ -56,9 +59,35 @@ def crop_person(image: Image, pose: Pose):
return image


@cache
def load_rmbg_model():
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-2.0", trust_remote_code=True)
torch.set_float32_matmul_precision("high")
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()
return model, device


def remove_image_background(image: Image):
model = load_huggingface_model("image-segmentation", model="briaai/RMBG-1.4")
return model(image)
model, device = load_rmbg_model()

transform_image = transforms.Compose([
transforms.Resize(RMBG_INPUT_SIZE),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])

rgb_image = image.convert("RGB")
input_tensor = transform_image(rgb_image).unsqueeze(0).to(device)

with torch.no_grad():
preds = model(input_tensor)[-1].sigmoid().cpu()
mask = transforms.ToPILImage()(preds[0].squeeze()).resize(rgb_image.size)

result = rgb_image.copy()
result.putalpha(mask)
return result


def image_to_avatar(image: Image):
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ dependencies = [
# Transformers with vision
"transformers",
"torchvision",
"scikit-image"
"scikit-image",
# Required by briaai/RMBG-2.0 trust_remote_code modeling file
"kornia",
]

[project.optional-dependencies]
Expand Down
Loading