Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best Solution for Face Parsing (Segmentation) #36

Open
XavierJiezou opened this issue Dec 18, 2024 · 0 comments
Open

Best Solution for Face Parsing (Segmentation) #36

XavierJiezou opened this issue Dec 18, 2024 · 0 comments

Comments

@XavierJiezou
Copy link

Demo

  • Input
    image
  • Output
    image

Install

pip install torch pyfacer==0.04

Code

import torch
import facer
import numpy as np
from PIL import Image


def save_result(mask, output_path):
    palette = np.array(
        [
            [0, 0, 0],        # "background"
            [255, 153, 51],   # "neck"
            [204, 0, 0],      # "face"
            [0, 204, 0],      # "cloth"
            [102, 51, 0],     # "rr"
            [255, 0, 0],      # "lr"
            [0, 255, 255],    # "rb"
            [255, 204, 204],  # "lb"
            [51, 51, 255],    # "re"
            [204, 0, 204],    # "le"
            [76, 153, 0],     # "nose"
            [102, 204, 0],    # "imouth"
            [0, 0, 153],      # "llip"
            [255, 255, 0],    # "ulip"
            [0, 0, 204],      # "hair"
            [204, 204, 0],    # "eyeg"
            [255, 51, 153],   # "hat"
            [0, 204, 204],    # "earr"
            [0, 51, 0],       # "neck_l"
        ],
        dtype=np.uint8,
    )
    mask = mask.squeeze(0).cpu().byte().numpy()
    mask = Image.fromarray(mask, mode="P")
    mask.putpalette(palette.flatten())
    mask.save(output_path)


def inference(input_path, output_path):
    device = "cuda" if torch.cuda.is_available() else "cpu"

    image = facer.hwc2bchw(facer.read_hwc(input_path)).to(
        device
    )  # image: 1 x 3 x h x w

    model = facer.face_parser(
        "farl/celebm/448",
        device,
        model_path="https://github.com/FacePerceiver/facer/releases/download/models-v1/face_parsing.farl.celebm.main_ema_181500_jit.pt",
    )

    with torch.inference_mode():
        logits, _ = model.net(image / 255.0)
        mask = logits.argmax(dim=1)

    save_result(mask, output_path)


if __name__ == "__main__":
    inference("input.jpg", "output.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant