avaf
July 3, 2022, 8:59pm
1
These are my experiments with NPU to detect objects in real-time using MIPI CSI OV5647 and a USB camera to show how exposure affects the results. I am no expert in AI / NPU but i think there is room for some enhancements. The demo use OpenCV4.2, SDL2 (KMSDRM), and NPU. It is based on odroid-m1 example of how to detect objects in real-time with NPU.
note:
The lens used with OV5647 and the USB 2 MB sensor is the same.
OV5647 captures frames at 15 FPS while the USB at 5 FPS. I can capture frames (1920x1080) at 30 FPS with H264 on the USB camera but i could not make OpenCV work with H264. I tried FFMpeg which i am able to decode H264 in hw but OpenCV refuses to return the frame.
The setup:
Ubuntu 20.04 (debloated) CLI with mail blob
kernel 4.19.193
OpenCV 4.2
NPU
Shooting with different exposure (ov5647 - $ 19.00 with IR):
https://drive.google.com/file/d/1ho9G7_mIKO5zEiqJ_RWL23NHrMXlAYRd/view
https://drive.google.com/file/d/1Zy_3FyCDZVAg3uSWj38Nz17GYbutzKqU/view
https://drive.google.com/file/d/1vAftvMvsmJb6qHXTjvXGtgkqGSWZdsfJ/view
Shooting with USB camera (2 MB - $ 30.00)
https://drive.google.com/file/d/19DEaJrXDEalCuttyX4ce3zZ2ccOCqY4k/view
How it is done:
grab a frame from the camera (1980x1080)
adjust to 640x640
run NPU
render on screen (1920x1080) with sdl2 using KMSDRM
The experiment is done using the following sources:
Simple Directmedia Layer - ROCKCHIP 3D hw accel (OpenGLES2) - Deprecated
Contribute to hardkernel/rknpu2 development by creating an account on GitHub.
OV56447 driver port by @abel
For future enhancements, i am looking for removing OpenCV as it grabs the frame using gstreamer which seems to do some minor image conversion and image enhancements but has some memory leaks. I switched to pure v4l2 to fix the memory leak.
I opened an issue report with OpenCV, but i am not sure if the memory leak is on OpenCV or gstreamer.
If anyone happens to know how to fix it, please do so.
opened 12:37AM - 29 Jun 22 UTC
closed 11:08PM - 19 Oct 22 UTC
There is a small memory leak while capturing video from a USB / CSI camera attac… hed to an arm64 sbc board.
Running on a rockchip rk3568 board with hardware acceleration (KMSDRM), capturing YUV (USB cam) or NV12 (CSI cam) frames, and rendering on screen with SDL2 leaks memory.
The leak is on cap_gstreamer.cpp, it is small but in the long run (24/7) can have a significant impact. It is about 24 to 32 bytes every frame, sometimes a bit more.
The memory leak does not seem to affect the OpenCV 3.2.0+dfsg (Ubuntu x64), cap_gstreamer.cpp is not called with the same camera attached. There are other leaks, but not related to the same leak experienced on the arm64 board.
I have no experience with OpenCV and/or gstreamer, so i don't know why gstreamer is involved here.
##### System information (version)
- OpenCV => 4.2.0 (opencv-4.2.0+dfsg Ubuntu package)
- Operating System / Platform =>
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
- Compiler => g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
##### Detailed description
Capturing the frames (YUV or NV12) from a USB camera or CSI camera leaks a small amount of memory, usually 32 bytes or 128 bytes.
The sample attached to reproduce the problem only reads the frames and is not rendered on the screen. I would expect only v4l2 calls but for some reason, cap_gstreamer opens the camera device and looks like leaks memory.
##### Steps to reproduce
Attach a USB camera, and build and run the sample code attached.
**Build with OpenCV 4.2.0 (Ubuntu package):**
`g++ main6.cpp -o opencv-6 -I/usr/include -I/usr/include/opencv4 -D REENTRANT -lpthread -ldl -lm -lopencv_highgui -lopencv_videoio -lopencv_imgproc -lopencv_core
`
**Run:**
`valgrind --leak-check=full ./opencv-6`
##### Issue submission checklist
- [ Yes ] I report the issue, it's not a question
- [ Yes ] I checked the problem with documentation, FAQ, open issues,
forum.opencv.org, Stack Overflow, etc and have not found any solution
- [ No ] I updated to the latest OpenCV version and the issue is still there
- [ Yes ] There is reproducer code and related data files: videos, images, onnx, etc
[valgrind_x86_opencv3.2.txt](https://github.com/opencv/opencv/files/9006296/valgrind_x86_opencv3.2.txt)
[main6.cpp.txt](https://github.com/opencv/opencv/files/9006301/main6.cpp.txt)
[valgrind_rockchip_arm64_opencv4.2.txt](https://github.com/opencv/opencv/files/9006303/valgrind_rockchip_arm64_opencv4.2.txt)
[cap_gstreamer.cpp.tar.gz](https://github.com/opencv/opencv/files/9006307/cap_gstreamer.cpp.tar.gz)
Cheers
5 Likes