前情提要
在之前嘗試使用Diffusers庫(kù)來(lái)進(jìn)行stable-diffusion的接口調(diào)用以及各種插件功能實(shí)現(xiàn),但發(fā)現(xiàn)diffusers庫(kù)中各復(fù)雜功能的添加較為麻煩,而且難以實(shí)現(xiàn)對(duì)采樣器的添加,safetensors格式模型的讀取。在官網(wǎng)上找到了webui有專(zhuān)門(mén)的api接口,能夠極大方便我們進(jìn)行類(lèi)似webui界面的api調(diào)用。
diffusers文檔
webui項(xiàng)目官網(wǎng)
webui API說(shuō)明
webui項(xiàng)目部署
這種調(diào)用webui自帶的api的方法需要先將webui運(yùn)行起來(lái),無(wú)論是自己從官網(wǎng)配置的webui,還是各類(lèi)啟動(dòng)器一鍵啟動(dòng)的都是可以的。(我使用的為一鍵啟動(dòng)包,較為簡(jiǎn)單)
一鍵啟動(dòng)包教程
如果是自己配置的
使用
bash webui.sh --nowebui
或者
python launch.py --xformers --api
API接口調(diào)用
當(dāng)我們把webui項(xiàng)目啟動(dòng)之后,我們可以看到運(yùn)行的端口(默認(rèn)為7860)
可以進(jìn)行調(diào)用
1. 文生圖(python示例):
import json
import requests
import io
import base64
from PIL import Image
url = "http://127.0.0.1:7860"
prompt = "dog"
negative_prompt = ""
payload = {
# 模型設(shè)置
"override_settings":{
"sd_model_checkpoint": "v1-5-pruned.ckpt",
"sd_vae": "animevae.pt",
"CLIP_stop_at_last_layers": 2,
},
# 基本參數(shù)
"prompt": prompt,
"negative_prompt": negative_prompt,
"steps": 30,
"sampler_name": "Euler a",
"width": 512,
"height": 512,
"batch_size": 1,
"n_iter": 1,
"seed": 1,
"CLIP_stop_at_last_layers": 2,
# 面部修復(fù) face fix
"restore_faces": False,
#高清修復(fù) highres fix
# "enable_hr": True,
# "denoising_strength": 0.4,
# "hr_scale": 2,
# "hr_upscaler": "Latent",
}
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
r = response.json()
image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
image.show()
image.save('output.png')
2. 圖生圖(python 示例)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-796954.html
import json
import requests
import io
import base64
from PIL import Image
import cv2
url = "http://127.0.0.1:7860"
prompt = "cat"
negative_prompt = ""
# 此處為讀取一張圖片作為輸入圖像
img = cv2.imread('image.jpg')
# 編碼圖像
retval, bytes = cv2.imencode('.png', img)
encoded_image = base64.b64encode(bytes).decode('utf-8')
payload = {
# # 模型設(shè)置
# "override_settings":{
# "sd_model_checkpoint": "v1-5-pruned.ckpt",
# "sd_vae": "animevae.pt",
# "CLIP_stop_at_last_layers": 2,
# },
# 基本參數(shù)
"prompt": prompt,
"negative_prompt": negative_prompt,
"steps": 30,
"sampler_name": "Euler a",
"width": 512,
"height": 512,
"batch_size": 1,
"n_iter": 1,
"seed": 1,
"cfg_scale": 7,
"CLIP_stop_at_last_layers": 2,
"init_images": [encoded_image],
# 面部修復(fù) face fix
"restore_faces": False,
#高清修復(fù) highres fix
# "enable_hr": True,
# "denoising_strength": 0.4,
# "hr_scale": 2,
# "hr_upscaler": "Latent",
}
response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
r = response.json()
image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
image.show()
image.save('output.png')
如要修改其他參數(shù)可參照官網(wǎng)文檔進(jìn)行修改。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-796954.html
到了這里,關(guān)于Stable-diffusion-WebUI 的API調(diào)用(內(nèi)含文生圖和圖生圖實(shí)例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!