The last function we need to create in this file is parse classification output.
So here create private function parse classification output.
It takes one parameter.
Output buffer.
It's of type tensor buffer.
It returns a list of detection result class.
Here start with val results equals to mutable.
List of detection result.
Also val output array equals to output buffer dot float array.
Now I want from you to focus with me because there is a very important note here.
We should pay attention to it.
We're going to process classification model outputs and convert them into detection results.
If you remember we stored the returning result of this parse classification output into the variable
results and pass it to the listener on results method.
And if you pay attention, the list of detection result is the returning type of the parse classification
output function.
Okay, now let's start to start the processing.
For I in output array dot indices, if I less than labels size what we need to do.
We need to start with val confidence equals to output array at index I.
Then if the confidence greater than 0.5 f we need to add it to the results.
Let me explain what we are doing here.
As you know we are iterating through each element in the output array.
Each element represents the confidence probability for a specific class.
Okay, it's very simple.
We are iterating over this array.
Bounds checking.
We are checking here if the labels dot size is is is accessible.
And we ensure that we don't access beyond the available labels preventing index out of bounds exception.
It's normal to now everything is normal.
The val confidence.
The output array at index I extract the confidence score of class at index I.
We need to get the confidence at index I only processes classifications with 50% confidence.
This filters out weak or unreliable predictions.
So if there is a detection with 40% like a hard disk.
We don't need it.
We get classifications only higher than 50%.
So this is the mean of it.
Okay.
It's very simple.
If you need 60% higher 70% detection confidence, you can increase this number.
For now.
Keep it 50%.
It's good.
Okay.
Now for classification models.
We don't have bounding boxes.
We'll create a dummy bounding box that covers the whole image.
So for that starting with results dot add.
And here set a detection result.
Set the labels at label index confidence confidence and rect at rect f.
Here let me draw this rect f.
The left bound is for example zero f the left, the top 0F1, f right, and one f, but okay.
This is a normalized coordinates of this results.
The label class name from labels at index I.
Confidence probability score from 0 to 1.0.
So probability zero less than probability or confidence and one point that bounding box dummy rectangle
covering the entire image okay so here dummy rectangle.
By the way if we run the application and if there is any errors, any logical or drawing errors, we
can modify those later on.
Okay.
Dummy rectangle covering entire image.
We can replace it later on with with smaller okay.
If the model detects hard disk with 0.8 confidence, it will throw it if the model detects like AirPods
with confidence 0.3.
It will not draw anything.
Okay.
It's very simple.
Now I want from you to focus with me.
The results don't add if the model detects if the model.
Let me show you.
If the model detects a hard disk with 0.8% confidence.
What we need to pass here.
What we need to add to the results.
We're going to add detection.
Result label is hard disk.
So if we go to the labels dot text here we get hard disk.
So the model will copy this name.
This label passed to the first parameter of the detection result.
Then the confidence is 0.8 and the bounding box would be a rect f.
For example 0.1.
Here we are drawing 0.0.
So let me add 0.1 like this.
You can draw it like this example.
It's very good okay.
So this is the model detection.
And the detection result object will be added to the results array.
Now let me return this results by specifying here the returning result.
Returning result is result list.
Okay.
We need to move it outside till here.
Let me show you guys.
Oops.
Let me remove it after this.
And here we go.
We have an extra one.
And this should be for the list okay.
So let me move it up and here we go okay.
This is our function pass classification output.