Stable Diffusion 插件开发基础讲解

news/2024/7/10 19:49:30 标签: stable diffusion, ai, ai作画, 计算机视觉
aidu_pl">

近来Stable diffusion扩散网络大热,跟上时代,简单的文生图,图生图,其实可以满足绝大多数设计师的应用,但是有什么是赛博画手无法做到的呢?
那就是他们使用到的stable diffusion的插件开发,他们并不清楚stable diffusino的代码结构,如果遇到一些代码层面的报错问题,他们将无法简单解决。
我们想要开发出我想要的stable diffusion插件。那么我们首先要去学习一些gradio的基础知识。
Gradio接口文档
1.想要了解stable diffusion的插件的形式,插件基本都是放在extension文件夹里面。
启动器提供通过git下载对应的内容。
其实就是通过直接copy github里面的代码来实现插件的。

2.以一个简单ffmpeg嵌入倒放视频的功能为例吧

启动的时候需要安装一些库,需要准备install.py文件会自动运行代码

import launch
if not launch.is_installed("ffmpeg-python"):
    launch.run_pip("install ffmpeg-python", "requirements for TemporalKit extension")

if not launch.is_installed("moviepy"):
    launch.run_pip("install moviepy", "requirements for TemporalKit extension")
    
if not launch.is_installed("imageio_ffmpeg"):
    launch.run_pip("install imageio_ffmpeg", "requirements for TemporalKit extension")

requirement.txt最好也准备一些你需要的库

ffmpeg-python
moviepy

3.一个启动简单的启动代码,看不懂的可以看注释,这个例子简单包含按钮,滑动条,视频展示等容器。如果需要查看更多的容器,需要去看gradio api

import gradio as gr
from modules import scripts, script_callbacks
import os
import ffmpeg

# base_dir = scripts.basedir()

#ffmpeg倒放命令
def convert_video(input_file: str, output_directory: str, speed: int, reverse: bool):
  #ffmpeg -i G:\1\c6cfb2d13929eb4967417e0bd81c314c.mp4 -vf reverse -y reverse.mp4
  fileName = os.path.basename(input_file)
  outputFile = os.path.join(output_directory, fileName)
  ffm = ffmpeg.input(input_file)
  if speed != 1 :
    ffm = ffm.filter('setpts', f'PTS/{speed}')

  if reverse :
    ffm = ffm.filter("reverse")

  ffm.output(outputFile).run()
  return outputFile

def on_ui_tabs():
  with gr.Blocks(analytics_enabled=False) as ffmpeg_kit_ui:
    with gr.Row():
      with gr.Column(variant="panel"):
        with gr.Column():
          video_file = gr.Textbox(
            label="Video File",
            placeholder="Wrire your video file address",
            value="",
            interactive=True,
          )
          org_video = gr.Video(
            interactive=True, mirror_webcam=False
          )
          def fn_upload_org_video(video):
            return video
          org_video.upload(fn_upload_org_video, org_video, video_file)
          gr.HTML(value="<p style='margin-bottom: 1.2em'>\
            If you have trouble entering the video path manually, you can also use drag and drop.For large videos, please enter the path manually. \
          </p>")
        with gr.Column():
          output_directory = gr.Textbox(
            label="Video Output Directory",
            placeholder="Directory containing your output files",
            value="",
            interactive=True,
          )
        with gr.Column():
          with gr.Row():
            speed_slider = gr.Slider(
              label="Video Speed",
              minimum=0, maximum=8,
              step=0.1,
              value=1
            )
          with gr.Row():
            reverse_checkbox = gr.Checkbox(
              label="Video need reverse",
              value=False
            )
      with gr.Column(variant="panel"):
        with gr.Row():
          convert_video_btn = gr.Button(
            "Convert Video", label="Convert Video", variant="primary"
          )
        with gr.Row():
          dst_video = gr.Video(
            interactive=True, mirror_webcam=False
          )
        #生成按钮
        convert_video_btn.click(
          convert_video,
          inputs=[
            video_file,
            output_directory,
            speed_slider,
            reverse_checkbox
          ],
          outputs=dst_video
        )
        gr.HTML(value="<p>Converts video in a folder</p>")
  #ui布局 扩展模块名 
  return (ffmpeg_kit_ui, "FFmpeg Kit", "ffmpeg_kit_ui"),

#启动的时候,script_callback加载到扩展模块当中
script_callbacks.on_ui_tabs(on_ui_tabs)
print("FFmpeg kit init")

这里只是一个ffmpeg的功能嵌入,并没有包含原来一些文生图,图生图的功能。
4.下一个介绍如何嵌入功能到文生图或者图生图的脚本功能

def title(self):设置脚本的标题,例如设想为“prompt matrix”
def show(self, is_img2img):决定脚本标签是否可以显示,比如如果脚本只想展示在img2img标签中,那便需要在show方法中做判断
def ui(self, is_img2img):ui界面的相关代码,例如“把可变部分放在提示词文本的开头”“为每张图片使用不同随机种子”这些选项的实现。
def run(self, p, angle, hflip, vflip, overwrite):额外的处理流程,例如在这里改变prompt,然后传给下一步。这里p是图片处理器,里面的参数是可以读取ui里面的返回的[]对象

