This commit is contained in:
2026-02-17 12:29:20 +08:00
parent f6c361dd13
commit 0ab6f52525
4 changed files with 132 additions and 29 deletions

View File

@@ -95,7 +95,7 @@ def create_highlighted_visualization(image: Image.Image, masks, output_path: str
# Save
Image.fromarray(result_np).save(output_path)
def analyze_demographics_with_qwen(image_path: str, model_name: str = 'qwen-vl-max') -> dict:
def analyze_demographics_with_qwen(image_path: str, model_name: str = 'qwen-vl-max', prompt_template: str = None) -> dict:
"""
调用 Qwen-VL 模型分析人物的年龄和性别
"""
@@ -104,19 +104,24 @@ def analyze_demographics_with_qwen(image_path: str, model_name: str = 'qwen-vl-m
abs_path = os.path.abspath(image_path)
file_url = f"file://{abs_path}"
# 默认 Prompt
default_prompt = """请仔细观察这张图片中的人物头部/面部特写:
1. 识别性别 (Gender):男性/女性
2. 预估年龄 (Age):请给出一个合理的年龄范围,例如 "25-30岁"
3. 简要描述:发型、发色、是否有眼镜等显著特征。
请以 JSON 格式返回,包含 'gender', 'age', 'description' 字段。
不要包含 Markdown 标记。"""
final_prompt = prompt_template if prompt_template else default_prompt
# 构造 Prompt
messages = [
{
"role": "user",
"content": [
{"image": file_url},
{"text": """请仔细观察这张图片中的人物头部/面部特写:
1. 识别性别 (Gender):男性/女性
2. 预估年龄 (Age):请给出一个合理的年龄范围,例如 "25-30岁"
3. 简要描述:发型、发色、是否有眼镜等显著特征。
请以 JSON 格式返回,包含 'gender', 'age', 'description' 字段。
不要包含 Markdown 标记。"""}
{"text": final_prompt}
]
}
]
@@ -144,7 +149,8 @@ def process_face_segmentation_and_analysis(
image: Image.Image,
prompt: str = "head",
output_base_dir: str = "static/results",
qwen_model: str = "qwen-vl-max"
qwen_model: str = "qwen-vl-max",
analysis_prompt: str = None
) -> dict:
"""
核心处理逻辑:
@@ -209,7 +215,7 @@ def process_face_segmentation_and_analysis(
cropped_img.save(save_path)
# 3. 识别
analysis = analyze_demographics_with_qwen(save_path, model_name=qwen_model)
analysis = analyze_demographics_with_qwen(save_path, model_name=qwen_model, prompt_template=analysis_prompt)
# 构造返回结果
# 注意URL 生成需要依赖外部的 request context这里只返回相对路径或文件名