Install openCV in Blender 2.91 on Ubuntu 20.04 LTS

Trying to use cv2 for motion capture in Blender 2.91.
But I am not able to install the openCV package at all.

I cd to the Blender install folder, cd into 2.91/python/bin in there executing:

./python3.7m -m pip install -U --timeout 1000 opencv-python opencv-contrib-python imutils numpy dlib --user

(the timeout command is only used because my internet is very slow)

It always is exiting with:

ERROR: Command errored out with exit status 1: /opt/E_cycles_2.91/2.91/python/bin/python3.7m -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-hqo782aq/dlib_be71238b870248949ea70541cccb55e3/setup.py'"'"'; __file__='"'"'/tmp/pip-install-hqo782aq/dlib_be71238b870248949ea70541cccb55e3/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-1fzhs790/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/magdesign/.local/include/python3.7m/dlib Check the logs for full command output.

Already updated setuptools with:

./python3.7m -m pip install -U --timeout 1000 setuptools

Can someone explain how to install openCV within Blender or how to tell Blender to use Python from OS ?

Here is what I tried.

You are in this directory
blender\2.91\python\bin\python.exe

Type this to get some info
.\python.exe -m ensurepip
Requirement already satisfied: setuptools …
Requirement already satisfied: …

And then you can call pip like this
to get the pip help message

.\python.exe …\lib\site-packages\pip

you want the python.exe from this directory
to go to the pip folder and run it as module
(notice the dot in front of the python.exe
otherwise you might refer to the system python
instead if you also have it installed as well)

Then you can install python open cv as mentioned
in this tutorial, same thing but only that is installed
in Blender’s packages
https://www.youtube.com/watch?v=oXlwWbU8l2o
.\python.exe .\..\lib\site-packages\pip install opencv-contrib-python

and after install you should get this:
Successfully installed opencv-contrib-python-4.5.1.48
and the package installed here
blender\2.91\python\lib\site-packages\opencv_contrib_python-4.5.1.48.dist-info

And now you do a test, in the script editor window typing this code.

import cv2 as cv
img = cv.imread(r'D:\documents\graphics\test.jpg')
cv.imshow('test', img)

You should be able to open an image.

Thanks for your help.
Unfortunately I am on Ubuntu Linux and tried all tutorials and instructions I could find.
Its no problem to install openCV on the system python, even compiling from scratch.
But somehow not possible on Blenders python.

Also tried with Blenders ./pip and of course ./python3.7m

Thats why I am in hope to somehow link Blender to python of my OS, or I do not know…

I have not tried this on Linux, it is really weird that this technique doesn’t work. Though the entire technique looks like a hack.

There is another way to install pip packages right from inside Python.

Such as for example if you type this on the command prompt you would look at this:

>>> import pip
>>> pip.__path__
['D:\\programs\\graphics\\blender29\\2.91\\python\\lib\\site-packages\\pip']

Then you can install something:

>>> pip.main(['install', 'Pillow'])
Requirement already satisfied: Pillow in d:\programs\graphics\blender29\2.91\python\lib\site-packages (8.1.0)
0

Does this work?

I don’t have linux here, but maybe this helps:
./pip3 install openvc-python

Oh, I didn’t thought of this, it seems much better.
D:\programs\graphics\blender29\2.91\python\Scripts\pip3.exe

1 Like