I want to detect images taken with a camera with FaceAPI.

笹岡 愛 20 Reputation points
2023-02-17T01:09:15.7166667+00:00

I am implementing it in kotlin.

I want to detect the captured image taken by the camera with FaceAPI.
I'm using Detect With Stream, but I'm getting an "image format unsupported" error.

Please tell me how to use Detect With Stream.

//Convert the captured image to Bitmap.
var imageUri = intent.getParcelableExtra<Uri>("image")
val imageBitmap: Bitmap? = decodeUriToBitmap(imageUri)

//Convert imageBitmap to ByteArrayOutputStream.
val baos = ByteArrayOutputStream()
val compress = imageBitmap!!.compress(Bitmap.CompressFormat.PNG, 100, baos)

//Run Detect With Stream.
val faceId = FaceApi.detectWithStream("""{"""+baos+"""}""")    
	?:return@launch
	


URI Parameters
Endpoint:"endpoint"/detect

Request Header
Ocp-Apim-Subscription-Key:"SUBSCRIPTION_KEY"

Request Body
Media Types: "application/octet-stream"
image:{"baos"}  // baos:ByteArrayOutputStream
Azure Face
Azure Face
An Azure service that provides artificial intelligence algorithms that detect, recognize, and analyze human faces in images.
153 questions
0 comments No comments
{count} votes

Accepted answer
  1. romungi-MSFT 43,246 Reputation points Microsoft Employee
    2023-02-17T10:10:44.5766667+00:00

    笹岡 愛 As per the REST API reference, I think the image binary should be in base 64 format.

    I do not have experience in using kotlin or bitmap images, but I found a reference of this conversion in Java from this page on internet.

    val baos = ByteArrayOutputStream()
    val compress = imageBitmap!!.compress(Bitmap.CompressFormat.PNG, 100, baos)

    byte[] bytes=baos.toByteArray();
    sImage= Base64.encodeToString(bytes,Base64.DEFAULT);

    val faceId = FaceApi.detectWithStream(sImage)

    ?:return@launch

    The above syntax may be incorrect but I think if you use it base64 format the API should detect the stream data as valid.

    Ref: https://www.geeksforgeeks.org/how-to-encode-and-decode-image-in-base64-in-android/

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful