Created ControlNet Web API (markdown)

Chenlei Hu
2024-02-10 13:30:48 -05:00
parent 179615d0e8
commit bfc9130dc0

50
ControlNet-Web-API.md Normal file

@@ -0,0 +1,50 @@
This page is a guide on how to use ControlNet's Web API.
You need to pass parameters to ControlNet script, when you use [A1111's txt2img/img2img generation API](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API).
Here is an example payload in Python:
```python
import base64
import cv2
import requests
def read_image(img_path: Path) -> str:
img = cv2.imread(str(img_path))
_, bytes = cv2.imencode(".png", img)
encoded_image = base64.b64encode(bytes).decode("utf-8")
return encoded_image
# Note: You can no longer pass file path in payload as in sd-webui-controlnet.
base64image = read_image("D:/foo.png")
base64mask = read_image("D:/bar.png")
unit1 = {
"image": base64image,
"mask_image": base64mask,
"control_mode": "Balanced", # "Balanced", "My prompt is more important", "ControlNet is more important"
"enabled": True,
"guidance_end": 1,
"guidance_start": 0,
"pixel_perfect": True,
"processor_res": 512,
"resize_mode": "Just Resize", # "Just Resize", "Crop and Resize", "Resize and Fill"
"threshold_a": 64,
"threshold_b": 64,
"weight": 1,
"module": "canny",
"model": "sd15_canny.pth",
"save_detected_map": True,
"hr_option": "Both", # "Both", "High res only", "Low res only"
}
unit2 = ...
unit3 = ...
payload = {
"alwayson_scripts": {"ControlNet": {"args": [unit1, unit2, unit3]}},
"batch_size": 1,
"cfg_scale": 7,
... (other parameters)
}
resp = requests.post("http://localhost:7860/sdapi/v1/txt2img", json=payload)
imgs = resp.json()["images"]
```