GPU-accelerated Docker container with OpenCV 4.5, Python 3.8 , GStreamer and CUDA 10,2
6.6K
sm_80. I did not test this case. Not sure )Pull the image from here :
$ docker pull fizmath/gpu-opencv:latest
With GPU
You need to install NVIDIA Container Toolkit on your machine. Run the container by this command :
$ docker run --gpus all -it --rm fizmath/gpu-opencv:latest
root@22067ad0cc87:/myapp#
With CPU :
If no GPU available on your machine, yet you can use the container with Docker
$ docker run -it --rm fizmath/gpu-opencv:latest
root@cc00562d816e:/myapp#
for running the example .py files in this repo with CPU you should comment these two lines :
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
Download this repo . The pretrained models are included, but you just need to download one of them yolov4.weights. And also no arg-parsing for the simplicity
Unzip and set its directory in your machine as your docker volume :
$ docker run --gpus all -it --rm -v <your volume dir>:/myapp fizmath/gpu-opencv:latest
root@771c5bcb2895:/myapp# ls
Dockerfile YOLOv4_cam.py face_SSD.py model_SSD model_YOLOv4
README.md cv2_info.py golden_axe.png model_SURE super_resolution.py
Print out OpenCV build info into a textile, check out the produced output in your volume :
root@771c5bcb2895:/myapp# python3 cv2_info.py
Test image Super-Resolution with the included image, a SR image SURE_golden_axe.png will be produced in your volume :
root@771c5bcb2895:/myapp# python3 super_resolution.py
compare both images visually. If you run out of GPU memory , make inference by your CPU cores ; see the .py file.
For the next two examples, you need to include these commands to docker run :
-e DISPLAY=$DISPLAY : this sends the display id from your machine to the container.
--device="/dev/video0:/dev/video0" : this lets the container find the camera.
-v /tmp/.X11-unix:/tmp/.X11-unix:rw : this lets the container find the display via X server. In order to display the GUI with Docker, the X client in the Docker container needs to communicate with the host X server.
Note that I tested the above commands in UBUNTU. These may differ in other systems.
Real-time GPU accelerated face detection with OpenCV DNN and GStreamer . Before running the container type in your CMD :
$ xhost +
access control disabled, clients can connect from any host
which allows the user to access the running X server. After being done with the examples type xhost - for the sake of security
$ docker run --gpus all --rm -it -e DISPLAY=$DISPLAY -v <volume dir>:/myapp -v /tmp/.X11-unix:/tmp/.X11-unix:rw --device="/dev/video0:/dev/video0" fizmath/gpu-opencv:latest
root@1be1f7efabf9:/myapp# python3 face_SSD.py

I drew encircling ellipses instead of the common face bounding boxes . Click q to terminate the session.
Real-time object detection with YOLO v4, GStreamer and CUDA. First download yolov4.weights and put it in it's folder. Following from the above:
root@1be1f7efabf9:/myapp# python3 YOLOv4_cam.py
bring some objects from this list in front of your camera.
Type into the container's shell :
root@1be1f7efabf9:/myapp# gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink
then a GStreamer hello world window pops up . See the documentation.
Export OpenCV Log Levels or GStreamer Debug Levels into our container's shell . For example:
root@1be1f7efabf9:/myapp# export OPENCV_LOG_LEVEL=INFO
or
root@1be1f7efabf9:/myapp# export GST_DEBUG=2
then run one of the examples and browse the output.
The Dockerfile culprit for the image may not have a perfect structure . It is just my own assembly. You may modify, upgrade and build a proper one for your requirements :
$ docker build -f Dockerfile -t <name>:<tag> .
It won't be that straight-forward, you will get some deprecation warnings and compatibility issues.
To keep the image light-weight and compatible with old GPU architectures SM_30 , SM_35 , SM_37 I implanted 10.2-cudnn7-devel-ubuntu18.04 as base
image which in compressed form is about 1.7 GB lighter than the latest image 11.3.0-cudnn8-devel-ubuntu20.04 .
Content type
Image
Digest
Size
2.6 GB
Last updated
over 5 years ago
docker pull fizmath/gpu-opencv