Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
        mDetectingThread.start();
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

How when i run app on my phone, it crashes and report "java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:724) at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373) at java.lang.Thread.run(Thread.java:764)" Who can give me some suggestions ? Thanks in advance !

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
        mDetectingThread.start();
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

How However when i run app on my phone, it crashes and report "java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:724) at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373) at java.lang.Thread.run(Thread.java:764)" Who can give me some suggestions ? Thanks in advance !

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
private boolean startdect = false;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
         if (startdect == false) {
           mDetectingThread.start();
        }
        startdect = true;
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

However when i run app on my phone, it crashes and report "java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:724) at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373) at java.lang.Thread.run(Thread.java:764)" Who can give me some suggestions ? Thanks in advance !

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
private boolean startdect = false;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
         if (startdect == false) {
           mDetectingThread.start();
        }
        startdect = true;
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

However when i run app on my phone, it crashes and report "java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:724) at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373) at java.lang.Thread.run(Thread.java:764)" Who can give me some suggestions ? Thanks in advance !

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
private boolean startdect = false;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
         if (startdect == false) {
           mDetectingThread.start();
        }
        startdect = true;
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

However when i run app on my phone, it crashes and report "java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:724) at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373) at java.lang.Thread.run(Thread.java:764)" Who can give me some suggestions ? Thanks in advance !

click to hide/show revision 6
None

updated 2019-11-02 10:17:02 -0600

berak gravatar image

How to create a new thread for object detection task?

I deploy a DNN in my android app, but the FPS is just 4 that makes the frames looks discontinuous, so i intend to create a new thread to handle the object detection task, Here is part of the code:

private Thread mDetectingThread = null;
private boolean startdect = false;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if (mDetectingThread == null) {
            mDetectingThread = new ObjectDectThread();
        }
         if (startdect == false) {
           mDetectingThread.start();
        }
        startdect = true;
        return mRgba;
    }
class ObjectDectThread extends Thread {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 0.007843;
    final double MEAN_VAL = 127.5;
    final double THRESHOLD = 0.3;
    @Override
    public void run() {
        super.run();
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(mRgba, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob);
        Mat detections = net.forward();
        int cols = mRgba.cols();
        int rows = mRgba.rows();
        detections = detections.reshape(1, (int) detections.total() / 7);
        for (int i = 0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int) detections.get(i, 1)[0];
                int left = (int) (detections.get(i, 3)[0] * cols);
                int top = (int) (detections.get(i, 4)[0] * rows);
                int right = (int) (detections.get(i, 5)[0] * cols);
                int bottom = (int) (detections.get(i, 6)[0] * rows);
                Imgproc.rectangle(mRgba, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0), 2);
                String label = classNames[classId] + ": " + confidence;
                Imgproc.putText(mRgba, label, new Point(left, top - 5),
                        Core.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 0, 0), 2);
            }
        }
    }
}

However when i run app on my phone, it crashes and report

"java.lang.IllegalThreadStateException
 at java.lang.Thread.start(Thread.java:724)
 at com.example.iguardonxiaomi8se.MainActivity.onCameraFrame(MainActivity.java:236)
 at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
 at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
 at java.lang.Thread.run(Thread.java:764)"

Who can give me some suggestions ? Thanks in advance !