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/

相关推荐
love530love10 小时前
【笔记】重建 Stable Diffusion WebUI 虚拟环境实录
人工智能·windows·笔记·python·stable diffusion·aigc·虚拟环境
love530love13 小时前
【笔记】解决 Stable Diffusion WebUI 启动 “找不到llama_cpp模块”
运维·windows·笔记·python·stable diffusion·github·llama
aosky1 天前
如何部署本地 stable diffusion 本地使用大模型Z Image Turbo
stable diffusion
薛定e的猫咪1 天前
【论文精读】ICLR 2023 --- 作为离线强化学习强表达能力策略类的扩散策略
人工智能·深度学习·机器学习·stable diffusion
余蓝2 天前
本地部署!文生图LCM超简单教程
图像处理·人工智能·深度学习·ai作画·stable diffusion·dall·e 2
8Qi817 天前
Stable Diffusion详解
人工智能·深度学习·stable diffusion·图像生成
起个名字总是说已存在19 天前
stable-diffusion安装EasyPhoto启动报错解决
stable diffusion·ai绘画
微学AI20 天前
内网穿透的应用-摆脱局域网!Stable Diffusion3.5 结合cpolar使用更方便
docker·stable diffusion·内网穿透
陈苏同学20 天前
Diffusion 到 Flow Matching ( 从 DDPM 到 Stable Diffusion ) 丝滑入门
stable diffusion
CaracalTiger20 天前
本地部署 Stable Diffusion3.5!cpolar让远程访问很简单!
java·linux·运维·开发语言·python·微信·stable diffusion