Question

I have been facing a problem while processing the camera feeds in android Application for 'face detection''.

What i am trying to do:

-I have An activity which shows continuosly the phone camera feed in a surfaceView(UI thread)

-To avoid performance headaches,I put a surfaceview on top to draw the Rectangles over the detected faces using OpenCv(This is running in a thread,say thread2)

-I am trying to pass a copy of the Direct feed image to the Thread2 so that the thread 2 can do the detection and draw on the overlay surfaceview

Where i am now:

-I was thinking of using a thread with a blockingqueue so that the UIthread can put the Image in to the BlockingQueue using BlockingQueue.add(..) and the Thread2 can take the image in the BlockingQ ,process it and detect faces in background

Conditions: I want the UI thread ,not to wait,so that uniterupted feed is seen on the display.It only need to put the image and then without waiting for anything else it can go and fetch next feed and process is repeated

Currently i am using a new class implementing Runnable interface which has two methods putImage(bitmap) and getImage() which puts and get images using the blockingQueue.putImage() is called by UIthread and getImage() is called by thread2

The problem: When i keep putting on the Images to the blocking Queue,the Thread2 cant access the blockingQ(Itsblocked) and since the UI thread should run uninterrupted,I cannot call wait() or sleep() in the Main thread.

Is there any way Thread2 can get this Images from Queue without blocking the MainThread???

Was it helpful?

Solution

What you have here is a typical producer-consumer problem. Your producer should be the main thread. And the consumer is the one that detects the faces. It is the consumer that will have to run in a different thread. There is no need for a separate thread that handles the queue, it can be anywhere where it is accessible for the producer and the consumer.
This way there shouldn't be any blocking unless your queue is bounded. A default LinkedBlockingQueue is practically unbounded.
To access ready products without blocking you could do the following: Declare an other queue that will hold the result of the done tasks. The face processor will be it's producer and the main thread will be the consumer. Let the processor fill it as it goes, and the main thread periodically check it for ready products. If it finds that the result queue is not empty, you can notify the user or do whatever you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top