Last active
February 24, 2019 12:43
-
-
Save apkelly/f40cf370d89e5ae0938c6258bdec1912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
private const val PROJECT = "devnibbles" | |
private const val LOCATION = "us-central1" | |
private const val MODEL = "ICN3704829353327390855" | |
private const val ACCESS_TOKEN = "<insert token here>" | |
} | |
fun classifyUsingRetrofit(faceId: Int, imageBytes: ByteArray) { | |
launch(errorHandler) { | |
// Show loading indicator while we wait for the request. | |
mResult.value = LoadingResource(null) | |
// Build the body of our request, essentially the image to be classified. | |
val body = CloudAutoMLModel( | |
Payload( | |
MlImage( | |
String( | |
Base64.encodeBase64(imageBytes) | |
) | |
) | |
) | |
) | |
// Define the authentication credentials and make the API request | |
val response = getRESTService().classify( | |
"Bearer $ACCESS_TOKEN", | |
PROJECT, LOCATION, MODEL, body | |
).await() | |
if (response.payload?.isNotEmpty() == true) { | |
// We have a prediction! | |
var predictedName: String? = null | |
response.payload.forEach { entry -> | |
// TODO: Check that score is within a valid threshold. | |
if (entry.displayName != null) { | |
predictedName = entry.displayName | |
} | |
} | |
if (predictedName != null) { | |
// We had an actual name returned | |
mResult.postValue(SuccessResource(Pair(faceId, predictedName!!))) | |
} else { | |
// No name was returned, this is an unknown face. | |
mResult.postValue(ErrorResource(null, Pair(-1, "Not recognised (001)"))) | |
} | |
} else { | |
// There were no payloads returned, possible error or unknown face. | |
mResult.postValue(ErrorResource(null, Pair(-1, "Not recognised (002)"))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment