console.log(faceapi);
const calculateSanTing = (positions) => {
  // 获取特定索引的特征点位置
  const leftEye = positions[36]; // 左眼角
  const rightEye = positions[42]; // 右眼角
  const noseBase = positions[33]; // 鼻尖
  const mouth = positions[48]; // 嘴巴中心

  // 计算三庭的比例
  const foreheadHeight = noseBase.x - leftEye.x; // 水平距离
  const noseLength = rightEye.x - leftEye.x; // 水平距离
  const chinHeight = mouth.x - noseBase.x; // 水平距离

  const foreheadHeightRounded = parseFloat(foreheadHeight.toFixed(2));
  const noseLengthRounded = parseFloat(noseLength.toFixed(2));
  const chinHeightRounded = parseFloat(chinHeight.toFixed(2));

  return {
    foreheadHeight: foreheadHeightRounded,
    noseLength: noseLengthRounded,
    chinHeight: -chinHeightRounded,
  };
};


const calculateWuYan = (positions) => {
  // 获取特定索引的特征点位置
  const leftEyebrow = positions[17]; // 左眉毛上角
  const rightEyebrow = positions[22]; // 右眉毛上角

  // 计算五眼的比例
  const eyeSpan = Math.abs(leftEyebrow.x - rightEyebrow.x);
  const fiveEyeSpan = eyeSpan * 5;

  const fiveEyeSpanRounded = parseFloat(fiveEyeSpan.toFixed(2));

  return fiveEyeSpanRounded;
};

const calculateFaceShape = (positions) => {
    // 选择关键特征点
    const jawOutline = positions.slice(0, 17); // 下巴轮廓
    const cheekLeft = positions[29]; // 左颧骨
    const cheekRight = positions[30]; // 右颧骨
    const foreheadLeft = positions[21]; // 左额头
    const foreheadRight = positions[22]; // 右额头
  
    // 计算宽度
    const cheekWidth = Math.sqrt((cheekRight.x - cheekLeft.x) ** 2 + (cheekRight.y - cheekLeft.y) ** 2);
    const foreheadWidth = Math.sqrt((foreheadRight.x - foreheadLeft.x) ** 2 + (foreheadRight.y - foreheadLeft.y) ** 2);
    const jawWidth = Math.sqrt((jawOutline[8].x - jawOutline[6].x) ** 2 + (jawOutline[8].y - jawOutline[6].y) ** 2);
  
    // 计算长度
    const faceLength = Math.sqrt((jawOutline[8].x - jawOutline[0].x) ** 2 + (jawOutline[8].y - jawOutline[0].y) ** 2);
  
    // 计算比例
    const cheekForeheadRatio = cheekWidth / foreheadWidth;
    const jawForeheadRatio = jawWidth / foreheadWidth;
    const faceAspectRatio = faceLength / foreheadWidth;
  
    // 判断脸型
    if (faceAspectRatio > 1.5) {
      // 长脸
      if (cheekForeheadRatio > 0.8 && cheekForeheadRatio < 1.2 && jawForeheadRatio < 0.8) {
        return '瓜子脸';
      } else {
        return '长形脸';
      }
    } else if (faceAspectRatio < 1) {
      // 宽脸
      return '宽脸';
    } else {
      // 标准脸
      if (cheekForeheadRatio > 0.8 && cheekForeheadRatio < 1.2 && jawForeheadRatio > 0.8) {
        return '心形脸';
      } else if (cheekForeheadRatio < 0.8 && jawForeheadRatio < 0.8) {
        return '菱形脸';
      } else {
        return '标准脸';
      }
    }
  };

