• stable diffusion图片转高清前后对比


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

    1. const sdUpscaleOnAzure = async (req, res) => {
    2. let { list = [], type = '1', apiKey = '' } = req.body
    3. if (apiKey === 'xxxx') {
    4. if (Array.isArray(list) && list.length > 0) {
    5. let promiseListForDownload = list.map((item) => {
    6. return new Promise((resolve) => {
    7. let sdUrl = item
    8. let urlObj = new URL(sdUrl)
    9. const client = urlObj.protocol == 'https:' ? https : http
    10. client.get(sdUrl, async (httpRes) => {
    11. let fileName = Date.now()
    12. let endIndex =
    13. sdUrl.indexOf('?') > 0 ? sdUrl.indexOf('?') : sdUrl.length
    14. sdUrl = sdUrl.slice(0, endIndex)
    15. let urlCdnArr = sdUrl.split('/')
    16. let filePath = `/temp/ai/sdAzure/download/${fileName}-${
    17. urlCdnArr[urlCdnArr.length - 1]
    18. }`
    19. let stream = fs.createWriteStream(filePath)
    20. httpRes.pipe(stream)
    21. stream.on('finish', async () => {
    22. resolve(filePath)
    23. })
    24. })
    25. })
    26. })
    27. let localFileList = await Promise.all(promiseListForDownload)
    28. let promiseListForUpscale = localFileList.map((item) => {
    29. return new Promise((resolve) => {
    30. // 文件上传
    31. let now = Date.now()
    32. let localFile = item
    33. const data = new FormData()
    34. data.append('image', fs.readFileSync(localFile))
    35. let config = {
    36. headers: {
    37. ...data.getHeaders(),
    38. Accept: 'application/json',
    39. Authorization:
    40. 'Bearer sk-xxx',
    41. },
    42. method: 'post',
    43. }
    44. if (type === '1') {
    45. config.url =
    46. 'https://api.stability.ai/v1/generation/esrgan-v1-x2plus/image-to-image/upscale'
    47. } else if (type === '2') {
    48. config.url =
    49. 'https://api.stability.ai/v1/generation/stable-diffusion-x4-latent-upscaler/image-to-image/upscale' //贵
    50. }
    51. config.data = data
    52. axios({
    53. ...config,
    54. })
    55. .then(async (response) => {
    56. if (
    57. response.data &&
    58. Array.isArray(response.data.artifacts) &&
    59. response.data.artifacts.length > 0
    60. ) {
    61. let localFileArr = localFile.split('/')
    62. let key = `/temp/ai/sdAzure/big/${now}-${
    63. localFileArr[localFileArr.length - 1]
    64. }`
    65. response.data.artifacts.forEach((image) => {
    66. fs.writeFileSync(key, Buffer.from(image.base64, 'base64'))
    67. })
    68. resolve(key)
    69. } else {
    70. resolve()
    71. }
    72. })
    73. .catch((err) => {
    74. console.log(err)
    75. resolve()
    76. })
    77. })
    78. })
    79. let bigPictureList = await Promise.all(promiseListForUpscale)
    80. let promiseListForUpload = bigPictureList.map((item) => {
    81. return new Promise((resolve) => {
    82. // 文件上传
    83. let now = Date.now()
    84. // let localFile =
    85. // '/temp/ai/sd/1694572598201-844b497d-8864-4a89-bf67-befddf68a69a-0.png'
    86. let localFile = item
    87. let localFileArr = localFile.split('/')
    88. let key = `ai/sd/${now}-${localFileArr[localFileArr.length - 1]}`
    89. let historyKey = key
    90. putExtra.checkCrc = false
    91. formUploader.putFile(
    92. uploadToken,
    93. key,
    94. localFile,
    95. putExtra,
    96. async function (respErr, respBody, respInfo) {
    97. if (respErr) {
    98. console.log('上传SD图片失败【1】', respErr)
    99. }
    100. if (
    101. respInfo.statusCode == 200 ||
    102. (respInfo.statusCode === 614 &&
    103. respBody.error === 'file exists')
    104. ) {
    105. let key = ''
    106. if (respBody.data) {
    107. key = respBody.data.key
    108. }
    109. if (!key) {
    110. key = historyKey
    111. }
    112. resolve(key)
    113. } else {
    114. console.log(respInfo.statusCode)
    115. console.log('上传SD图片失败【2】', respBody)
    116. }
    117. }
    118. )
    119. })
    120. })
    121. let cdnPictureList = await Promise.all(promiseListForUpload)
    122. res.send({
    123. code: 200,
    124. data: {
    125. list,
    126. localFileList,
    127. bigPictureList,
    128. cdnPictureList,
    129. },
    130. message: '成功',
    131. })
    132. } else {
    133. res.send({
    134. code: 400,
    135. message: '失败:参数list',
    136. })
    137. }
    138. } else {
    139. res.send({
    140. code: 400,
    141. message: '失败:参数apiKey',
    142. })
    143. }
    144. }

     

     参考链接:

    https://chat.xutongbao.top/

  • 相关阅读:
    第十六章总结:反射和注解
    golang 基础-golang里面的读写锁实现与核心原理分析
    七、【Vue-Router】router-link标签的replace属性
    Java构件技术
    vue中的响应式数据和副作用函数
    网络-httpclient调用https服务端绕过证书的方法
    python小玩意——图片转素描
    华为数字化转型之道认知篇第一章数字化转型,华为的战略选择
    vue ant 隐藏列
    YoC的使用
  • 原文地址:https://blog.csdn.net/xutongbao/article/details/133992774