简体   繁体   English

如何转换相机图像以在 tflite 中使用它

[英]How to convert camera image to use it in tflite

I have an android app in Kotlin which depends on CameraX to analyze images and pass them to tensorflow lite image detection model ( not classification ).我在 Kotlin 中有一个 android 应用程序,它依赖于 CameraX 来分析图像并将它们传递给 tensorflow 精简图像检测 model(不是分类)。 The problem is we get YUV or RGBA Output images and we need an argb bitmap to pass to tflite model to detect objects we have been struggling for weeks and we have a deadline soon help please.问题是我们得到 YUV 或 RGBA Output 图像,我们需要一个 argb bitmap 传递给 tflite model 来检测我们已经苦苦挣扎了数周的物体,我们很快就有了截止日期,请帮忙。

    ImageAnalysis.Analyzer {
    private val supportedImages = listOf(ImageFormat.FLEX_RGBA_8888, ImageFormat.FLEX_RGB_888)
    private var data = ""

    @SuppressLint("UnsafeOptInUsageError")
    override fun analyze(image: ImageProxy) {
        if (image.format in supportedImages) {
            val options = ObjectDetector.ObjectDetectorOptions.builder()
                .setScoreThreshold(0.2F)
                .setBaseOptions(BaseOptions.builder().build())
                .build()
            val objectDetector =
                ObjectDetector.createFromFileAndOptions(
                    context, Model.createModel(context, "model.tflite").path, options
                )
            val bitmap = toBitmap(image)
            val tfImage = TensorImage.fromBitmap(bitmap)
            val results: List<Detection> = objectDetector.detect(tfImage)
            for (result in results) {
                val categories = result.categories
                for (category in categories) {
                    val label = category.label
                    val boundingBox = result.boundingBox
                    data += "label : $label | bounding : $boundingBox"
                }
            }
            listener(data)
            image.close()
        }
    }

    private fun toBitmap(image: ImageProxy): Bitmap {
        val r = image.planes[0].buffer.int
        val g = image.planes[1].buffer.int
        val b = image.planes[2].buffer.int
        val a = image.planes[3].buffer.int
        var color = Color.argb(a, r, g, b)
        val rect = Rect(0, 0, 1, 1)
        color = ColorUtils.compositeColors(color, Color.WHITE)
        val bitmap = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val paint = Paint()
        paint.color = color
        canvas.drawRect(rect, paint)
        savePic(bitmap = bitmap!!, display_name = Random(100000L).toString())
        return bitmap
    }
    private fun savePic(bitmap: Bitmap, display_name: String): Boolean {
        val mediaCollection = latest {
            MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        } ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        val contentValues = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, "${display_name}.jpg")
            put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            put(MediaStore.Images.Media.WIDTH, bitmap.width)
            put(MediaStore.Images.Media.HEIGHT, bitmap.height)
        }
        return try {
            context.contentResolver.insert(mediaCollection, contentValues)?.also { uri ->
                context.contentResolver.openOutputStream(uri).use { outputStream ->
                    if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)) {
                        throw IOException("could not save bitmap!")
                    }
                }
            } ?: throw IOException("could not create media store entry")
            return true
        } catch (e: IOException) {
            e.printStackTrace()
            false
        }
    }

}```


Here is the function that converts YUV_420_888 imageproxy into argb format bitmap.这里是将YUV_420_888 imageproxy转成argb格式的function bitmap。

I know a late answer but providing for others.我知道一个迟到的答案,但为其他人提供。

fun imageToBitmap(image: Image): Bitmap {
    val planes: Array<Image.Plane> = image.planes
    val yBuffer: ByteBuffer = planes[0].buffer
    val uBuffer: ByteBuffer = planes[1].buffer
    val vBuffer: ByteBuffer = planes[2].buffer
    val ySize = yBuffer.remaining()
    val uSize = uBuffer.remaining()
    val vSize = vBuffer.remaining()
    val nv21 = ByteArray(ySize + uSize + vSize)
    //U and V are swapped
    yBuffer[nv21, 0, ySize]
    vBuffer[nv21, ySize, vSize]
    uBuffer[nv21, ySize + vSize, uSize]
    val yuvImage = YuvImage(nv21, ImageFormat.NV21, image.width, image.height, null)
    val out = ByteArrayOutputStream()
    yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 75, out)
    val imageBytes: ByteArray = out.toByteArray()
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}

Provide imageproxy from cameraX analyser to this function and it will provide the bitmap.从 cameraX 分析器向这个 function 提供图像代理,它将提供 bitmap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android TFLite 的 Camera X ImageAnalyzer 图像格式 - Android Camera X ImageAnalyzer Image Format for TFLite 如何在android studio中使用.tflite model进行图像分类 - How to use .tflite model in android studio for image classification 显示在 flutter 应用程序中的图像帧上运行 tflite 的相机活动 - Show a Camera Activity which runs tflite on image frames in a flutter app 如何将.png图像转换为字节数组-Android,TFLITE应用程序 - How do I convert a .png image to a Byte Array - Android, TFLITE application 如何将TensorFlow JSON图形模型转换为.tflite? - How to convert a TensorFlow JSON graph model to .tflite? 在Firebase图像标签中如何将TFLite模型与uint8的输入和输出数据类型一起使用 - How to use TFLite model with input and output datatype of uint8 in Firebase image labeling 如何将图像传递给android中的tflite model - How to pass image to tflite model in android 如何在Android中使用相机捕获图像和视频? - How to use camera in android to capture image and video? 如何在 tflite 中使用posenet模型的输出 - How to use outputs of posenet model in tflite 如何在android studio中将相机图像字节数组转换为Uri - How to convert a camera image byte array to Uri in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM