KMeans Clustering for Dominant Color

Worked on a KMeans Clustering approach to find dominant color in an image. The module here uses a few external libraries that need some TLC to work correctly - so make sure your Python house is in order. Bonus feature, it runs the analysis process in another thread so the image analysis doesn’t block touch

A little hard to setup, for me, but very useful! Thank you!

Nice, thank you for posting this, Matthew! A side goal of mine is to port this logic to a gpu based library so that we can start to do this real time :sunglasses:

i have trouble making the thing work, are you guys still able to use it with last versions of python, td and dependencies ? sklearn is installed properly as said by the test results after install, still td returns me with strange issue about it being badly installed

what sort of issues do you see returned?

I generally test in three tiers:

In TD it’s important to do a sys.path.insert(0,path/to/my/site-packages) then import your modules (NOTE: this has to be done before any other import statements are executed in your TOE file, so you need to manage this carefully if you have lots of executeDAT’s firing code that uses libraries at start.)

I think when you add your external python path to the preferences field, it actually appends it to the sys.path, if you print the paths you’ll see it at the end in my experience.

This usually makes the most sense because you want to default to what comes with TD and fall back to other libraries as you need them… but I’ve personally had tons of issues with more advanced libraries that need some special version of numpy (+MKL) for example.

seems like my sys path is correct, as it includes sklearn and my site package folder, i used the text DAT method provided by raganmd and printed sys.path to check, its correct !

and the errors returned are those :

python >>>
Traceback (most recent call last):
File “</base1/base_dominant_color/domColorEXT:op(’/base1/base_dominant_color/domColorEXT’).run()>”, line 1
td.Error: File “C:/Program Files/Python37/Lib/site-packages\”, line 44
from ._check_build import check_build # noqa
r = previousimport(*args, **kw)
ImportError: No module named ‘sklearn.__check_build._check_build’

During handling of the above exception, another exception occurred:

