stable diffusion图片转高清前后对比

https://platform.stability.ai/sandbox/upscaling

javascript 复制代码
const sdUpscaleOnAzure = async (req, res) => {
  let { list = [], type = '1', apiKey = '' } = req.body

  if (apiKey === 'xxxx') {
    if (Array.isArray(list) && list.length > 0) {
      let promiseListForDownload = list.map((item) => {
        return new Promise((resolve) => {
          let sdUrl = item
          let urlObj = new URL(sdUrl)
          const client = urlObj.protocol == 'https:' ? https : http
          client.get(sdUrl, async (httpRes) => {
            let fileName = Date.now()
            let endIndex =
              sdUrl.indexOf('?') > 0 ? sdUrl.indexOf('?') : sdUrl.length
            sdUrl = sdUrl.slice(0, endIndex)
            let urlCdnArr = sdUrl.split('/')
            let filePath = `/temp/ai/sdAzure/download/${fileName}-${
              urlCdnArr[urlCdnArr.length - 1]
            }`
            let stream = fs.createWriteStream(filePath)
            httpRes.pipe(stream)
            stream.on('finish', async () => {
              resolve(filePath)
            })
          })
        })
      })

      let localFileList = await Promise.all(promiseListForDownload)

      let promiseListForUpscale = localFileList.map((item) => {
        return new Promise((resolve) => {
          // 文件上传
          let now = Date.now()

          let localFile = item

          const data = new FormData()
          data.append('image', fs.readFileSync(localFile))
          let config = {
            headers: {
              ...data.getHeaders(),
              Accept: 'application/json',
              Authorization:
                'Bearer sk-xxx',
            },
            method: 'post',
          }
          if (type === '1') {
            config.url =
              'https://api.stability.ai/v1/generation/esrgan-v1-x2plus/image-to-image/upscale'
          } else if (type === '2') {
            config.url =
              'https://api.stability.ai/v1/generation/stable-diffusion-x4-latent-upscaler/image-to-image/upscale' //贵
          }

          config.data = data

          axios({
            ...config,
          })
            .then(async (response) => {
              if (
                response.data &&
                Array.isArray(response.data.artifacts) &&
                response.data.artifacts.length > 0
              ) {
                let localFileArr = localFile.split('/')
                let key = `/temp/ai/sdAzure/big/${now}-${
                  localFileArr[localFileArr.length - 1]
                }`
                response.data.artifacts.forEach((image) => {
                  fs.writeFileSync(key, Buffer.from(image.base64, 'base64'))
                })
                resolve(key)
              } else {
                resolve()
              }
            })
            .catch((err) => {
              console.log(err)
              resolve()
            })
        })
      })

      let bigPictureList = await Promise.all(promiseListForUpscale)

      let promiseListForUpload = bigPictureList.map((item) => {
        return new Promise((resolve) => {
          // 文件上传
          let now = Date.now()
          // let localFile =
          //   '/temp/ai/sd/1694572598201-844b497d-8864-4a89-bf67-befddf68a69a-0.png'
          let localFile = item
          let localFileArr = localFile.split('/')
          let key = `ai/sd/${now}-${localFileArr[localFileArr.length - 1]}`
          let historyKey = key
          putExtra.checkCrc = false
          formUploader.putFile(
            uploadToken,
            key,
            localFile,
            putExtra,
            async function (respErr, respBody, respInfo) {
              if (respErr) {
                console.log('上传SD图片失败【1】', respErr)
              }
              if (
                respInfo.statusCode == 200 ||
                (respInfo.statusCode === 614 &&
                  respBody.error === 'file exists')
              ) {
                let key = ''
                if (respBody.data) {
                  key = respBody.data.key
                }
                if (!key) {
                  key = historyKey
                }
                resolve(key)
              } else {
                console.log(respInfo.statusCode)
                console.log('上传SD图片失败【2】', respBody)
              }
            }
          )
        })
      })

      let cdnPictureList = await Promise.all(promiseListForUpload)

      res.send({
        code: 200,
        data: {
          list,
          localFileList,
          bigPictureList,
          cdnPictureList,
        },
        message: '成功',
      })
    } else {
      res.send({
        code: 400,
        message: '失败:参数list',
      })
    }
  } else {
    res.send({
      code: 400,
      message: '失败:参数apiKey',
    })
  }
}

参考链接:

https://chat.xutongbao.top/

相关推荐
web150850966417 小时前
Stable Diffusion WebUI Two Shot 项目常见问题解决方案
stable diffusion
Jeremy_lf16 小时前
【生成模型之三】ControlNet & Latent Diffusion Models论文详解
人工智能·深度学习·stable diffusion·aigc·扩散模型
minos.cpp2 天前
Mac上Stable Diffusion的环境搭建(还算比较简单)
macos·ai作画·stable diffusion·aigc
不当菜鸡的程序媛4 天前
Stable Diffusion模型微调LORA及其变种介绍
stable diffusion
AIGC安琪4 天前
只需3步,使用Stable Diffusion无限生成AI数字人视频
人工智能·stable diffusion·数字人·sd·sd教程
AI绘画咪酱5 天前
【AIGC】ComfyUI 入门教程(4):ComfyUI 管理器|AI 生成图片
人工智能·stable diffusion·aigc
我算是程序猿5 天前
Stable Diffusion4.9.0(Ai绘画)安装教程
人工智能·ai作画·stable diffusion·aigc
AI绘画君5 天前
Stable Diffusion【二次元模型】:最受欢迎的通用二次元大模型AWPainting
linux·服务器·人工智能·ai作画·stable diffusion
不秃头de程序猿5 天前
AI绘画Stable Diffusion ,3种方法精确控制人物姿势,总有一种适合你!
大数据·人工智能·ai作画·stable diffusion·aigc·sd
AI码上来5 天前
【保姆级教程】Linux上部署Stable Diffusion WebUI和LoRA训练,拥有你的专属图片生成模型
linux·人工智能·python·stable diffusion