包含: - hair_service_sd: 主服务(换发型/换发色/生发,端口8801) - photo_service: LoRA调度+训练(端口32678) - hair_grow_service: 调试测试页(端口8888,含4个测试页) - 批量训练脚本(batch_train_hairstyles.py) - 发际线mask自动识别(hairline_mask.py,4种方案) - 手绘mask换发型(hair_swap_manual.py) - 文档:README.md + LARGE_FILES.md + docs/ 大文件(模型权重200G、训练数据123G)已排除,见 LARGE_FILES.md OSS/COS密钥已脱敏为环境变量,原文件备份在本地
69 lines
2.0 KiB
Python
Executable File
69 lines
2.0 KiB
Python
Executable File
import base64
|
|
import time
|
|
|
|
import requests
|
|
import cv2
|
|
import json
|
|
import re
|
|
import os
|
|
import tqdm
|
|
|
|
# OpenAI API Key
|
|
api_key = "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"
|
|
|
|
# Function to encode the image
|
|
def encode_image(image_path):
|
|
img = cv2.imread(image_path)
|
|
scale = 500.0 / min(img.shape[:2])
|
|
img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
|
|
scaled_path = '/tmp/scaled_image.jpg'
|
|
cv2.imwrite(scaled_path, img)
|
|
with open(scaled_path, "rb") as image_file:
|
|
return base64.b64encode(image_file.read()).decode('utf-8')
|
|
|
|
def caption_image(image_path):
|
|
# Getting the base64 string
|
|
base64_image = encode_image(image_path)
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {api_key}"
|
|
}
|
|
|
|
payload = {
|
|
"model": "gpt-4-vision-preview",
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": "As an AI image tagging expert, please provide precise tags for the hairstyle in the image, To enhance CLIP model's understanding of the content. Please provide a detailed description of the hairstyle in the image, including but not limited to the color, style, length, curliness, hairline, highlights, gradients, etc. Your tags should be accurate, non-duplicative, and within a 10-20 word count range. Tags should be comma-separated. No need to provide any safety statements or precautions."
|
|
},
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {
|
|
"url": f"data:image/jpeg;base64,{base64_image}",
|
|
"detail": "low"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"max_tokens": 300
|
|
}
|
|
|
|
try:
|
|
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
tmp_json = response.json()
|
|
return tmp_json['choices'][0]['message']['content']
|
|
except Exception as e:
|
|
print(e)
|
|
return ""
|
|
|
|
|
|
if __name__ == '__main__':
|
|
prompt = caption_image('/home/chinatszrn/Downloads/abc/train_data/style1/07ebac82-4c0f-4dd2-84bd-bc34a059bd9b.png')
|
|
print(prompt)
|
|
|