opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
76.49k stars 55.64k forks source link

C++ cv::VideoCapture.open(0) always return false on Android #11952

Closed omatrot closed 3 years ago

omatrot commented 5 years ago
System information (version)
Detailed description
Context

I'm writing a cross platform OpenCV based C++ library. The consuming code is a React Native Application through a react native native module.

To be perfectly clear, there is no access from Java Code to C++ OpenCV on Android. There are events with the result of the OpenCV C++ code sent to Javascript through the react native bridge.

My native library is compiled on Android as a SHARED library. It is dynamically linked to the libopencv_world.so that is produced by the compilation of OpenCV C++ for Android.

What it does

Basically, it opens the device's default camera and take snapshots.

The outcome

This code is then ran on iOS and Android.

This is working perfectly well on iOS. It fails on Android.

Here is the failing part of C++ code on Adndroid:

Steps to reproduce
// cap is a cv::VideoCapture object    
if (cap.open(0))
        {
            cap.set(cv::CAP_PROP_FRAME_WIDTH, CAM_WIDTH);
            cap.set(cv::CAP_PROP_FRAME_HEIGHT, CAM_HEIGHT);
        }
        else
        {
            reject("false", "cap.open(0) returned false");
        }
komakai commented 3 years ago

@splinterwhatever thanks for testing and for your feedback. I'll have a go at fixing the stop/restart problem and then put together a PR. BTW what is your use case? Is there some reason you prefer native camera access over the JavaCamera* classes?

splintersilk commented 3 years ago

Hey @komakai That would be awesome! Is there a way to help make this part of the official openCV repo?

To your question: I'm learning/working on a project that relies upon some cross-platform frameworks, including openCV. As such my language of choice for this project is C++, hence my aversion to Java thus far.

Beyond the stop/restart issue, the other thing I'm challenged with is capture resolution. Try as I might, on Android I only seem to be able to pull in the max res for the camera - which understandably slows things down. I'd like to be able to query the available resolutions per camera, then set it accordingly. cv::VideoCapture.set and get seem to allow for this, but on my oneplus5 handset I've had no luck so far.

https://answers.opencv.org/question/186435/unable-to-set-video-capture-size/

jumpy88 commented 3 years ago

Hello everyone, I'm trying to accede USB cameras in non-rooted Android devices in C++. I'm trying to use https://github.com/komakai/UVCCamera/tree/komakai-fixes but I can't find ho to set it up without using the Java wrapper in the examples provided with the library. If someone is interested or knows useful information we can discuss about it in a dedicated issue I've created https://github.com/saki4510t/UVCCamera/issues/593 Thank you in advance for the contribution.

komakai commented 3 years ago

@splinterwhatever I have pushed a new commit with a fix for the hang when attempting to restart Camera capture.

I'd like to be able to query the available resolutions per camera, then set it accordingly. cv::VideoCapture.set and get seem to allow for this, but on my oneplus5 handset I've had no luck so far.

Setting the resolution won't work yet because I haven't implemented the AndroidCameraCapture::setProperty function yet

splintersilk commented 3 years ago

@komakai Thats awesome. I was looking forward to checking it out - just got around to it.

Hmmm I must be doing something else wrong as I'm getting the same behaviour. Namely:

  1. Open a test app and see the capture going nicely.
  2. Return to home screen and then to another camera app (the standard android camera app in this case)
  3. Navigate back to the previously running test app.
  4. Note that there is no updating data from the cam device.

This is for: cv::VideoCapture cap {1}; //The self camera in my case cv::Mat frame; doing a cap>>frame displaying frame.

On a fresh run of the test app the camera behaves really well. When following the steps, by the time you return to the test app apparently frame is empty with 0 columns and 0 rows.

Obviously not requesting/expecting anyone to debug my silliness. Just wanted to clarify the steps in case it hints at an issue with the restarting camera issue.

Also thanks for clarity on the resolution/AndroidCameraCapture::setProperty situation. Would be superb if that got fixed too.

Let me know what I can do to help on either issue.

EDIT: To be clear, I don't see the freezing if I go to another app that doesn't require the camera, then return to the test app. Actually it works quite nicely in that scenario.

komakai commented 3 years ago

@jumpy88

but I can't find ho to set it up without using the Java wrapper in the examples provided with the library. If someone is interested or knows useful information we can discuss about it in a dedicated issue I've created

Looking at the top answer here: https://stackoverflow.com/questions/22197425/low-level-usb-api-on-android-at-ndk-level-to-use-on-qt-android it looks like what you're trying to do isn't possible without using some Java, specifically you need to use the UsbManager to discover your devices

jumpy88 commented 3 years ago

@komakai thanks for your reply! It's not what I was hoping but it's useful to know. I've also found some additional information https://forum.qt.io/topic/85176/crazy-frameworks-integration-and-i-m-stuck I'll make you know if I find a way to use UsbManager inside C++ source code (through NDK maybe).

wangrentao commented 2 years ago

Is it possible to open camera using cv::videoCapture(0) in android now?

Oxymore-coder commented 1 year ago

Is it possible to open camera using cv::videoCapture(0) in android now?

I second that question