Google ML Kit面部检测无法从URI识别面部。

huangapple go评论57阅读模式
英文:

Google ML Kit face detection can not identify faces from uri

问题

I'm using google ml kit for face detection, then use bounding Box function to crop faces.
I successfully integrate cameras, then want to add select from gallery function, which I successfully implement previous training app that I wrote in Java, yet I can find why App written in Kotlin can not identify image Uri from gallery.

I tried multiple methods InputImage.fromBitmap(), InputImage.fromFilePath(), and InputImage.fromByteArray("use nv21"), and used ImageView to display images with rotation degrees that seem perfect. You can find the code initiated by the onClick method below, except InputImage.fromFilePath() and InputImage.fromByteArray("use nv21"), which were tried first, then deleted the codes.
Also, thank you beforehand.

private fun selectImage() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 11)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == 11) {
                if (data != null){
                    val bitmap = getBitmapFromUri(data.data!!)
                    
                    Log.d(TAG, "onActivityResult: ${data.data!!}")

                    val inputStream = contentResolver.openInputStream(data.data!!)
                    val exif = ExifInterface(inputStream!!)
                    val rotation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL
                    )
                    val rotationInDegrees: Int = exifToDegrees(rotation)
                    inputStream.close()
                    Log.d(TAG, "onActivityResult: rotation: $rotation")

                    if (bitmap != null) selectedImageAnalyser(bitmap, rotationInDegrees)
                }
            }
        }
    }

    private fun exifToDegrees(exifOrientation: Int): Int {
        return when (exifOrientation) {
            ExifInterface.ORIENTATION_ROTATE_90 -> {
                90
            }
            ExifInterface.ORIENTATION_ROTATE_180 -> {
                180
            }
            ExifInterface.ORIENTATION_ROTATE_270 -> {
                270
            }
            else -> 0
        }
    }

    private fun getBitmapFromUri(uri: Uri): Bitmap? {
        var bitmap: Bitmap? = null
        try {
            bitmap = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
                val source = ImageDecoder.createSource(
                    contentResolver, uri
                )
                ImageDecoder.decodeBitmap(source)
            } else {
                MediaStore.Images.Media.getBitmap(contentResolver, uri)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return bitmap
    }

    private fun selectedImageAnalyser(bitmap: Bitmap, rotationDegree: Int) {
        val mBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
        val inputImage = InputImage.fromBitmap(mBitmap, rotationDegree)
        val rotation = inputImage.rotationDegrees
        Log.d(TAG, "onActivityResult: rotation: $rotation")

        binding!!.ivFace.apply {
            visibility = View.VISIBLE
            setImageBitmap(inputImage.bitmapInternal)
        }

        faceDetector.process(inputImage)
            .addOnSuccessListener { faces ->
                if (faces.isNullOrEmpty()){
                    val croppedFace = cropToBox(bitmap,faces[0].boundingBox,rotation)
                    startEnrollDialog(croppedFace)
                } else{
                    Log.d(TAG, "selectedImageAnalyser: face cannot be identified")
                }
            }
            .addOnFailureListener { Log.d(TAG, "selectedImageAnalyser: \n\t Cause: ${it.cause} \n\t Message: ${it.message}") }
            .addOnCompleteListener {  }
    }
英文:

I'm using google ml kit for face detection, then use bounding Box function to crop faces.
I successfully integrate cameras ,then want to add select from gallery function, which I successfully implement previous training app that I wrote in Java, yet I can find why App written in Kotlin can not identify image Uri from gallery.

I tried multiple methods InputImage.fromBirmap(), InputImage.fromFilePath() and InputImage.fromByteArray("use nv21"), and used Image View to display images with rotation degrees that seems perfect. You can find codes that initiated by on click method below, except InputImage.fromFilePath() and InputImage.fromByteArray("use nv21") which tried first, then delete the codes.
Also thank you before hand.

private fun selectImage() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 11)
}
@RequiresApi(Build.VERSION_CODES.N)
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (requestCode == 11) {
if (data != null){
val bitmap = getBitmapFromUri(data.data!!)
Log.d(TAG, "onActivityResult: ${data.data!!}")
val inputStream = contentResolver.openInputStream(data.data!!)
val exif = ExifInterface(inputStream!!)
val rotation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
)
val rotationInDegrees: Int = exifToDegrees(rotation)
inputStream.close()
Log.d(TAG, "onActivityResult: rotation: $rotation")
if (bitmap != null) selectedImageAnalyser(bitmap, rotationInDegrees)
}
}
}
}
private fun exifToDegrees(exifOrientation: Int): Int {
return when (exifOrientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> {
90
}
ExifInterface.ORIENTATION_ROTATE_180 -> {
180
}
ExifInterface.ORIENTATION_ROTATE_270 -> {
270
}
else -> 0
}
}
private fun getBitmapFromUri(uri: Uri): Bitmap? {
var bitmap: Bitmap? = null
try {
bitmap = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
val source = ImageDecoder.createSource(
contentResolver, uri
)
ImageDecoder.decodeBitmap(source)
} else {
MediaStore.Images.Media.getBitmap(contentResolver, uri)
}
} catch (e: IOException) {
e.printStackTrace()
}
return bitmap
}
private fun selectedImageAnalyser(bitmap: Bitmap, rotationDegree: Int) {
val mBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
val inputImage = InputImage.fromBitmap(mBitmap, rotationDegree)
val rotation = inputImage.rotationDegrees
Log.d(TAG, "onActivityResult: rotation: $rotation")
binding!!.ivFace.apply {
visibility = View.VISIBLE
setImageBitmap(inputImage.bitmapInternal)
}
faceDetector.process(inputImage)
.addOnSuccessListener { faces ->
if (faces.isNullOrEmpty()){
val croppedFace = cropToBox(bitmap,faces[0].boundingBox,rotation)
startEnrollDialog(croppedFace)
} else{
Log.d(TAG, "selectedImageAnalyser: face can not identified")
}
}
.addOnFailureListener { Log.d(TAG, "selectedImageAnalyser: \n\t Cause: ${it.cause} \n\t Message: ${it.message}") }
.addOnCompleteListener {  }
}

答案1

得分: 0

问题是由这个if块引起的,它看起来像这样 "if (faces.isNullOrEmpty())"。您有注意到错误吗?是的,我忘记加 '!'。

请接受我最诚挚的道歉,浪费了您的时间。
最好的问候。

英文:

Problem was caused by if block which looks like this "if (faces.isNullOrEmpty())". Do you catch the mistake, yes I forgot to add '!'.

Please accept my sincerest apologies for wasting your time.
Best Regards

huangapple
  • 本文由 发表于 2023年2月8日 20:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定