diff --git a/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceClient.java b/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceClient.java index d675a9e..b5be7c7 100644 --- a/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceClient.java +++ b/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceClient.java @@ -226,6 +226,19 @@ public String toString() { */ Face[] detect(InputStream imageStream, boolean returnFaceId, boolean returnFaceLandmarks, FaceAttributeType[] returnFaceAttributes) throws ClientException, IOException; + /** + * Detects faces in an uploaded image. + * @param imageStream The image stream. + * @param returnFaceId If set to true [return face ID]. + * @param returnFaceLandmarks If set to true [return face landmarks] + * @param returnFaceAttributes Return face attributes. + * @param recognitionModel specific of recognition model to detect the face + * @return detected faces. + * @throws ClientException + * @throws IOException + */ + Face[] detect(InputStream imageStream, boolean returnFaceId, boolean returnFaceLandmarks, FaceAttributeType[] returnFaceAttributes, String recognitionModel) throws ClientException, IOException; + /** * Verifies whether the specified two faces belong to the same person. * @param faceId1 The face id 1. diff --git a/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceRestClient.java b/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceRestClient.java index aee5af7..7e6a7f9 100644 --- a/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceRestClient.java +++ b/ClientLibrary/lib/src/main/java/com/microsoft/projectoxford/face/FaceServiceRestClient.java @@ -184,6 +184,50 @@ public Face[] detect(InputStream imageStream, boolean returnFaceId, boolean retu return faces.toArray(new Face[faces.size()]); } + @Override + public Face[] detect(InputStream imageStream, boolean returnFaceId, boolean returnFaceLandmarks, FaceAttributeType[] returnFaceAttributes, String recognitionModel) throws ClientException, IOException { + Map params = new HashMap<>(); + + params.put("returnFaceId", returnFaceId); + params.put("returnFaceLandmarks", returnFaceLandmarks); + if (returnFaceAttributes != null && returnFaceAttributes.length > 0) { + StringBuilder faceAttributesStringBuilder = new StringBuilder(); + boolean firstAttribute = true; + for (FaceAttributeType faceAttributeType: returnFaceAttributes) + { + if (firstAttribute) { + firstAttribute = false; + } else { + faceAttributesStringBuilder.append(","); + } + + faceAttributesStringBuilder.append(faceAttributeType); + } + params.put("returnFaceAttributes", faceAttributesStringBuilder.toString()); + } + params.put("recognitionModel", (recognitionModel == null || !recognitionModel.matches("recognition_0[12]")) ? "recognition_01" : recognitionModel); + + String path = String.format("%s/%s", mServiceHost, DETECT_QUERY); + String uri = WebServiceRequest.getUrl(path, params); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + int bytesRead; + byte[] bytes = new byte[1024]; + while ((bytesRead = imageStream.read(bytes)) > 0) { + byteArrayOutputStream.write(bytes, 0, bytesRead); + } + byte[] data = byteArrayOutputStream.toByteArray(); + params.clear(); + params.put(DATA, data); + + String json = (String)mRestCall.request(uri, RequestMethod.POST, params, STREAM_DATA); + Type listType = new TypeToken>() { + }.getType(); + List faces = mGson.fromJson(json, listType); + + return faces.toArray(new Face[faces.size()]); + } + @Override public VerifyResult verify(UUID faceId1, UUID faceId2) throws ClientException, IOException { Map params = new HashMap<>();