This file contains hidden or 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
private fun cosineSim(x1: FloatArray, x2: FloatArray): Float { | |
var dotProduct = 0.0f | |
var normA = 0.0f | |
var normB = 0.0f | |
for (i in x1.indices) { | |
dotProduct += x1[i] * x2[i] | |
normA += x1[i].pow(2) | |
normB += x2[i].pow(2) | |
} | |
return dotProduct / (sqrt(normA) * sqrt(normB)) |
This file contains hidden or 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
private fun l2Norm(x1: FloatArray, x2: FloatArray): Float { | |
var sum = 0.0f | |
val mag1 = sqrt(x1.map { xi -> xi.pow(2) }.sum()) | |
val mag2 = sqrt(x2.map { xi -> xi.pow(2) }.sum()) | |
for (i in x1.indices) { | |
sum += ((x1[i] / mag1) - (x2[i] / mag2)).pow(2) | |
} | |
return sqrt(sum) | |
} |
This file contains hidden or 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
val interpreterOptions = Interpreter.Options().apply { | |
setNumThreads(4) | |
} | |
val interpreter = Interpreter(FileUtil.loadMappedFile(context, "model.tflite"), interpreterOptions) | |
private fun runFaceNet(inputs: Any): Array<FloatArray> { | |
val faceNetModelOutputs: MutableMap<Int, Any> = HashMap() | |
faceNetModelOutputs[0] = Array(1) { FloatArray(embeddingDim) } | |
This file contains hidden or 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
private val imgSize = | |
// Set this image size according to our choice of DNN Model (160 for FaceNet, 112 for MobileFaceNet) | |
private val imageTensorProcessor = ImageProcessor.Builder() | |
.add(ResizeOp(imgSize, imgSize, ResizeOp.ResizeMethod.BILINEAR)) | |
.add(CastOp( DataType.FLOAT32 )) | |
.build() | |
private fun convertBitmapToBuffer(image: Bitmap): ByteBuffer { | |
return imageTensorProcessor.process(TensorImage.fromBitmap(image)).buffer |
This file contains hidden or 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
implementation 'org.tensorflow:tensorflow-lite:2.4.0' | |
implementation 'org.tensorflow:tensorflow-lite-gpu:2.4.0' | |
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0' |
This file contains hidden or 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
arrayOfLikeness : [ | |
( | |
name: "Wyndham", | |
likeness: [ // This is the result form DNN model of each images that contains this person's face | |
[1.2,1.4, -10.3, .... ], | |
[1.2,1.4, -10.3, .... ], | |
[1.2,1.4, -10.3, .... ], | |
..., | |
[1.2,1.4, -10.3, .... ]] | |
), |
This file contains hidden or 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
fun Bitmap.bitmapToNV21ByteArray(): ByteArray { | |
val argb = IntArray(this.width * this.height) | |
this.getPixels(argb, 0, this.width, 0, 0, this.width, this.height) | |
val yuv = ByteArray( | |
this.height * this.width + 2 * ceil(this.height / 2.0).toInt() | |
* ceil(this.width / 2.0).toInt() | |
) | |
encodeYUV420SP(yuv, argb, this.width, this.height) | |
return yuv | |
} |
This file contains hidden or 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
val image = InputImage.fromByteArray( | |
bitmap?.bitmapToNV21ByteArray()!!, | |
bitmap!!.width, | |
bitmap!!.height, | |
screenOrientation, | |
InputImage.IMAGE_FORMAT_NV21 | |
) | |
faceDetector.process(image).addOnSuccessListener { image -> | |
// Continue processing here, | |
// including Cropping image from BoundingBox (which available inside `image` Object above) |
This file contains hidden or 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
val options = FaceDetectorOptions.Builder() | |
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST) | |
.setContourMode(FaceDetectorOptions.LANDMARK_MODE_NONE) // skips landmark mapping | |
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE) // skips facial expressions and other classification such as wink | |
.build() | |
val faceDetector = FaceDetection.getClient(options) |
This file contains hidden or 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
class PageDotDecorator( | |
private val itemCount: Int, | |
private val context: Context? | |
) : RecyclerView.ItemDecoration() { | |
companion object { | |
/** | |
* Draw Parameters | |
*/ | |
const val DOT_RADIUS = 3f |
NewerOlder