Files
change_hair/project/hair_service_sd/gpt4v_caption.py
T
xsl 443cfa298f 初始化:换发型/换发色/训练发型服务
包含:
- 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密钥已脱敏为环境变量,原文件备份在本地
2026-07-07 13:53:52 +08:00

82 lines
2.4 KiB
Python

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 encode_image_v2(image_in):
img = image_in
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_in):
# Getting the base64 string
base64_image = encode_image_v2(image_in)
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 ""
return ""
if __name__ == '__main__':
prompt = caption_image('/home/chinatszrn/Downloads/abc/train_data/style1/07ebac82-4c0f-4dd2-84bd-bc34a059bd9b.png')
print(prompt)