#加载到文生图或者图生图的应用过程当中
class FFmpegKitScript(scripts.Script):
  def __init__(self) -> None:
    super().__init__()
  # 功能块名
  def title(self):
    return "FFmpeg Kit"
  #是否默认显示
  def show(self, is_img2img):
      return scripts.AlwaysVisible
  
  #ui显示
  def ui(self, is_img2img):
    video_file = gr.Textbox(
      label="Video File",
      placeholder="Wrire your video file address",
      value="",
      interactive=True,
    )
    output_directory = gr.Textbox(
      label="Video Output Directory",
      placeholder="Directory containing your output files",
      value="",
      interactive=True,
    )

    generateBtn = gr.Button("Generate", label="Generate", variant="primary")

    generateBtn.click(
      convert_video,
      inputs=[
        video_file,
        output_directory
      ],
      outputs=[]
    )

    return [
      video_file,
      output_directory,
      generateBtn,
    ]
  #运行的时候嵌入运行
  def run(self, video_file, output_directory, generateBtn):
    return

4.添加到设定页面里面
这里需要调用script_callbacks.on_ui_settings方法
shared.opts.add_optioin是添加公共的设置,Shared.OptionsInfo里面是对应的布局,对应都是return对象。
实在不知道怎么写的同学可以参照infinite_zoom这个插件。

vbnet复制代码def on_ui_settings():
    section = ("infinite-zoom", "Infinite Zoom")

    shared.opts.add_option(
        "infzoom_outpath",
        shared.OptionInfo(
            "outputs",
            "Path where to store your infinite video. Default is Outputs",
            gr.Textbox,
            {"interactive": True},
            section=section,    
        ),
    )

    shared.opts.add_option(
        "infzoom_outSUBpath",
        shared.OptionInfo(
            "infinite-zooms",
            "Which subfolder name to be created in the outpath. Default is 'infinite-zooms'",
            gr.Textbox,
            {"interactive": True},
            section=section,
        ),
    )

script_callbacks.on_ui_settings方法
shared.opts.add_optioin是添加公共的设置,Shared.OptionsInfo里面(on_ui_settings)

需要拿出设置里面的数据,可以拿shared.opts.data.get的方法来实现

output_path = shared.opts.data.get("infzoom_outpath", "outputs")

这里简单介绍了,stable diffusion的插件功能的方法,一些深入的定制需要会在接下来的文章中介绍一些深入应用。


http://www.niftyadmin.cn/n/4939337.html

相关文章

Tomcat多实例部署及nginx+tomcat的负载均衡和动静分离

Tomcat多实例部署 安装 jdk、tomcat&#xff08;流程可看之前博客&#xff09; 配置 tomcat 环境变量 [rootlocalhost ~]# vim /etc/profile.d/tomcat.sh#tomcat1 export CATALINA_HOME1/usr/local/tomcat/tomcat1 export CATALINA_BASE1/usr/local/tomcat/tomcat1 export T…

Vue3封装一个左侧可拖拽折叠的侧边栏布局

功能 1、点击左侧侧边栏可折叠或打开 2、左侧侧边栏可拖拽 代码 <template><div class"fold-left-box"><div class"fold-left-box-left" :style"{ width: asideWidth px }" v-show"asideWidth > 0">left<…

ORCA优化器浅析——IMDRelation Storage type of a relation GP6与GP7对比

如上图所示IMDRelation作为Interface for relations in the metadata cache&#xff0c;其定义了Storage type of a relation表的存储类型&#xff0c;如下所示&#xff1a; enum Erelstoragetype {ErelstorageHeap,ErelstorageAppendOnlyCols,ErelstorageAppendOnlyRows,Erels…

vue element 多图片组合预览

定义组件&#xff1a;preview-image <template><div><div class"imgbox"><divclass"preview-img":class"boxClass"v-if"Imageslist 3 ||Imageslist 5 ||Imageslist 7 ||Imageslist 8 ||Imageslist > 9"&…

无涯教程-Perl - select函数

描述 此函数将输出的默认文件句柄设置为FILEHANDLE,如果未指定文件句柄,则设置由print和write等功能使用的文件句柄。如果未指定FILEHANDLE,则它将返回当前默认文件句柄的名称。 select(RBITS,WBITS,EBITS,TIMEOUT)使用指定的位调用系统功能select()。 select函数设置用于处理…

Linux服务器上配置HTTP和HTTPS代理

本文将向你分享如何在Linux服务器上配置HTTP和HTTPS代理的方法&#xff0c;解决可能遇到的问题&#xff0c;让你的爬虫项目顺利运行&#xff0c;畅爬互联网&#xff01; 配置HTTP代理的步骤 1. 了解HTTP代理的类型&#xff1a;常见的有正向代理和反向代理两种类型。根据实际需求…

七夕特辑:所以结婚到底有什么好处?

点击文末“阅读原文”即可参与节目互动 剪辑、音频 / 伊姐 运营 / SandLiu 卷圈 监制 / 姝琦 封面 / 姝琦Midjourney 产品统筹 / bobo 场地支持 / 声湃轩天津录音间 七夕来临&#xff0c;“记者下班”第一次和大家聊聊亲密关系。 无论是青梅竹马、相识二十年的阿福&#…

【计算机设计大赛】国赛一等奖项目分享——基于多端融合的化工安全生产监管可视化系统

文章目录 一、计算机设计大赛国赛一等奖二、项目背景三、项目简介四、系统架构五、系统功能结构六、项目特色&#xff08;1&#xff09;多端融合&#xff08;2&#xff09;数据可视化&#xff08;3&#xff09;计算机视觉&#xff08;目标检测&#xff09; 七、系统界面设计&am…