一、介绍

1、该服务是基于SD Web UI搭建而成,采用sd-v1-5-inpainting模型,提供给AI Art Generator、AI Avatar项目使用,支持文字转图片、文字+图片转图片功能;

2、UI通过Web UI界面,调整参数,生成效果图;后台通过调用API接口传递UI调整的参数,生成所需要的图片,提供给前端使用;

二、搭建步骤

1、使用https://colab.research.google.com/github/camenduru/stable-diffusion-webui-colab/blob/main/stable/stable_diffusion_inpainting_webui_colab.ipynb里面的代码,搭建环境;

2、启动服务时(上述代码的最后一句),添加–api参数;
python launch.py --xformers --enable-insecure-extension-access --theme dark --api # 本地服务
python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --api # 外部服务

三、API调用

1、文字转图片

import requests
import io
import base64
from PIL import Image
 
def txt2img():
    url = "http://xx.com"
 
    payload = {
        "prompt": "cat",
         "negative_prompt": "xx",
         "steps": 20,
    }
 
    response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
    images = response.json()['images']
    if len(images) > 0:
        image_data = images[0]
        image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
        image.save('output1.png')
 
txt2img()

2、文字+图片转图片

import requests
import io
import base64
from PIL import Image
 
def img2img():
    url = "http://xx.com"
 
    payload = {
        "prompt": "cat",
         "negative_prompt": "xx", 
         "steps": 20,
        "init_images": ["data:image/png;base64," + base64.b64encode(open('input.png', 'rb').read()).decode('utf-8')]
    }
 
    response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
    images = response.json()['images']
    if len(images) > 0:
        image_data = images[0]
        image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
        image.save('output1.png')
 
img2img()

3、更详细的文字+图片转图片

import requests
import io
import base64
from PIL import Image
 
def img2img_more():
    url = "http://xx.com"
    prompt = "((watercolor)), colorful, close-up, front, extreme detail, detailed, 8k, nice face, Portrait, flowing, fresh"
    negative_prompt = "ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature, cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face, blurry, draft, grainy"
 
    payload = {
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "steps": 30,
        "cfg_scale": 15.0,
        "denoising_strength": 0.65,
        "sampler_name": "Euler a",
        "inpainting_mask_weight": 0.35,
        "extra_generation_params": {"Mask blur": 4},
        "seed_resize_from_w": -1,
        "seed_resize_from_h": -1,
        "init_images": ["data:image/png;base64," + base64.b64encode(open('xx.png', 'rb').read()).decode('utf-8')]
    }
 
    response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
    images = response.json()['images']
    if len(images) > 0:
        image_data = images[0]
        image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
        image.save('output1.png')
 
img2img_more()

参考:https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API

标签: none

已有 14 条评论

  1. 想想你的文章写的特别好

  2. 你的文章让我感受到了生活的美好,谢谢! https://www.yonboz.com/video/89940.html

  3. 你的文章内容非常卖力,让人点赞。 https://www.yonboz.com/video/86792.html

  4. 你的文章让我心情愉悦,每天都要来看一看。 http://www.55baobei.com/GM5Ca2mGDT.html

  5. 《爱神》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/14381.html

  6. 你的文章充满了欢乐,让人忍不住一笑。 https://www.4006400989.com/qyvideo/15487.html

  7. 哈哈哈,写的太好了https://www.lawjida.com/

  8. 多语种文献的引用彰显学术包容性。

  9. 每个标点都承载着思考的重量。

  10. 操作步骤清晰,指导性强,易于实践。

  11. 建议融入东方智慧,形成对话张力。

  12. 作者对主题的挖掘深入骨髓,展现了非凡的洞察力和理解力。

  13. 这篇文章不错!

  14. 这篇文章不错!

添加新评论