import requests
def validate_and_process_image(image_file, api_key):
try:
# 1. 调用人脸分析 API
analysis_result = analyze_face(image_file, api_key)
# 2. 验证人脸数量
if analysis_result['result']['face_num'] != 1:
raise ValueError('请使用包含且仅包含一个人脸的照片')
# 3. 验证人脸占比
face_info = analysis_result['result']['face_info'][0]
face_proportion = face_info['face_proportion']
if face_proportion < 0.2 or face_proportion > 0.8:
raise ValueError('人脸占比应在20%-80%之间')
# 4. 验证人脸角度
face_angle = face_info['face_angle']
yaw, pitch, roll = face_angle['yaw'], face_angle['pitch'], face_angle['roll']
if abs(yaw) > 30 or abs(pitch) > 30 or abs(roll) > 30:
raise ValueError('请使用正面照片,人脸角度不应过大')
# 5. 调用发型编辑 API
hairstyle_result = process_hairstyle(image_file, 'BuzzCut', api_key)
return hairstyle_result
except Exception as error:
print(f'图片验证失败: {error}')
return None