Android调用Face API获取人脸属性的版本及参数配置技术咨询
Hey there! Let's break down your questions one by one to get you sorted out with the face attributes you need.
Question 1: FACE API Updates & Cross-Platform Attribute Differences
Is there an updated version of FACE API 1.0?
Absolutely! FACE API 1.0 is an older release—there have been several major updates since then, with the latest stable version being v3.1. Upgrading to the newest build gives you access to more attributes, better performance, bug fixes, and improved model accuracy. I highly recommend making the switch if you can, as older versions might not support some of the attributes you're targeting (like glasses on Android).
Why can't Android see attributes like glasses that iOS has?
This usually comes down to two key issues:
- SDK version mismatch: Your iOS app might be using a newer Face SDK that supports the
glassesattribute, while your Android app is stuck on an older version (like 1.0) that doesn't include it. - Missing attribute parameters: When calling the Face API, you need to explicitly list which attributes you want returned via the
returnFaceAttributesparameter. It’s likely your iOS request includesglassesin this list, but your Android request doesn’t. Older SDKs also often have different default attributes, so never rely on defaults—always specify exactly what you need.
Question 2: Formatting Parameters to Get Age, Gender, and Facial Hair
Retrieving these attributes is straightforward once you know how to structure your request. Here’s how to do it for both REST API calls and SDK implementations:
REST API Request Example
When sending a POST request to the Face API detect endpoint, include the returnFaceAttributes field in your JSON body with the list of attributes you want:
curl -X POST "https://<your-region>.api.cognitive.microsoft.com/face/v3.1/detect" \ -H "Ocp-Apim-Subscription-Key: <your-subscription-key>" \ -H "Content-Type: application/json" \ -d '{ "url": "https://example.com/your-face-image.jpg", "returnFaceAttributes": ["age", "gender", "facialHair", "glasses"] }'
Android SDK Example (Using Microsoft's Official SDK)
If you’re using the Android SDK, explicitly define the attributes you want to fetch when making the detect call:
// Initialize the Face Client FaceClient faceClient = new FaceClientBuilder() .withSubscriptionKey("<your-subscription-key>") .withEndpoint("<your-api-endpoint>") .buildClient(); // Define the attributes you need List<FaceAttributeType> desiredAttributes = Arrays.asList( FaceAttributeType.AGE, FaceAttributeType.GENDER, FaceAttributeType.FACIAL_HAIR, FaceAttributeType.GLASSES ); // Detect faces with attributes List<Face> detectedFaces = faceClient.face.detectWithUrl( "https://example.com/your-face-image.jpg", true, // Return face ID false, // Return face landmarks desiredAttributes ); // Access the retrieved attributes for (Face face : detectedFaces) { FaceAttributes attributes = face.faceAttributes(); Log.d("FaceAttributes", "Age: " + attributes.age()); Log.d("FaceAttributes", "Gender: " + attributes.gender()); Log.d("FaceAttributes", "Facial Hair: " + attributes.facialHair()); Log.d("FaceAttributes", "Glasses: " + attributes.glasses()); }
Key Notes:
- Always specify exactly which attributes you need in
returnFaceAttributes—don’t assume defaults will include everything. - Make sure your Android SDK is updated to match the version you’re using on iOS to unlock all available attributes.
- Double-check your API endpoint and subscription key are correct for the version you’re using (v3.1 endpoints follow the format
https://<region>.api.cognitive.microsoft.com/face/v3.1/detect).
内容的提问来源于stack exchange,提问作者czane

