android Google ML Kit人脸检测无法从URI识别人脸

k2fxgqgv  于 2023-03-06  发布在  Android
关注(0)|答案(1)|浏览(185)

我正在使用谷歌ml工具包进行人脸检测,然后使用边框功能来裁剪人脸。我成功地集成了摄像头,然后想添加从图库中选择功能,我成功地实现了以前用Java编写的培训应用程序,但我可以找到为什么用Kotlin编写的应用程序无法从图库中识别图像URI。
我尝试了多种方法InputImage.fromBirmap(),InputImage.fromFilePath()和InputImage.fromByteArray(“use nv21”),并使用Image View来显示旋转Angular 看起来很完美的图像。你可以找到下面的代码,除了InputImage.fromFilePath()和InputImage.fromByteArray(“use nv21”),它们是第一个尝试的,然后删除代码。也谢谢你。

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 {  }
    }
wydwbb8l

wydwbb8l1#

问题是由if块引起的,它看起来像“if(faces.isNullOrEmpty())"。你发现错误了吗,是的,我忘记添加'!'了。
浪费您的时间,请接受我最诚挚的歉意。

相关问题