Using Openvino's OpenCV build on Anaconda environment - python

I have recently installed the latest OpenVINO release (2018 R5 0.1) for Windows 10 which, if I understood correctly, comes with a fully built OpenCV. Many tutorials show the use of that OpenCV but I failed to make it work on my Anaconda environment (with Python 3.6).
Running the environement setup C:\Intel\cvsdk\bin\setupvars.bat script I get the following output:
Commande ECHO désactivée.
PYTHONPATH=C:\Intel\computer_vision_sdk_2018.5.456\python\python3.6;C:\Program Files\Python36;
[setupvars.bat] OpenVINO environment initialized
In my conda env, if I have no opencv package installed, I get the error:
ModuleNotFoundError: No module named cv2
And if I install one with conda install py-opencv (or opencv), I get this:
cv2.error: OpenCV(3.4.2) [...] Build OpenCV with Inference Engine to enable loading models from Model Optimizer
Installing with pip (pip install opencv-python) while on the anaconda environment also doesn't work:
cv2.error: OpenCV(4.0.0) [...] Build OpenCV with Inference Engine to enable loading models from Model Optimizer
For clarification, I have successfully built the opencv examples with CMake and can run the executables. Here is the output of one of their sample programs:
(OpenVino) C:\Intel\computer_vision_sdk_2018.5.456\opencv\build\Debug>openvino_sample_opencv_version.exe
Welcome to OpenCV 4.0.1-openvino
Clearly, that OpenCV is usable somehow, I just can't find how to use it in my conda environment from a python script.

you need to run
C:\Intel\cvsdk\bin\setupvars.bat
every time you activate the enviroment

For a clean installation of openVINO & anaconda should be enough to run the environment setup, as mentioned here, for Jupyter notebooks however could be better to run it explicitly at the beginning with:
PC
!C:\Intel\...\bin\setupvars.bat
Mac
!source /opt/intel/openvino/bin/setupvars.sh

I solved the problem by using windows command prompt rather than power shell, which is recommended by the official open-vino doc.

Append OpenVINO python path in the beginning of your python code as shown below:
import sys
sys.path.append("C:\Intel\computer_vision_sdk_<version_number>\python\python3.6")
For eg:
sys.path.append("C:\Intel\computer_vision_sdk_2018.5.456\python\python3.6")

Related

opencv: Could not load the Qt platform plugin "xcb" in "" even though it was found

I installed opencv-python on ubuntu wsl, after setting up a venv using virtualenvwrapper (I use wsl in visual studio code).
When running this code (which appears in one of the articles of this OCR guide:
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True)
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("I", image)
with this command on teminal:
python script.py --image temp.png
I get:
qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/ben123/.local/bin/.virtualenvs/ocr_venv/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb.
The interpreter in vscode is the correct one (the one of the venv), and when I type pip list I get
Package Version
------------- --------
numpy 1.22.2
opencv-python 4.5.5.62
pip 22.0.3
setuptools 60.6.0
wheel 0.37.1
Would appreciate any help at this point, since I spent so much time and didn't get nowhere.
Things I tried:
following this guide to install it. Gave the same error.
following an older guide from this site, was much more complicated and didn't work as well.
uninstalling opencv-python and installing opencv-python again/ opencv-python-contrib/ opencv-python-headless/ opencv-python-contrib-headless (only one of them at a time)
following this thread because it has similar problem
literally reset my wsl several times just to make sure I don't have multiple pythons/ opencv versions that mess this up.
tried installing (to a wsl venv) opencv directly with the official documentation
Tried to give up on wsl completely and install opencv using anaconda but even that didn't work.
Just delete cv2.imshow from your code. Your OS is without graphics and can't display image
I had the same error in a completely different context.
Found that the problem was a PyQt5 installation in my virtual environment.
Check if you have a PyQt in the path
/home/ben123/.local/bin/.virtualenvs/ocr_venv/lib/python3.8/site-packages/
if so, remove it
$ pip uninstall <PyQT package installed>
example:
$ pip uninstall PyQt5
Then reinstall opencv-python
$ pip uninstall opencv-python
$ pip install opencv-python
Hope that works!
To display graphical information about wsl, you should configure x11 related content.
eg: You can use MobaXterm for graphical display.
Uninstalling opencv and installing similar headless version worked for me.
$ pip install opencv-python-headless

