Git Product home page Git Product logo

Comments (4)

shubham0204 avatar shubham0204 commented on July 28, 2024

@gowthami77, have you modified any variables in FaceNetModel.kt? Like imgSize or embeddingDim?

from facerecognition_with_facenet_android.

gowthami77 avatar gowthami77 commented on July 28, 2024

No, I was not changed anything

This is my file

// Utility class for FaceNet model
class FaceNetModel( context : Context ) {

// TFLiteInterpreter used for running the FaceNet model.
private var interpreter : Interpreter

// Input image size for FaceNet model.
private val imgSize = 112

// Output embedding size
private val embeddingDim = 192

// Image Processor for preprocessing input images.
private val imageTensorProcessor = ImageProcessor.Builder()
        .add( ResizeOp( imgSize , imgSize , ResizeOp.ResizeMethod.BILINEAR ) )
        .add( NormalizeOp( 127.5f , 127.5f ) )
        .build()

init {
    // Initialize TFLiteInterpreter
    val interpreterOptions = Interpreter.Options().apply {
        setNumThreads( 4 )
    }
    interpreter = Interpreter(FileUtil.loadMappedFile(context, "facenet_int8.tflite") , interpreterOptions )
}

// Gets an face embedding using FaceNet, use the `crop` rect.
fun getFaceEmbedding( image : Bitmap , crop : Rect , preRotate: Boolean ) : FloatArray {

     return runFaceNet(
         convertBitmapToBuffer(
             cropRectFromBitmap( image , crop , preRotate )
         )
     )[0]
}

// Gets an face embedding using the FaceNet model, given the cropped images.
fun getFaceEmbeddingWithoutBBox( image : Bitmap ) : FloatArray {
    return runFaceNet( convertBitmapToBuffer( image ) )[0]
}

// Run the FaceNet model.
private fun runFaceNet(inputs: Any): Array<FloatArray> {
    val t1 = System.currentTimeMillis()
    val outputs = Array(1) { FloatArray(embeddingDim ) }
    interpreter.run(inputs, outputs)
    Log.i( "Performance" , "FaceNet Inference Speed in ms : ${System.currentTimeMillis() - t1}")
    return outputs
}

// Resize the given bitmap and convert it to a ByteBuffer
private fun convertBitmapToBuffer( image : Bitmap) : ByteBuffer {
    val imageTensor = imageTensorProcessor.process( TensorImage.fromBitmap( image ) )
    Log.e("bitmap after",imageTensor.toString());

    return imageTensor.buffer
}

// Crop the given bitmap with the given rect.
private fun cropRectFromBitmap(source: Bitmap, rect: Rect , preRotate : Boolean ): Bitmap {
    var width = rect.width()
    var height = rect.height()
    if ( (rect.left + width) > source.width ){
        width = source.width - rect.left
    }
    if ( (rect.top + height ) > source.height ){
        height = source.height - rect.top
    }
    val croppedBitmap = Bitmap.createBitmap(
            if ( preRotate ) rotateBitmap( source )!! else source,
            rect.left,
            rect.top,
            width,
            height )
    Log.e("cropped bitmap","");

// saveBitmap( croppedBitmap , "image111")
return croppedBitmap
}

private fun saveBitmap(image: Bitmap, name: String) {
    val fileOutputStream =
            FileOutputStream(File( Environment.getExternalStorageDirectory()!!.absolutePath + "/$name.png"))
    image.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
}


private fun rotateBitmap(source: Bitmap): Bitmap? {
    val matrix = Matrix()
    matrix.postRotate( -90f )
    return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix , false )
}

}

can you please help me to resolve this issue. It was very important to me.

from facerecognition_with_facenet_android.

shubham0204 avatar shubham0204 commented on July 28, 2024

@gowthami77, you need to change the model's file path, as well as imgSize and embeddingDim like,


// Utility class for FaceNet model
class FaceNetModel( context : Context ) {

    // TFLiteInterpreter used for running the FaceNet model.
    private var interpreter : Interpreter

    // Input image size for FaceNet model.
    private val imgSize = 160

    // Output embedding size
    private val embeddingDim = 128

    // Image Processor for preprocessing input images.
    private val imageTensorProcessor = ImageProcessor.Builder()
            .add( ResizeOp( imgSize , imgSize , ResizeOp.ResizeMethod.BILINEAR ) )
            .add( NormalizeOp( 127.5f , 127.5f ) )
            .build()

    init {
        // Initialize TFLiteInterpreter
        val interpreterOptions = Interpreter.Options().apply {
            setNumThreads( 4 )
        }
        interpreter = Interpreter(FileUtil.loadMappedFile(context, "facenet_int8.tflite") , interpreterOptions )
    }

This seems to be a bug, but I'll fix it in 2/3 days, as I have to make some other modifications as well. These changes will come in the next commit. Also, please verify if these changes are helpful.

from facerecognition_with_facenet_android.

vishmanihub avatar vishmanihub commented on July 28, 2024

it works. Thanks for the fix.

from facerecognition_with_facenet_android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.