googlesamples / mlkit

A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Function isnt taking the path

Harshad0011 opened this issue · comments

private fun processProcessedVideo(uri: Uri) {
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(this, uri)

    val durationStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
    val duration = durationStr?.toLong() ?: 0
    // Define the output file path for the processed video
    val outputFilePath = Environment.getExternalStorageDirectory().path + "/TestVideo/processed_video.mp4"
    // Initialize a MediaMuxer to write frames into a video file
    val muxer = MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)


    val frameIntervalMicros = 1000L // 1 second in microseconds

    for (timeMicros in 0 until duration step frameIntervalMicros) {
        val frameBitmap = retriever.getFrameAtTime(timeMicros, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
        //val bitmap = BitmapFactory.decodeByteArray(frameBitmap, 0, frameBitmap.size)



        if (frameBitmap != null) {
            // Convert the bitmap frame to InputImage
            val inputImage = ImageUtils.bitmapToInputImage(frameBitmap)

            stillposeDetector.process(inputImage)
                    .addOnSuccessListener { results ->
                        runOnUiThread {
                            graphicOverlay.clear()

                            val poseGraphic = PoseGraphic(
                                graphicOverlay,
                                results,
                                false,  // Change this to false if you don't want to display classification text
                                false,  // Change this to false if you don't want to visualize Z
                                true,  // Change this to false if you don't want to rescale Z
                                listOf(),// Pass your classification list here if needed
                                false
                            )

                            graphicOverlay.add(poseGraphic)

                            // Save the processed frame to a directory
                            saveProcessedFrame(frameBitmap)
                            // Save the processed frame to the video file
                            val outputBufferInfo = MediaCodec.BufferInfo()
                            val outputIndex = muxer.addTrack(MediaFormat.createVideoFormat("video/mp4", frameBitmap.width, frameBitmap.height))
                            muxer.start()
                            val byteBuffer = ByteBuffer.allocate(frameBitmap.byteCount)
                            frameBitmap.copyPixelsToBuffer(byteBuffer)
                            byteBuffer.flip()
                            muxer.writeSampleData(outputIndex, byteBuffer, outputBufferInfo)

                        }
                    }
                    .addOnFailureListener { e ->
                        Log.e(TAG, "Pose detection failed", e)
                    }
                    .addOnCompleteListener {
                        // Close the image proxy to release system resources
                        Log.e(TAG, "Pose detection Success")
                    }
            // Release resources
            retriever.release()
            muxer.stop()
            muxer.release()
        }
        else{
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
        }
    }

    Toast.makeText(this, "Frames processed", Toast.LENGTH_SHORT).show()
}

In this above code I'm passing a pre recorded video with lowered fps = 10 to mlkit pose detection on STREAM_MODE but there is this error where path is not being found.
can someone also provide me limitations of mlkit? if you know.
I'm trying to recreate the video with pose graphicoverlay over it 

Could you share the error stacktrace? Also more information as requested in the issue template, e.g. what version of the SDK you are using? Device? OS level?...

@ai-plays i don't have my device currently but the issue rises with mediamuxer, as it was closed in loop it self and also was constantly starting in loop, ive adjusted the code accordingly later, by using muxer.start() before frame interval and muxer.stop() before final toast text.

Even after doing that it throws different error of improper initialisation of muxer.

I've ignored that currently, now i actually want help with main thing which is how can i get processed video after passing through mlkit pose detection for pre recorded video in Android app using Android studio in kotlin. I'm not full fledged Android developer so i don't have complete idea how that works that's why I need support.

E FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.posex, PID: 12592
java.lang.IllegalStateException: Can't stop due to wrong state(INITIALIZED)
at android.media.MediaMuxer.stop(MediaMuxer.java:492)
at com.example.posex.record.RecordActivity$processProcessedVideo$1.invokeSuspend(RecordActivity.kt:209)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e308eb2, Dispatchers.IO]
2024-02-26 10:16:49.412 12944-12944 OplusCusto...ionManager com.example.posex E sInstance is null, start a new sInstance

this is log being shown to me

Thanks for the additional info.

Based on your description, it is not an ML Kit API question, and this repo is not the best place for it.

Could you try rephrasing it to be a general video processing question and ask in general Android dev channels like StackOverflow?