Skip to content Skip to sidebar Skip to footer

Android: Run Image Classifier In Infinite Loop

I want to run a image classifier in a infinite loop on a background thread. The function should be called immediately after launching the app. I want to feed the classifier with cu

Solution 1:

Here is the gist of my solution. One needs to fit in the actual image classifier at the SystemClock.sleep part. The trick is to use TextureView instead of ImageView or VideoView, since it's faster and more flexible

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_playback);
    initializeBottomSheet();


    uri_video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_demo_video);

    textureView = findViewById(R.id.textureView);
    textureView.setSurfaceTextureListener(this);

    mediaPlayer = new MediaPlayer();
    assert textureView != null;

    startBackgroundThread();
}




/** Starts a background thread and its {@link Handler}. */
private void startBackgroundThread() {
    backgroundThread = new HandlerThread(HANDLE_THREAD_NAME);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());
    synchronized (lock) {
        runClassifier = true;
    }
    backgroundHandler.post(periodicClassify);
}

/** Stops the background thread and its {@link Handler}. */
private void stopBackgroundThread() {
    backgroundThread.quitSafely();
    try {
        backgroundThread.join();
        backgroundThread = null;
        backgroundHandler = null;
        synchronized (lock) {
            runClassifier = false;
        }
    } catch (InterruptedException e) {
        Log.e(TAG, "Interrupted when stopping background thread", e);
    }
}

private Runnable periodicClassify =
        new Runnable() {
            @Override
            public void run() {
                // Get current frame from video playback
                mBitmap = textureView.getBitmap();
                if (classifier != null && mBitmap != null) {
                    Log.d(TAG, "Classifier: Start thread");
                    SystemClock.sleep(3000); // Instead I simulate the classifier via sleep
                }
                backgroundHandler.post(periodicClassify);
            }
        };


@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    Surface surface = new Surface(surfaceTexture);
    Context context = getApplicationContext();
    try {
        mediaPlayer.setDataSource(context, uri_video);
        mediaPlayer.setSurface(surface);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){

            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();

                textureView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mediaPlayer.isPlaying()){
                            mediaPlayer.pause();
                        }else{
                            mediaPlayer.start();
                        }
                    }
                });
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }



}



@Override
protected void onDestroy() {
    super.onDestroy();
    stopBackgroundThread();
    if(mediaPlayer != null){
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

@Override
protected void onPause() {
    super.onPause();
    if(mediaPlayer != null && mediaPlayer.isPlaying()){
        mediaPlayer.pause();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(mediaPlayer != null && mediaPlayer.isPlaying()){
        mediaPlayer.start();
    }
    startBackgroundThread();
}

Post a Comment for "Android: Run Image Classifier In Infinite Loop"