Views
No views yet
1import base64
2
3import requests
4
5HF_TOKEN = 'hf_xxxxxxxxxxxxx'
6API_ENDPOINT = 'https://xxxxxxxxxxx.us-east-1.aws.endpoints.huggingface.cloud'
7
8def load_image(path):
9 try:
10 with open(path, 'rb') as file:
11 return file.read()
12 except FileNotFoundError as error:
13 print('Error reading image:', error)
14
15
16def get_b64_image(path):
17 image_buffer = load_image(path)
18 if image_buffer:
19 return base64.b64encode(image_buffer).decode('utf-8')
20
21
22def process_images(original_image_path, mask_image_path, result_path, prompt, width, height):
23 original_b64 = get_b64_image(original_image_path)
24 mask_b64 = get_b64_image(mask_image_path)
25
26 if not original_b64 or not mask_b64:
27 return
28
29 body = {
30 'inputs': prompt,
31 'image': original_b64,
32 'mask_image': mask_b64,
33 'width': width,
34 'height': height
35 }
36
37 headers = {
38 'Authorization': f'Bearer {HF_TOKEN}',
39 'Content-Type': 'application/json',
40 'Accept': 'image/png'
41 }
42
43 response = requests.post(
44 API_ENDPOINT,
45 json=body,
46 headers=headers
47 )
48 blob = response.content
49
50 save_image(blob, result_path)
51
52
53def save_image(blob, file_path):
54 with open(file_path, 'wb') as file:
55 file.write(blob)
56 print('File saved successfully!')
57
58
59if __name__ == '__main__':
60 original_image_path = 'images/original.png'
61 mask_image_path = 'images/mask.png'
62 result_path = 'images/result.png'
63 process_images(original_image_path, mask_image_path, result_path, 'cyberpunk mona lisa', 512, 768)
64diffusers format.
It can be used in combination with Stable Diffusion, such as runwayml/stable-diffusion-v1-5.
1. Let's install `diffusers` and related packages:2. Run code:
```python
import torch
import os
from diffusers.utils import load_image
from PIL import Image
import numpy as np
from diffusers import (
ControlNetModel,
StableDiffusionControlNetPipeline,
UniPCMultistepScheduler,
)
checkpoint = "lllyasviel/control_v11p_sd15_inpaint"
original_image = load_image(
"https://huggingface.co/lllyasviel/control_v11p_sd15_inpaint/resolve/main/images/original.png"
)
mask_image = load_image(
"https://huggingface.co/lllyasviel/control_v11p_sd15_inpaint/resolve/main/images/mask.png"
)
def make_inpaint_condition(image, image_mask):
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
image_mask = np.array(image_mask.convert("L"))
assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
image[image_mask < 128] = -1.0 # set as masked pixel
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
image = torch.from_numpy(image)
return image
control_image = make_inpaint_condition(original_image, mask_image)
prompt = "best quality"
negative_prompt="lowres, bad anatomy, bad hands, cropped, worst quality"
controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
generator = torch.manual_seed(2)
image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=30,
generator=generator, image=control_image).images[0]
image.save('images/output.png')


| Model Name | Control Image Overview | Condition Image | Control Image Example | Generated Image Example |
|---|---|---|---|---|
| lllyasviel/control_v11p_sd15_canny | Trained with canny edge detection | A monochrome image with white edges on a black background. | ![]() | ![]() |
| lllyasviel/control_v11e_sd15_ip2p | Trained with pixel to pixel instruction | No condition . | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_inpaint | Trained with image inpainting | No condition. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_mlsd | Trained with multi-level line segment detection | An image with annotated line segments. | ![]() | ![]() |
| lllyasviel/control_v11f1p_sd15_depth | Trained with depth estimation | An image with depth information, usually represented as a grayscale image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_normalbae | Trained with surface normal estimation | An image with surface normal information, usually represented as a color-coded image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_seg | Trained with image segmentation | An image with segmented regions, usually represented as a color-coded image. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_lineart | Trained with line art generation | An image with line art, usually black lines on a white background. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15s2_lineart_anime | Trained with anime line art generation | An image with anime-style line art. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_openpose | Trained with human pose estimation | An image with human poses, usually represented as a set of keypoints or skeletons. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_scribble | Trained with scribble-based image generation | An image with scribbles, usually random or user-drawn strokes. | ![]() | ![]() |
| lllyasviel/control_v11p_sd15_softedge | Trained with soft edge image generation | An image with soft edges, usually to create a more painterly or artistic effect. | ![]() | ![]() |
| lllyasviel/control_v11e_sd15_shuffle | Trained with image shuffling | An image with shuffled patches or regions. | ![]() | ![]() |
| lllyasviel/control_v11f1e_sd15_tile | Trained with image tiling | A blurry image or part of an image . | ![]() | ![]() |