async function validateAndProcessImage(imageFile) {
try {
// 1. 调用人脸分析 API
const analysisResult = await analyzeFace(imageFile);
// 2. 验证人脸数量
if (analysisResult.face_num !== 1) {
throw new Error('请使用包含且仅包含一个人脸的照片');
}
// 3. 验证人脸占比
const faceInfo = analysisResult.face_info[0];
const faceProportion = faceInfo.face_proportion;
if (faceProportion < 0.2 || faceProportion > 0.8) {
throw new Error('人脸占比应在20%-80%之间');
}
// 4. 验证人脸角度
const { yaw, pitch, roll } = faceInfo.face_angle;
if (Math.abs(yaw) > 30 || Math.abs(pitch) > 30 || Math.abs(roll) > 30) {
throw new Error('请使用正面照片,人脸角度不应过大');
}
// 5. 调用发型编辑 API
const hairstyleResult = await processHairstyle(imageFile, 'BuzzCut');
return hairstyleResult;
} catch (error) {
console.error('图片验证失败:', error.message);
// 显示错误信息给用户
showErrorToUser(error.message);
}
}