// 封装计算眉形的函数
const calculateBrowShape = (positions) => {
    // 选择关键特征点
    const leftEyebrowLeft = positions[17]; // 左眉毛上角
    const leftEyebrowRight = positions[22]; // 左眉毛下角
    const rightEyebrowLeft = positions[25]; // 右眉毛上角
    const rightEyebrowRight = positions[31]; // 右眉毛下角
  
    // 计算眉毛宽度
    const leftBrowWidth = Math.abs(leftEyebrowLeft.x - leftEyebrowRight.x);
    const rightBrowWidth = Math.abs(rightEyebrowLeft.x - rightEyebrowRight.x);
    const browWidth = Math.max(leftBrowWidth, rightBrowWidth);
  
    // 计算眉毛长度
    const leftBrowLength = Math.sqrt((leftEyebrowLeft.x - leftEyebrowRight.x) ** 2 + (leftEyebrowLeft.y - leftEyebrowRight.y) ** 2);
    const rightBrowLength = Math.sqrt((rightEyebrowLeft.x - rightEyebrowRight.x) ** 2 + (rightEyebrowLeft.y - rightEyebrowRight.y) ** 2);
    const browLength = Math.max(leftBrowLength, rightBrowLength);
  
    // 判断眉形
    if (browWidth > 0.5 * browLength) {
      return '浓眉';
    } else if (browWidth < 0.3 * browLength) {
      return '细眉';
    } else {
      return '标准眉';
    }
  
    // 其他眉形判断条件可以添加在这里
    // ...
  };

  // 封装计算唇形的函数
const calculateLipShape = (positions) => {
    // 选择关键特征点
    const upperLipTop = positions[61]; // 上嘴唇顶部
    const upperLipBottom = positions[62]; // 上嘴唇底部
    const lowerLipTop = positions[63]; // 下嘴唇顶部
    const lowerLipBottom = positions[64]; // 下嘴唇底部
  
    // 判断唇形
    if (upperLipBottom.y - upperLipTop.y > 10 && lowerLipBottom.y - lowerLipTop.y > 10) {
      return '厚唇';
    } else if (upperLipBottom.y - upperLipTop.y < 5 && lowerLipBottom.y - lowerLipTop.y < 5) {
      return '薄唇';
    } else if (upperLipBottom.y - upperLipTop.y > 10 && lowerLipBottom.y - lowerLipTop.y < 5) {
      return '微笑唇';
    } else if (upperLipBottom.y - upperLipTop.y < 5 && lowerLipBottom.y - lowerLipTop.y > 10) {
      return '嘟嘟唇';
    } else {
      return '标准唇型';
    }
  };
  // 封装计算眼睛形状的函数
