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/

相关推荐
全息数据2 天前
DDPM代码讲解【详细!!!】
深度学习·stable diffusion·多模态·ddpm
老鱼说AI11 天前
当自回归模型遇上扩散模型:下一代序列预测模型详解与Pytorch实现
人工智能·pytorch·深度学习·神经网络·语言模型·自然语言处理·stable diffusion
我希望的一路生花17 天前
Nik Collection 6.2全新版Nik降噪锐化调色PS/LR插件
人工智能·计算机视觉·设计模式·stable diffusion·aigc
GetcharZp17 天前
玩转AI绘画,你只差一个节点式“魔法”工具——ComfyUI 保姆级入门指南
人工智能·stable diffusion
Seeklike19 天前
diffuxers学习--AutoPipeline
人工智能·python·stable diffusion·diffusers
游戏AI研究所19 天前
ComfyUI 里的 Prompt 插值器(prompt interpolation / text encoder 插值方式)的含义和作用!
人工智能·游戏·机器学习·stable diffusion·prompt·aigc
迈火22 天前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
Seeklike23 天前
diffusers学习--stable diffusion的管线解析
人工智能·stable diffusion·diffusers
马甲是掉不了一点的<.<23 天前
Stable Diffusion 环境配置详细指南
stable diffusion·环境配置
软件测试-阿涛23 天前
【AI绘画】Stable Diffusion webUI 常用功能使用技巧
人工智能·深度学习·计算机视觉·ai作画·stable diffusion