Setting up OpenAI Gym on Windows 10

I'm trying to set up OpenAI's gym on Windows 10, so that I can do machine learning with Atari games.
On PyCharm I've successfully installed gym using Settings > Project Interpreter. But when I try to set up a breakout environment (or any other Atari game) using:
import gym
env = gym.make('BreakoutDeterministic-v4')
I get the following error:
ImportError: No module named 'atari_py'
...
HINT: you can install Atari dependencies by running 'pip install gym[atari]'
So I tried the command 'pip install gym[atari]' in the PyCharm terminal, and got the error:
Unable to execute 'make build -C atari_py/ale_interface -j 11'. HINT: are you sure `make` is installed?
error: [WinError 2] The system cannot find the file specified
I tried installing 'make' from http://gnuwin32.sourceforge.net/packages/make.htm, and tried some other methods, but this didn't solve the problem.
I've also tried 'pip install atari_py', which runs fine but also doesn't solve the problem.
Any ideas/help would be much appreciated. I'm new to Python and PyCharm, so I apologise if any of this was unclear. Please let me know if there's any more info I can provide to help.
Thanks in advance
The gym documentation https://github.com/openai/gym#installing-everything says that for using all the environments, you should have installed dependencies through pip install 'gym[all]'. Can you run this command preferably through the terminal and see if it works?
We currently support Linux and OS X running Python 2.7 or 3.5 -- 3.7.
Windows support is experimental - algorithmic, toy_text,
classic_control and atari should work on Windows (see next section for
installation instructions); nevertheless, proceed at your own risk.
https://github.com/openai/gym#supported-systems
I ended up using Windows subsystem for Linux to run OpenAI Gym with Atari on Windows 10.
I have written a complete guide here.
Works great!

Installing gdal with conda and setting environmental variables

I have been trying to get the gdal library work using Python 2.7 and Anaconda in Windows 8 environment.
Besides gdal, I have also installed libgdal (frankly, I don't really understand the difference between the two). I now seem to have gdal 2.1.0 and 2.0.2 as well as libgdal 2.1.0.
However, when I run my Py code, there is a gdal error:
'gdalwarp' is not recognized as an internal or external command,
operable program or batch file.
I have already set the GDAL_DATA environmental variable to point to
C:\Anaconda\pkgs\libgdal-2.1.0-vc9_0\Library\share\gdal
I have also added a path, although I am not entirely sure where this should point to:
C:\Anaconda\pkgs\libgdal-2.1.0-vc9_0\Library\bin
I have tried the same with gdal 2.0.2 without success. gdalwarp.exe does seem to exist under libgdal 2.1.0 and gdal 2.0.2.
Any ideas? Is there an issue with the installation or have I not set the environmental variables correctly?
FYI, I have tried various installation commands, notably:
conda install gdal
conda install -c conda-forge gdal
conda install -c anaconda gdal
Addendum: I have found a manual solution: I set the GDAL_DATA and PATH variables in the terminal (pointing to libgdal 2.1.0) before running the code...
However, there is still an issue when I run my Py code: it is supposed to convert a tiff file to shp with gdal_polygonize:
cmd = 'gdal_polygonize.py %s -f "ESRI Shapefile" %s'%(dst_tif, dst_shp)
There is no error but the shapefile is not created (which leads to an error later on in the code). Any ideas as to why gdal is still not working correctly?
I have tried pointing the env variables to osgeo:
set PATH=%PATH%;C:\Anaconda2\Lib\site-packages\osgeo\scripts
set GDAL_DATA=C:\Anaconda2\Lib\site-packages\osgeo\data\gdal
Gdalinfo works but the gdal_polygonize used in my Py code does not appear to work.
The key is the activation script which is (potentially) executed when activating the environment. Not every GDAL build for Conda contains this. In my experience recent Conda-Forge builds are really good.
With your requirements of py27 and GDAL 2.1 i can get it working by following these steps:
1) Create a new environment: conda create -n gdaltest python=2.7
2) Activate: activate gdaltest
3) Install GDAL: conda install gdal=2.1 -c conda-forge
4) Reactivate environment: deactivate + activate gdaltest
This forces the just installed activation script to be executed, this sets the environment variables.
If i start python and run os.system("gdalinfo"), i can see its picked up correctly. And running os.environ['GDAL_DATA'] confirms the path is set correctly.
You can view the (de)activation script yourself at:
C:\Miniconda3\envs\<env name>\etc\conda\activate.d\gdal-activate.bat
A few years ago this didn't work as well as it does today, so make sure you have a recent Conda version (4.3.x) etc.
The benefit of this method is, that when switching environments, your paths are also changed accordingly. A "hard coded" GDAL_DATA path could potentially cause some compatibility issues if you mix and match different GDAL versions (although normally is should work OK).