const calculateEyeShape = (positions) => {
    // 选择关键特征点
    const leftEyeInner = positions[33]; // 左眼内侧
    const leftEyeOuter = positions[39]; // 左眼外侧
    const rightEyeInner = positions[45]; // 右眼内侧
    const rightEyeOuter = positions[51]; // 右眼外侧
  
    // 计算眼睛长度
    const leftEyeLength = Math.sqrt((leftEyeInner.x - leftEyeOuter.x) ** 2 + (leftEyeInner.y - leftEyeOuter.y) ** 2);
    const rightEyeLength = Math.sqrt((rightEyeInner.x - rightEyeOuter.x) ** 2 + (rightEyeInner.y - rightEyeOuter.y) ** 2);
    const eyeLength = Math.max(leftEyeLength, rightEyeLength);
  
    // 判断眼睛形状
    if (eyeLength > 10 && leftEyeLength > rightEyeLength) {
      return '杏仁眼';
    } else if (eyeLength > 10 && leftEyeLength < rightEyeLength) {
      return '圆形眼';
    } else if (leftEyeInner.y > leftEyeOuter.y) {
      return '单眼皮';
    } else {
      return '双眼皮';
    }
  };
  
  // 封装计算鼻形的函数
  const calculateNoseShape = (positions) => {
    // 选择关键特征点
    const noseBase = positions[33]; // 鼻尖
    const noseTip = positions[34]; // 鼻尖
    const noseSide = positions[35]; // 鼻翼
  
    // 判断鼻形
    if (noseBase.y > noseTip.y && noseSide.x > noseTip.x) {
      return '鹰钩鼻';
    } else if (noseBase.y > noseTip.y && noseSide.x < noseTip.x) {
      return '蒜头鼻';
    } else if (noseBase.y < noseTip.y) {
      return '直鼻';
    } else {
      return '塌鼻';
    }
  };
  
  // 封装计算颧骨的函数
  const calculateCheekBones = (positions) => {
    // 选择关键特征点
    const cheekLeft = positions[29]; // 左颧骨
    const cheekRight = positions[30]; // 右颧骨
  
    // 判断颧骨
    if (cheekLeft.y > cheekRight.y) {
      return '颧骨高';
    } else {
      return '颧骨平';
    }
  };
  
  // 封装计算下巴的函数
  const calculateChin = (positions) => {
    // 选择关键特征点
    const chin = positions[60]; // 下巴中心
  
    // 判断下巴
    if (chin.y < chin.x) {
      return '长下巴';
    } else if (chin.y > chin.x) {
      return '短下巴';
    } else {
      return '尖下巴';
    }
  };
  
  // 封装计算额头的函数
  const calculateForehead = (positions) => {
    // 选择关键特征点
    const foreheadLeft = positions[21]; // 左额头
    const foreheadRight = positions[22]; // 右额头
  
    // 判断额头
    if (foreheadLeft.x > foreheadRight.x) {
      return '宽额头';
    } else {
      return '窄额头';
    }
  };
  

  const calculateCheeksShape = (positions) => {
    // 选择关键特征点
    const cheekLeft = positions[29]; // 左颧骨
    const cheekRight = positions[30]; // 右颧骨
  
    // 判断脸颊形状
    if (cheekLeft.y > cheekRight.y) {
      return '丰满脸颊';
    } else {
      return '瘦削脸颊';
    }
  };

  

  
const run = async () => {
  // 加载模型
  await Promise.all([
    faceapi.nets.ssdMobilenetv1.loadFromUri('./models'),
    faceapi.nets.faceLandmark68Net.loadFromUri('./models'),
    faceapi.nets.faceRecognitionNet.loadFromUri('./models'),
    faceapi.nets.ageGenderNet.loadFromUri('./models'),
  ]);

  const face1 = document.getElementById('face');

  let faceAIData = await faceapi
    .detectAllFaces(face1)
    .withFaceLandmarks()
    .withFaceDescriptors()
    .withAgeAndGender();

  console.log(faceAIData);

  // 获取第一个检测到的人脸的特征点
  const landmarks = faceAIData[0].landmarks;
  const positions = landmarks._positions;

  // 计算三庭和五眼的比例
  const sanTing = calculateSanTing(positions);//计算三庭
  const wuYan = calculateWuYan(positions);//计算五眼
  // 计算脸型
  const faceShape = calculateFaceShape(positions);
  // 计算眉形
  const browShape = calculateBrowShape(positions);
  // 计算唇形
  const lipShape = calculateLipShape(positions);
  // 计算眼睛形状
  const eyeShape = calculateEyeShape(positions);
  // 计算鼻形
  const noseShape = calculateNoseShape(positions);
  // 计算颧骨
  const cheekBones = calculateCheekBones(positions);
  // 计算下巴
  const chinShape = calculateChin(positions);
  // 计算额头
  const foreheadShape = calculateForehead(positions);
  // 计算脸颊
  const cheeksShape = calculateCheeksShape(positions);

 console.log(`三庭比例: ${sanTing.foreheadHeight},${sanTing.noseLength}, ${sanTing.chinHeight}`);
 console.log(`五眼比例: ${wuYan}`);
 console.log(`脸型: ${faceShape}`);
 console.log(`眉形: ${browShape}`);
 console.log(`唇形: ${lipShape}`);
 console.log(`眼睛形状: ${eyeShape}`);
 console.log(`鼻形: ${noseShape}`);
 console.log(`颧骨: ${cheekBones}`);
 console.log(`下巴: ${chinShape}`);
 console.log(`额头: ${foreheadShape}`);
 console.log(`脸颊: ${cheeksShape}`);
};

run();