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/

相关推荐
qq_398898936 小时前
【简单三步】Stable diffusion Webai本地部署无法加载模型并报openai/clip-vit-large-patch14错误的解决方法
stable diffusion
ai_xiaogui6 小时前
AIStarter用户与创作者模式详解:一键管理Stable Diffusion项目!
人工智能·stable diffusion·一键发布ai项目·熊哥aistarter教程·开发者必备aistarter
一禅(OneZen)1 天前
「Windows/Mac OS」AIGC图片生成视频 ,webui + stable-diffusion环境部署教程
windows·stable diffusion
ai_xiaogui3 天前
一键部署AI工具!用AIStarter快速安装ComfyUI与Stable Diffusion
人工智能·stable diffusion·部署ai工具·ai应用市场教程·sd快速部署·comfyui一键安装
sigmoidAndRELU4 天前
读Vista
笔记·stable diffusion·世界模型
修炼室19 天前
Stable Diffusion WebUI 本地部署完整教程
stable diffusion
NetX行者19 天前
Stable Diffusion:开启AI图像生成新纪元
人工智能·stable diffusion
这是一个懒人24 天前
SD和comfyui常用模型介绍和下载
stable diffusion·comfyui·模型下载
有点小帅得平哥哥25 天前
Stable Diffusion WebUI 本地部署指南(Windows 11 + RTX 4060 Ti)
stable diffusion
CoovallyAIHub1 个月前
突破异常数据瓶颈!AnomalyAny:一句话+一张图,零样本生成任意异常图像
计算机视觉·stable diffusion