Stable Diffusion WebUI使用python调用WD 1.4标签器(tagger),获取并处理

[复制链接]
查看688 | 回复0 | 2023-8-16 12:57:36 | 显示全部楼层 |阅读模式
Stable Diffusion为秋叶大佬的绘世2.2.4
webUI api后台:http://127.0.0.1:7860/docs
  数据获取结果,python代码在文章尾部

  1. 1girl: 0.9883618950843811, 98%
  2. solo: 0.9468605518341064, 94%
  3. horns: 0.9203381538391113, 92%
  4. braid: 0.7536494731903076, 75%
  5. brown_hair: 0.7361204624176025, 73%
  6. sensitive: 0.7181869745254517, 71%
  7. looking_at_viewer: 0.6558270454406738, 65%
  8. long_hair: 0.6555134654045105, 65%
  9. portrait: 0.5619801878929138, 56%
  10. hair_ornament: 0.5276427268981934, 52%
  11. lips: 0.5271897912025452, 52%
  12. realistic: 0.47530364990234375, 47%
  13. brown_eyes: 0.44382530450820923, 44%
  14. fur_trim: 0.44058263301849365, 44%
  15. red_hair: 0.4004508852958679, 40%
  16. upper_body: 0.39194822311401367, 39%
  17. mole: 0.35748565196990967, 35%
  18. general: 0.2813188433647156, 28%
  19. questionable: 0.004140794277191162, 0%
  20. explicit: 0.0005668997764587402, 0%
复制代码
使用/tagger/v1/interrogate,先使用get方法获取model模组有十多个,然后把json_data提交上去就可以了。记得把图片转码为base64。本文章仅用于测试,请仔细阅读api docs,model和threshold按照需求调整即可



  1. import requests
  2. import base64
  3. from collections import OrderedDict
  4. from PIL import Image
  5. url = 'http://127.0.0.1:7860/tagger/v1/interrogate'
  6. image_path = 'D:/code/image/6.jpg'
  7. model = 'wd14-convnext'
  8. threshold = 0.35
  9. #确认照片为上传照片
  10. image = Image.open(image_path)
  11. image.show()
  12. # 将图片转换为Base64字符串
  13. with open(image_path, 'rb') as file:
  14.     image_data = file.read()
  15.     base64_image = base64.b64encode(image_data).decode('utf-8')
  16. # 构建请求体的JSON数据
  17. data = {
  18.     "image": base64_image,
  19.     "model": model,
  20.     "threshold": threshold
  21. }
  22. # 发送POST请求
  23. response = requests.post(url, json=data)
  24. # 检查响应状态码
  25. if response.status_code == 200:
  26.     json_data = response.json()
  27.     # 处理返回的JSON数据
  28.     caption_dict = json_data['caption']
  29.     sorted_items = sorted(caption_dict.items(), key=lambda x: x[1], reverse=True)
  30.     output = '\n'.join([f'{k}: {v}, {int(v * 100)}%' for k, v in sorted_items])
  31.     print(output)
  32. else:
  33.     print('Error:', response.status_code)
  34.     print('Response body:', response.text)
复制代码
来源:https://blog.csdn.net/weixin_42375356/article/details/130904119
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则