Skip to content

Instantly share code, notes, and snippets.

View muhwyndhamhp's full-sized avatar

M Wyndham Haryata Permana muhwyndhamhp

View GitHub Profile
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))
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)
}
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) }
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
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'
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, .... ]]
),
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
}
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)
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)
class PageDotDecorator(
private val itemCount: Int,
private val context: Context?
) : RecyclerView.ItemDecoration() {
companion object {
/**
* Draw Parameters
*/
const val DOT_RADIUS = 3f