File “/base1/base_dominant_color/domColorEXT”, line 12
r = previousimport(*args, **kw)
r = previousimport(*args, **kw)
File “C:/Program Files/Python37/Lib/site-packages\”, line 63
from . import __check_build
r = previousimport(*args, **kw)
r = previousimport(*args, **kw)
File “C:/Program Files/Python37/Lib/site-packages\”, line 46
File “C:/Program Files/Python37/Lib/site-packages\”, line 41, in raise_build_error
%s""" % (e, local_dir, ‘’.join(dir_content).strip(), msg))
ImportError: No module named ‘sklearn.__check_build._check_build’

Contents of C:/Program Files/Python37/Lib/site-packages\sklearn_check_build: check_build.cp37-win_amd64.pyd__init

It seems that scikit-learn has not been built correctly.

If you have installed scikit-learn from source, please do not forget
to build the package before using it: run python install or
make in the source directory.

If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
Results of run operation resulted in exception. <type:textDAT path:/base1/base_dominant_color/domColorEXT> <class ‘td.textDAT’>
python >>>
Error retrieving extension for /base1/base_dominant_color: (Extension 1) AttributeError: module ‘/base1/base_dominant_color/domColorEXT’ has no attribute ‘DomColor’
python >>>

it seems like my build of scikit-learn isnt correct, despite i’ve checket it correctly with the built in test that commes with the librairy

i surely am missing something easy … anyway thanks for the help !

hey olivercouche - this is a some grumbly python silliness.

I’d guess that it’s a numpy version issue. You can check this by using

import numpy print(numpy.__version__)

Ideally you should see version 1.15.4.

If you don’t see this, than it’s likely that the distributed version of numpy with Touch is different.

To work around this, check the repo for the latest update. There there is now a new folder called dep. Inside you’ll see a script to do some python clean-up and organizing. You should right click and run as admin - it will pull the required libraries into a new folder called python in the dep directory.

In the TOE file there’s now an addition to the start-up script that will add your newly created local dep directory to the sys.path. This will add the new directory to the top of your list. Quit and restart touch, and see if you still get the error. Fingers crossed that all of that hoopla will get you up and running.

hey !
thanks a lot for your response !
my version is 1.15.4

gonna try the rest !

it seems that for some reason touchdesigner is not able to import cv2, numpy and sklearn and i cannot figure out why, every folder is appended in the sys path correctly

Hey there sir - so you shouldn’t need to add each folder individually, though that shouldn’t break anything. If you print out the contents of sys.path, what folders to you see, in what order?

i see :

E:/vjing/iterative works/appli vjing/exemples/touchdesigner-dominant-color-masterv2/dep/python/
C:\Program Files\Derivative\TouchDesigner099\bin
C:\Program Files\Derivative\TouchDesigner099\bin\
C:\Program Files\Derivative\TouchDesigner099\bin\DLLs
C:\Program Files\Derivative\TouchDesigner099\bin\lib
C:\Program Files\Derivative\TouchDesigner099\bin
C:\Program Files\Derivative\TouchDesigner099\bin\lib\site-packages
C:/Program Files/Derivative/TouchDesigner099/Config/Cmd

Oh bother.

Okay, some questions then.

Do you have python installed for windows - which version?

I’m guessing you’ve confirmed that all of the libraries are downloaded into dep/python?

yep, python is the latest build, 3.7.1
executing your .cmd installed all the libraries into dep/python

i just tried the last version from the repo on another computer on wich i fresh installed python and touchdesigner, and it returns me the same error :

python >>>
Traceback (most recent call last):
File “</base1/base_dominant_color/domColorEXT:op(’/base1/base_dominant_color/domColorEXT’).run()>”, line 1
td.Error: ImportError: numpy.core.multiarray failed to import
File “/base1/base_dominant_color/domColorEXT”, line 7
r = previousimport(*args, **kw)
File “C:\Program Files\Derivative\TouchDesigner099\bin\lib\site-packages\”, line 7
from . import cv2
r = previousimport(*args, **kw)
r = previousimport(*args, **kw)
ImportError: numpy.core.multiarray failed to import
Results of run operation resulted in exception. <type:textDAT path:/base1/base_dominant_color/domColorEXT> <class ‘td.textDAT’>
python >>>
Error retrieving extension for /base1/base_dominant_color: (Extension 1) AttributeError: module ‘/base1/base_dominant_color/domColorEXT’ has no attribute ‘DomColor’
python >>>

despite dep/python is in the sys.path, importing cv2, numpy, scipy, and sklearn returns errors

seems like touchdesigner is not able to use dep/python to import libraries !

importing those libraries in python trough command prompt is working

okay - I’d start downgrading your python install on Windows to 3.5+ - I’d guess that the 3.7 version that got pulled for your local python installation isn’t compatible with the 3.5+ version that touch uses.

Then delete that python directory and re-run that cmd script.

Working with outside libraries is be pretty painful at first - so I totally feel your frustration here.

i just installed python 3.5.4, deleted 3.7, re-runned the cmd script wich worked well, but still td isn’t able to import libraries, is there something to do td side to use python and libraries properlly ?

sorry for all those questions, and thanks a lot for your help !

hrm - well that’s some rough business.

The other option isn’t one that I’d recommend, but is another option.

You can install python packages to a specific target path inside of your touch installation. There’s a post on the process here: … 545ae50a0e

The approach here would be to take that cmd script and edit it’s target to be your lib\site-packages directory. That should look something like this:

pip install --target="C:\\Program Files\\Derivative\\TouchDesigner099\\bin\\lib\\site-packages" numpy --upgrade pip install --target="C:\\Program Files\\Derivative\\TouchDesigner099\\bin\\lib\\site-packages" scipy pip install --target="C:\\Program Files\\Derivative\\TouchDesigner099\\bin\\lib\\site-packages" cv2 pip install --target="C:\\Program Files\\Derivative\\TouchDesigner099\\bin\\lib\\site-packages" sklearn

The caution that I have to offer you is that there are some things in Touch that currently rely on the distributed versions of numpy, so if you’re using camschnapper (for example) this will likely break that bit. You can always delete the packages to recover. Or a clean installation of Touch will do the job.

Having been bitten by this approach, I can’t say that I’d recommend it - though I do know that it works… just a little more brute force than I typically like.

hello! this base is beautiful and exactly what i need to implement in a machine learning algorithm for VJ show file management. I am rather struggling with getting the Tox running without compilation errors.

I am seeing this:

wondering if you all have any idea

it seemed as though much of the python library management was intended to be automated with the base version 1.1.1? on first load after uninstalling python libraries for troubleshooting purposes the base seemed to open its own command prompt and attempted to pip install the libraries which seemed brilliant, but between trying various 3.5 and 3.8 python releases, i cant get past this bug. whenever i mess with that line of code (186) to make the error go away, a larger list of errors matching the issues above pops up

if i go to line 186 and remove " , async = true " from the second argument for saving the temp image file to the cache, this is what i see: