Views
No views yet

1from huggingface_hub import hf_hub_download
2hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/config.json", local_dir="./checkpoints")
3hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/diffusion_pytorch_model.safetensors", local_dir="./checkpoints")
4hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")models/antelopev2.1# !pip install opencv-python transformers accelerate insightface
2import diffusers
3from diffusers.utils import load_image
4from diffusers.models import ControlNetModel
5
6import cv2
7import torch
8import numpy as np
9from PIL import Image
10
11from insightface.app import FaceAnalysis
12from pipeline_stable_diffusion_xl_instantid import StableDiffusionXLInstantIDPipeline, draw_kps
13
14# prepare 'antelopev2' under ./models
15app = FaceAnalysis(name='antelopev2', root='./', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
16app.prepare(ctx_id=0, det_size=(640, 640))
17
18# prepare models under ./checkpoints
19face_adapter = f'./checkpoints/ip-adapter.bin'
20controlnet_path = f'./checkpoints/ControlNetModel'
21
22# load IdentityNet
23controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
24
25pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
26... "stabilityai/stable-diffusion-xl-base-1.0", controlnet=controlnet, torch_dtype=torch.float16
27... )
28pipe.cuda()
29
30# load adapter
31pipe.load_ip_adapter_instantid(face_adapter)1# load an image
2image = load_image("your-example.jpg")
3
4# prepare face emb
5face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
6face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*x['bbox'][3]-x['bbox'][1])[-1] # only use the maximum face
7face_emb = face_info['embedding']
8face_kps = draw_kps(face_image, face_info['kps'])
9
10pipe.set_ip_adapter_scale(0.8)
11
12prompt = "analog film photo of a man. faded film, desaturated, 35mm photo, grainy, vignette, vintage, Kodachrome, Lomography, stained, highly detailed, found footage, masterpiece, best quality"
13negative_prompt = "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured (lowres, low quality, worst quality:1.2), (text:1.2), watermark, painting, drawing, illustration, glitch,deformed, mutated, cross-eyed, ugly, disfigured"
14
15# generate image
16image = pipe(
17... prompt, image_embeds=face_emb, image=face_kps, controlnet_conditioning_scale=0.8
18... ).images[0]

1@article{wang2024instantid,
2 title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
3 author={Wang, Qixun and Bai, Xu and Wang, Haofan and Qin, Zekui and Chen, Anthony},
4 journal={arXiv preprint arXiv:2401.07519},
5 year={2024}
6}