
Stable Diffusion Agent 開發:技術解析與應用前景
Stable Diffusion API 是基于深度學習技術的文本到圖像生成工具。它利用擴散模型,通過逐步添加噪聲和去噪聲的過程生成圖像。與傳統的生成對抗網絡(GANs)相比,擴散模型在生成細節和處理復雜場景時表現更佳。Stable Diffusion API 的開放性使得用戶可以輕松地在不同的開發環境中集成和使用。
在早期的圖像生成嘗試中,Diffusers 是一個常用的庫。然而,Diffusers 在處理復雜功能時可能顯得有些繁瑣,特別是在采樣器的添加和safetensors格式模型的讀取方面。相比之下,Stable Diffusion 提供的API接口則顯得更加簡潔和直接,極大地提高了開發效率。
為了使用Stable Diffusion API,我們首先需要部署WebUI。WebUI提供了一種圖形化的方式來管理和調用API接口,無論是自己從源碼配置還是使用一鍵啟動包,過程都相對簡單且高效。
對于初學者來說,使用一鍵啟動包是最簡單的選擇。通過訪問一鍵啟動包教程,用戶可以快速安裝并運行WebUI。
如果希望對WebUI有更多的控制,可以選擇手動配置。以下是啟動WebUI的基本命令:
bash webui.sh --nowebui
或者使用Python腳本啟動:
python launch.py --xformers --api
一旦WebUI成功啟動,用戶可以通過API接口進行文生圖的生成。以下是一個簡單的Python示例,展示了如何調用API生成圖像。
通過以下代碼,我們可以實現從文本到圖像的轉換:
import json
import requests
import io
import base64
from PIL import Image
url = "http://127.0.0.1:7860"
prompt = "dog"
negative_prompt = ""
payload = {
"override_settings":{
"sd_model_checkpoint": "v1-5-pruned.ckpt",
"sd_vae": "animevae.pt",
"CLIP_stop_at_last_layers": 2,
},
"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,
"restore_faces": False,
}
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')
除了文生圖,Stable Diffusion API 還支持圖生圖功能,以下是相關示例代碼:
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 = {
"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],
"restore_faces": False,
}
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')
在使用Stable Diffusion API時,可以通過帶參數的方式啟動WebUI,以便于調用不同的API接口。以下是相關操作步驟:
在WebUI啟動腳本中添加API啟動參數:
set COMMANDLINE_ARGS=--api
完成設置后,運行webui-user.bat
,然后訪問http://127.0.0.1:7860/docs/以查看API列表。
除了直接調用官方API,用戶還可以使用第三方庫如sdwebuiapi
來簡化調用過程。這些庫對API進行了封裝,使得調用變得更為簡單。
可以通過以下命令安裝sdwebuiapi
:
pip install sdwebuiapi
以下代碼展示了如何使用sdwebuiapi
進行圖像生成:
import webuiapi
def draw_expression(charactor_expression):
api = webuiapi.WebUIApi(host='127.0.0.1', port=7860, sampler='Euler a', steps=20)
prompt = "(masterpiece, best quality:1), 1girl, solo, arisu, halo,"
for e in charactor_expression:
prompt += e + ','
prompt += "white background , "
result1 = api.txt2img(prompt=prompt,
negative_prompt="noisy, blurry, grainy,text, graphite, abstract, glitch, deformed, mutated, ugly, disfigured, (realistic, lip, nose, tooth, rouge, lipstick, eyeshadow:1.0), low contrast",
seed=-1,
styles=["animate"],
cfg_scale=8)
save_file_name = './outputs/output'
for i in charactor_expression:
save_file_name += '_' + i.replace(" ", "_")
save_file_name += '.png'
result1.image.save(save_file_name)
return save_file_name
test_expression = ['Greet happily', 'Rub your hands']
img_file_Location = draw_expression(charactor_expression=test_expression)
print('output image saved to ' + img_file_Location)
問:如何安裝Stable Diffusion WebUI?
問:Stable Diffusion API 支持哪些功能?
問:如何提高生成圖像的質量?
問:如何使用第三方庫調用Stable Diffusion API?
sdwebuiapi
等第三方庫來簡化API調用過程,這些庫提供了封裝好的接口,便于使用。問:API調用時遇到錯誤如何解決?
通過以上指導,用戶可以更好地利用Stable Diffusion API進行圖像生成,滿足多樣化的創作需求。