OpenCV WITH_FFMPEG: Unable to stop the stream: Inappropriate ioctl for device

I'm receiving the error "Unable to stop the stream: Inappropriate ioctl for device" when trying to open a video file with OpenCV 3.2 in Python 3.5 on Ubuntu 16.04
Before marking as a duplicate, please note that I've looked at a lot of posts and none of them solved the problem for me.
I've setup a virtual environment under conda. I know there's a way to install OpenCV with conda but I don't believe it includes ffmpeg or other cuda abilities I need included. I've built the source for OpenCV 3.2 with WITH_FFMPEG on and WITH_LIBV4L on, which I've read could also be a solution to my problem. I've also set all of the python3 cmake variables to point to the python instance in the virtual environment in question:
PYTHON3_LIBRARY /home/brett/anaconda3/envs/opencvtest/lib/
PYTHON3_LIBRARY_DEBUG
PYTHON3_NUMPY_INCLUDE_DIRS /home/brett/anaconda3/envs/opencvtest/lib/python3.5/site-packages/numpy/core/include
PYTHON3_PACKAGES_PATH /home/brett/anaconda3/envs/opencvtest/lib/python3.5/site-packages/
After generating the make configuration I compile inside the build directory with make -j8 then install with sudo make install. Everything runs successfully and in the install I see it declare that FFMPEG is ON. If I now start Python in the virtual environment I can import cv2, but I see the above error when I try to open a video file. I can capture video from the webcam without a problem. In python the command print(cv2.__file__) displays the directory:
/home/brett/anaconda3/envs/opencvtest/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
So it appears to be using the correct OpenCV library.
I've also tried installing via conda with the command:
conda install -c menpo opencv3 ffmpeg
The output was:
The following NEW packages will be INSTALLED:
ffmpeg: 3.1.3-0 menpo
After the install completed I still get the same error message.
What else do I need to do for Python to load a video file with the OpenCV version that I built under Conda?

Tensorflow installation with anaconda installation and run issue

I have been trying to install Tensorflow for a really long time now, but I never seem to make it work. I have tried to install Tensorflow via pip, virtual environment and anaconda so far. The installation process seem to run smoothly with all three methods. But as soon as I try to validate the installation by running "import tensorflow" I get the following error. I know it looks kind of chaotic, I wasn't sure how to pose the question.
By now, all help is appreciated
Thanks
As mentioned, you have to install python 3.5.X first
Secondly, I strongly recommend you to use anaconda. You should install anaconda 4.4.0 for python 3.6 version and 64-bit installer.
Then, you should run the following command
conda create -n tensorflow python=3.5
By the way, would you watch the tensorflow installation tutorial ?
TensorFlow versions 1.2 and later are compatible with Python 3.6. The error message points to the actual problem:
ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory
This implies two things:
You have installed the tensorflow-gpu package, which requires a CUDA-capable GPU and a working installation of CUDA and cuDNN.
TensorFlow cannot find cuDNN.
This answer explains how to fix your cuDNN installation.
Update:
I get a similar error but the message in the bottom is now:
"ImportError libnvidia-fatbinaryloader.so.384.47: cannot open shared object file: No such file or directory"
Also when I enter "which nvcc" it returns /usr/bin/nvcc

Categories