This is a quick tutorial on how to get OpenCV working with PyQt4 on Virtualenv. At the time of this writing, I am using Ubuntu 16.04.2 LTS and OpenCV 3.1. I decided to write this tutorial post because it took me ages and a lot of 'Googling' to get the OpenCV working with PytQ4 in a virtual container. Hope this post will benefit those who want to play around with OpenCV + Qt Designer.
1. Make sure you have installed OpenCV 3.1 and set up a Virtualenv container. If not, please follow this excellent tutorial by Adrian Rosebrock:
Also ensure that your OpenCV + Python environment is using Python 2.7. I will assume that you have followed the tutorial instructions mentioned above. I also would like to add a few caveats to the OpenCV installation process:
- Ensure that you installed other OpenCV dependencies:
-Before compiling OpenCV, make sure to check the following CMake flags:
2. Notice that I set the flag WITH_CUDA to OFF. Typically, if you have a NVIDIA graphics card with CUDA support, most of you will want this to be ON instead (assuming that you have installed CUDA). However, when compiling OpenCV I've received a CUDA related compile error; I've figured out that it was due to the WITH_CUDA flag. I've used the flag with Python 3.5 and it works, hence my hypothesis is that it won't work with 2.7. But, feel free to experiment with the WITH_CUDA=ON. If there's an error, then you will have to recompile with WITH_CUDA=OFF.
3. Assuming that you have properly installed OpenCV in a virtual environment, it's time to install PyQt4. Now, to answer your question: It's 2017, why are we setting up everything in Python 2.7?
Apparently, on my machine PyQt4 does not like Python 3.5. After making the switch to 2.7, everything is smooth sailing. It seems that the PyQt5 version works well with 3.5. But, you can try to install PyQt4 with 3.5 if you're so adamant on using that version, AT YOUR OWN RISK of course.
Let's continue with PyQt4, shall we? I would like to credit the following tutorial to this video: https://www.youtube.com/watch?v=6NDVP5inphM&index=5&list=PLa-uxtJ_4raY8jnQv2na-ydm1iIlWKKA-
4. First, lets update our package list and install the necessary dependencies for PyQt4:
5. Lets go to the virtualenv 'bin' directory, activate our container then create a new folder called 'build':
6. WARNING: Ensure that you are inside the virtual environment by looking at the appended container's name in your command line. For example, if your container's name is cv then i.e. (cv)kennyallau: ~$ .
Now, we need to get the PyQt4 package:
7. We also need the SIP package:
8. Now let's extract the packages, first the PyQt4:
9. Now, it's time to compile the Make files for both packages. We will start with SIP since the installation of PyQt4 requires you to install SIP beforehand. Let's go to the extracted SIP package directory:
10. After that, we go to the PyQt4 directory and do the same thing:
11. There will be a license permission prompt so put 'YES' to accept license.
12. After that, we need to compile. The compiling process will take quite a while:
13. After it's done compiling, time to install:
14. You're almost done. Now let's try it out:
15. No errors? Congratulations, you're done! You can also use this tutorial together with Anaconda except you have to use conda environment and also omit step 5. Thank you for reading this post. Happy coding!