You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
import requests
|
|
import base64
|
|
from PIL import Image
|
|
|
|
url = 'http://192.168.1.21:7860/tagger/v1/interrogate'
|
|
image_path = r'D:\KeCheng\BaiHu\Backup\mote2.png'
|
|
|
|
# 反推模型
|
|
model = 'wd14-vit-v2-git' # 'wd14-convnext'
|
|
# 阀值
|
|
threshold = 0.35
|
|
|
|
# 确认照片为上传照片
|
|
image = Image.open(image_path)
|
|
# 将图片转换为Base64字符串
|
|
with open(image_path, 'rb') as file:
|
|
image_data = file.read()
|
|
base64_image = base64.b64encode(image_data).decode('utf-8')
|
|
|
|
image.close()
|
|
|
|
# 构建请求体的JSON数据
|
|
data = {
|
|
"image": base64_image,
|
|
"model": model,
|
|
"threshold": threshold
|
|
}
|
|
|
|
# 发送POST请求
|
|
response = requests.post(url, json=data)
|
|
|
|
# 检查响应状态码
|
|
if response.status_code == 200:
|
|
json_data = response.json()
|
|
# 处理返回的JSON数据
|
|
caption_dict = json_data['caption']
|
|
sorted_items = sorted(caption_dict.items(), key=lambda x: x[1], reverse=True)
|
|
# output = '\n'.join([f'{k}: {v}, {int(v * 100)}%' for k, v in sorted_items])
|
|
# output = ','.join([f'{k.replace("_"," ")}' for k, v in sorted_items])
|
|
|
|
output = ''
|
|
for k, v in sorted_items:
|
|
if v > threshold:
|
|
output = output + "," + k.replace("_", " ")
|
|
output = output[1:]
|
|
print(output)
|
|
else:
|
|
print('Error:', response.status_code)
|
|
print('Response body:', response.text)
|