-
-
Notifications
You must be signed in to change notification settings - Fork 55.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android NDK camera support #19597
Android NDK camera support #19597
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done! Thank you for contribution 👍
So, should now with this update |
@jumpy88 it will work for any device that is accessible to the Android NDK Camera API (https://developer.android.com/ndk/reference/group/camera)
This link is aimed at device manufacturers who are building the Android OS itself, not general Android app developers. (Also note: Camera2 is a Java API whereas the OpenCV |
@komakai Ops, sorry, I thought Camera2 was an API for developers and it was accessible in Android NDK. My knowledge about Android is still very poor. Ok, I'll give it a try with your code and will post the result. |
Hi, I've just done some experiments with 2e2df44 commit in a Godot Engine proof of concept. It works with built-in cameras but doesn't seem to recognize external USB cameras. The only weird thing is sometimes the visualized frame appears having blue and red channels swapped, but I don't have enough information to open an issue (I don't know if it's due to OpenCV itself or is my fault). You can find the sample code here https://github.com/jumpy88/godot_ocv-camera-reader |
} | ||
return true; | ||
case CV_CAP_PROP_CONVERT_RGB: | ||
convertToRgb = (value != 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cv::cvtColor(yuv, out, convertToRgb ? cv::COLOR_YUV2RGB_YV12 : cv::COLOR_YUV2BGR_YV12);
BTW, meaning of this property is different (name misleads unfortunately, it is not BGR / RGB switch):
- CV_CAP_PROP_CONVERT_RGB = 1 (default): VideoCapture returns frames as "BGR" 8UC3 cv::Mat.
- CV_CAP_PROP_CONVERT_RGB = 0: VideoCapture returns frames in other formats, like NV12, Y8, etc (format is retrieved and/or sometimes specified through
CAP_PROP_FOURCC
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alalek OK thanks for the explanation - will prepare a fix for this when I get a minute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a space in the api to allow for the BGR/RGB switch in addition to the correct convertToRGB behaviour?
cheers, Jeff
Hello, I am using opencv 4.5.4. python reports on android: after calling cam = cv2.VideoCapture(0) but i can't get a frame from the video stream. If i call retval, frame = cam.read() retval is returned as False and Frame is None. Do i miss something? The code works on windows. |
@komakai Do you know what i am missing here? |
@Ay-De running OpenCv Python on Android is not something I have tested. I would suggest to look in the Logcat output to see if there are any error messages there |
Hello, i am using kivy to run python code on android. OpenCV is being imported, videocapture(0) (or 1) works also. As i said, the isOpened() function also returns true, but if i try to read a frame, with read(), that returns nothing. No error is thrown according to logcat, but the variable for frame says it is None. |
@Ay-De Do you have an APK file you can share? |
Yes, here: There is also the source code (See the main.py in develop branch. sorry it is still quite messy). The APK is compiled with debug mode enabled. If you execute the main.py on a windows computer with a webcam, it will work without errors (i tested it on a Surface Pro 7). |
OK - so I tried your APK and saw lots of output like this: 2022-02-14 21:30:53.148 26028-26132/org.ayde.kivycamera V/NativeCamera: session 0x7bce246ba0 capture completed |
Ok sorry i somehow missed that part. I think the problem lies in returned image format from the sensor? Is there a way to specify it in OpenCV? OpenCV has a flag CAP_PROP_FORMAT to fetch undecoded RAW video streams, but unfortunatly, the android camera seems to support different ones and i can't currently find a way to set it on the Android side. Would it maybe be possible to set the camera format after opening the image stream by making direct API calls? I am sorry if i am derailing this thread here! |
@Ay-De The error message is coming from this line: There were some bug fixes recently in that area of the code: |
So, first of all, thanks for your help. I really appreciate it. I updated my files to include opencv in version 4.5.5, which include the above mentioned fixes, but it still doesn't work. May i ask you, which tools you are using to get the above mentioned logs?
I am using logcat for debugging but i am not seeing this errors, i guess i am maybe filtering out to much? I would like to understand the problems myself better instead of just asking and for that i think i need a better insight. I am currently only seeing these logs:
Sorry for all these probably easy to fix questions.^^ |
There is an Android/Java sample here: |
I finally got it to work. Thanks @komakai. |
Let us know any further improvements committed for external USB camera access? For external camera to support we need to enable below feature. My understanding is for some android mobile manufactures, USB Webcam access support is available, in that mobile CAMERA2 API will work. ie,. external camera will be detected in CameraManager API. |
Android NDK camera support * Add native camera video backend for Android * In the event of a "No buffer available error" wait for the appropriate callback and retry * Fix stale context when creating a new AndroidCameraCapture * Add property handling
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
This PR fixes #11952
Add an Android native camera capture backend to the
videoio
module.Based loosely on https://github.com/android/ndk-samples/tree/main/camera
Requires Android native API level 24 or above (in order to access NDK camera APIs)