-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
FrameAnalyser.kt
240 lines (209 loc) · 10.6 KB
/
FrameAnalyser.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2023 Shubham Panchal
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ml.quaterion.facenetdetection
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.Face
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
import com.ml.quaterion.facenetdetection.model.FaceNetModel
import com.ml.quaterion.facenetdetection.model.MaskDetectionModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.math.pow
import kotlin.math.sqrt
// Analyser class to process frames and produce detections.
class FrameAnalyser( context: Context ,
private var boundingBoxOverlay: BoundingBoxOverlay ,
private var model: FaceNetModel
) : ImageAnalysis.Analyzer {
private val realTimeOpts = FaceDetectorOptions.Builder()
.setPerformanceMode( FaceDetectorOptions.PERFORMANCE_MODE_FAST )
.build()
private val detector = FaceDetection.getClient(realTimeOpts)
private val nameScoreHashmap = HashMap<String,ArrayList<Float>>()
private var subject = FloatArray( model.embeddingDim )
// Used to determine whether the incoming frame should be dropped or processed.
private var isProcessing = false
// Store the face embeddings in a ( String , FloatArray ) ArrayList.
// Where String -> name of the person and FloatArray -> Embedding of the face.
var faceList = ArrayList<Pair<String,FloatArray>>()
private val maskDetectionModel = MaskDetectionModel( context )
private var t1 : Long = 0L
// <-------------- User controls --------------------------->
// Use any one of the two metrics, "cosine" or "l2"
private val metricToBeUsed = "l2"
// Use this variable to enable/disable mask detection.
private val isMaskDetectionOn = true
// <-------------------------------------------------------->
init {
boundingBoxOverlay.drawMaskLabel = isMaskDetectionOn
}
@SuppressLint("UnsafeOptInUsageError")
override fun analyze(image: ImageProxy) {
// If the previous frame is still being processed, then skip this frame
if ( isProcessing || faceList.size == 0 ) {
image.close()
return
}
else {
isProcessing = true
// Rotated bitmap for the FaceNet model
val cameraXImage = image.image!!
var frameBitmap = Bitmap.createBitmap( cameraXImage.width , cameraXImage.height , Bitmap.Config.ARGB_8888 )
frameBitmap.copyPixelsFromBuffer( image.planes[0].buffer )
frameBitmap = BitmapUtils.rotateBitmap( frameBitmap , image.imageInfo.rotationDegrees.toFloat() )
//val frameBitmap = BitmapUtils.imageToBitmap( image.image!! , image.imageInfo.rotationDegrees )
// Configure frameHeight and frameWidth for output2overlay transformation matrix.
if ( !boundingBoxOverlay.areDimsInit ) {
boundingBoxOverlay.frameHeight = frameBitmap.height
boundingBoxOverlay.frameWidth = frameBitmap.width
}
val inputImage = InputImage.fromBitmap( frameBitmap , 0 )
detector.process(inputImage)
.addOnSuccessListener { faces ->
CoroutineScope( Dispatchers.Default ).launch {
runModel( faces , frameBitmap )
}
}
.addOnCompleteListener {
image.close()
}
}
}
private suspend fun runModel( faces : List<Face> , cameraFrameBitmap : Bitmap ){
withContext( Dispatchers.Default ) {
t1 = System.currentTimeMillis()
val predictions = ArrayList<Prediction>()
for (face in faces) {
try {
// Crop the frame using face.boundingBox.
// Convert the cropped Bitmap to a ByteBuffer.
// Finally, feed the ByteBuffer to the FaceNet model.
val croppedBitmap = BitmapUtils.cropRectFromBitmap( cameraFrameBitmap , face.boundingBox )
subject = model.getFaceEmbedding( croppedBitmap )
// Perform face mask detection on the cropped frame Bitmap.
var maskLabel = ""
if ( isMaskDetectionOn ) {
maskLabel = maskDetectionModel.detectMask( croppedBitmap )
}
// Continue with the recognition if the user is not wearing a face mask
if (maskLabel == maskDetectionModel.NO_MASK) {
// Perform clustering ( grouping )
// Store the clusters in a HashMap. Here, the key would represent the 'name'
// of that cluster and ArrayList<Float> would represent the collection of all
// L2 norms/ cosine distances.
for ( i in 0 until faceList.size ) {
// If this cluster ( i.e an ArrayList with a specific key ) does not exist,
// initialize a new one.
if ( nameScoreHashmap[ faceList[ i ].first ] == null ) {
// Compute the L2 norm and then append it to the ArrayList.
val p = ArrayList<Float>()
if ( metricToBeUsed == "cosine" ) {
p.add( cosineSimilarity( subject , faceList[ i ].second ) )
}
else {
p.add( L2Norm( subject , faceList[ i ].second ) )
}
nameScoreHashmap[ faceList[ i ].first ] = p
}
// If this cluster exists, append the L2 norm/cosine score to it.
else {
if ( metricToBeUsed == "cosine" ) {
nameScoreHashmap[ faceList[ i ].first ]?.add( cosineSimilarity( subject , faceList[ i ].second ) )
}
else {
nameScoreHashmap[ faceList[ i ].first ]?.add( L2Norm( subject , faceList[ i ].second ) )
}
}
}
// Compute the average of all scores norms for each cluster.
val avgScores = nameScoreHashmap.values.map{ scores -> scores.toFloatArray().average() }
Logger.log( "Average score for each user : $nameScoreHashmap" )
val names = nameScoreHashmap.keys.toTypedArray()
nameScoreHashmap.clear()
// Calculate the minimum L2 distance from the stored average L2 norms.
val bestScoreUserName: String = if ( metricToBeUsed == "cosine" ) {
// In case of cosine similarity, choose the highest value.
if ( avgScores.maxOrNull()!! > model.model.cosineThreshold ) {
names[ avgScores.indexOf( avgScores.maxOrNull()!! ) ]
}
else {
"Unknown"
}
} else {
// In case of L2 norm, choose the lowest value.
if ( avgScores.minOrNull()!! > model.model.l2Threshold ) {
"Unknown"
}
else {
names[ avgScores.indexOf( avgScores.minOrNull()!! ) ]
}
}
Logger.log( "Person identified as $bestScoreUserName" )
predictions.add(
Prediction(
face.boundingBox,
bestScoreUserName ,
maskLabel
)
)
}
else {
// Inform the user to remove the mask
predictions.add(
Prediction(
face.boundingBox,
"Please remove the mask" ,
maskLabel
)
)
}
}
catch ( e : Exception ) {
// If any exception occurs with this box and continue with the next boxes.
Log.e( "Model" , "Exception in FrameAnalyser : ${e.message}" )
continue
}
Log.e( "Performance" , "Inference time -> ${System.currentTimeMillis() - t1}")
}
withContext( Dispatchers.Main ) {
// Clear the BoundingBoxOverlay and set the new results ( boxes ) to be displayed.
boundingBoxOverlay.faceBoundingBoxes = predictions
boundingBoxOverlay.invalidate()
isProcessing = false
}
}
}
// Compute the L2 norm of ( x2 - x1 )
private fun L2Norm( x1 : FloatArray, x2 : FloatArray ) : Float {
return sqrt( x1.mapIndexed{ i , xi -> (xi - x2[ i ]).pow( 2 ) }.sum() )
}
// Compute the cosine of the angle between x1 and x2.
private fun cosineSimilarity( x1 : FloatArray , x2 : FloatArray ) : Float {
val mag1 = sqrt( x1.map { it * it }.sum() )
val mag2 = sqrt( x2.map { it * it }.sum() )
val dot = x1.mapIndexed{ i , xi -> xi * x2[ i ] }.sum()
return dot / (mag1 * mag2)
}
}