Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created February 22, 2022 07:55
Show Gist options
  • Save shubham0204/a41828ad9c0f6b57af16b3d895e1f225 to your computer and use it in GitHub Desktop.
Save shubham0204/a41828ad9c0f6b57af16b3d895e1f225 to your computer and use it in GitHub Desktop.
// ML Model to detect the age of a person, hosted on Heroku
class AgeDetectionModel {
private val herokuModelPredictURL = "https://age-detection-tf-app.herokuapp.com/predict"
private val mediaType = "application/json".toMediaType()
private val okHttpClient = OkHttpClient()
interface PredictionCallback {
fun onResult( age : Int )
fun onError( error : String )
}
fun pass( inputImage : Bitmap , predictionCallback: PredictionCallback ) {
// Make the POST request
val requestBody = RequestBody.create( mediaType , makeRequestBody( inputImage ) )
val request = Request.Builder()
.url( herokuModelPredictURL )
.post( requestBody )
.build()
// Execute the request asynchronously
okHttpClient.newCall( request ).enqueue( object : Callback {
override fun onFailure(call: Call, e: IOException) {
predictionCallback.onError( e.message!! )
}
override fun onResponse(call: Call, response: Response) {
val result = JSONObject( response.body!!.string() )
val age = result.getJSONArray( "prob" ).get( 0 ) as Double
predictionCallback.onResult( age.roundToInt() )
}
})
}
private fun makeRequestBody( image : Bitmap ) : String {
val imageEncoding = bitmapToBase64( image )
// Return the request body's JSON
return "{ \"image\" : \"${imageEncoding}\" }"
}
// Transform Bitmap into base64 String
private fun bitmapToBase64( image : Bitmap ) : String {
val imageByteArray = ByteArrayOutputStream().run {
image.compress( Bitmap.CompressFormat.PNG , 100 , this )
toByteArray()
}
return Base64.encodeToString( imageByteArray , Base64.NO_WRAP )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment