Skip to content

Commit 002c568

Browse files
Merge pull request spmallick#462 from vkoriukina/vk/opencv-gpu
added code for OpenCV GPU Support
2 parents 9569089 + 249df54 commit 002c568

File tree

5 files changed

+710
-0
lines changed

5 files changed

+710
-0
lines changed

OpenCV-GPU-Support/README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# OpenCV GPU Support
2+
3+
This repository contains the code for [OpenCV GPU Support](https://www.learnopencv.com/opencv-gpu-support/)
4+
5+
## Getting Started
6+
7+
Our code is tested using Python 3.7.5, but it should also work with any other python3.x. If you'd like to check your
8+
version run:
9+
10+
```bash
11+
python3 -V
12+
```
13+
14+
### Virtual Environment
15+
16+
Let's create a new virtual environment. You'll need to install [virtualenv](https://pypi.org/project/virtualenv/)
17+
package if you don't have it:
18+
19+
```bash
20+
pip install virtualenv
21+
```
22+
23+
Now we can create a new virtualenv variable and call it `env`:
24+
25+
```bash
26+
python3 -m venv env
27+
```
28+
29+
The last thing we have to do is to activate it:
30+
31+
```bash
32+
source env/bin/activate
33+
```
34+
35+
### Numpy
36+
37+
Install numpy package by running:
38+
39+
```bash
40+
pip install numpy
41+
```
42+
43+
### Installing CUDA
44+
45+
The code was tested using CUDA Toolkit 10.2. Please follow the official instruction to download
46+
[CUDA Toolkit 10.2](https://developer.nvidia.com/cuda-10.2-download-archive) or higher.
47+
48+
### OpenCV with CUDA Support
49+
50+
In this blog post, we're using OpenCV with CUDA support to accelerate OpenCV algorithms. That is why we will need to
51+
customize the OpenCV library build and make it from scratch. To do so:
52+
53+
1. Install dependencies:
54+
55+
```bash
56+
sudo apt-get update
57+
sudo apt-get install build-essential cmake unzip pkg-config
58+
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
59+
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
60+
sudo apt-get install libxvidcore-dev libx264-dev
61+
sudo apt-get install libgtk-3-dev
62+
sudo apt-get install libatlas-base-dev gfortran
63+
sudo apt-get install python3-dev
64+
65+
```
66+
67+
2. Download the latest OpenCV version from the official repository:
68+
69+
```bash
70+
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.4.0.zip
71+
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.4.0.zip
72+
```
73+
74+
3. Unzip the downloaded archives:
75+
76+
```bash
77+
unzip opencv.zip
78+
unzip opencv_contrib.zip
79+
```
80+
81+
4. Rename the directories to match CMake paths:
82+
83+
```bash
84+
mv opencv-4.4.0 opencv
85+
mv opencv_contrib-4.4.0 opencv_contrib
86+
```
87+
88+
5. Compile OpenCV:
89+
90+
Create and enter a build directory:
91+
92+
```bash
93+
cd opencv
94+
mkdir build
95+
cd build
96+
```
97+
98+
Run CMake to configure the OpenCV build. Don't forget to set the right pass to the `PYTHON_EXECUTABLE`. If you are using
99+
the CUDA version different from `10.2`, please change the last 3 arguments accordingly.
100+
101+
```bash
102+
cmake -D CMAKE_BUILD_TYPE=RELEASE \
103+
-D CMAKE_INSTALL_PREFIX=/usr/local \
104+
-D INSTALL_PYTHON_EXAMPLES=OFF \
105+
-D INSTALL_C_EXAMPLES=OFF \
106+
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
107+
-D PYTHON_EXECUTABLE=env/bin/python3 \
108+
-D BUILD_EXAMPLES=ON \
109+
-D WITH_CUDA=ON \
110+
-D CUDA_FAST_MATH=ON \
111+
-D WITH_CUBLAS=ON \
112+
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
113+
-D OpenCL_LIBRARY=/usr/local/cuda-10.2/lib64/libOpenCL.so \
114+
-DOpenCL_INCLUDE_DIR=/usr/local/cuda-10.2/include/ \
115+
..
116+
```
117+
118+
Check the output and make sure that everything is set correctly. After that we're ready to build it with:
119+
120+
```bash
121+
make -j4
122+
```
123+
124+
Make sure, you didn't get any errors. Then run the following command:
125+
126+
```bash
127+
sudo ldconfig
128+
```
129+
130+
which creates the necessary links and cache to our freshly built shared library.
131+
132+
Rename the created Python3 bindings for OpenCV to `cv2.so`:
133+
134+
```bash
135+
mv lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so cv2.so
136+
```
137+
138+
The last step is to create a symlink of our OpenCV `cv2.so` into the virtual environment installed packages:
139+
140+
```bash
141+
cd env/lib/python3.7/site-packages/
142+
ln -s ~/opencv/build/cv2.so cv2.so
143+
```
144+
145+
## Running the Demo
146+
147+
**C++**
148+
149+
You first need to compile .cpp file with the following command:
150+
151+
```bash
152+
g++ `pkg-config --cflags --libs opencv4` demo.cpp -o demo.out -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -lopencv_video -lopencv_cudaarithm -lopencv_cudaoptflow -lopencv_cudaimgproc -lopencv_cudawarping -std=c++11
153+
154+
```
155+
156+
After that to run the demo, you will need to pass:
157+
158+
- path to the video file,
159+
- device, to choose between CPU and GPU inference. By default, the device is set to "cpu".
160+
161+
For example:
162+
163+
```bash
164+
./demo.out video/boat.mp4 gpu
165+
```
166+
167+
**Python**
168+
169+
To run the demo, you will need to pass:
170+
171+
- `--video` argument to set the path to the video file,
172+
- `--device` to choose between CPU and GPU inference. By default, the device is set to "cpu".
173+
174+
For example:
175+
176+
```bash
177+
python3 demo.py --video video/boat.mp4 --device "cpu"
178+
```
179+
180+
181+
# AI Courses by OpenCV
182+
183+
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start.
184+
185+
<a href="https://opencv.org/courses/">
186+
<p align="center">
187+
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png">
188+
</p>
189+
</a>

0 commit comments

Comments
 (0)