stable diffusion图片转高清前后对比

stable diffusion图片转高清前后对比_第1张图片

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

stable diffusion图片转高清前后对比_第2张图片

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',
    })
  }
}

stable diffusion图片转高清前后对比_第3张图片

 

 参考链接:

https://chat.xutongbao.top/

你可能感兴趣的:(web前端,stable,diffusion)