OpenCV in ScriptTOP, problems with module paths

Hi everyone, I’m finaly diving into the OpenCV world, and I’m stuck with some problems using the ScriptTOP and examples from OpenCV. For a good start, I followed the tutorial by @elburz (https://www.youtube.com/watch?v=1Uw2PWTR_XM), then tried to implement other examples. Here is where I’m stuck:

1 - Shi-Tomasi Corner Detector & Good Features to Track
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_shi_tomasi/py_shi_tomasi.html

For this one I’m stuck with an error saying “Cannot import name ft2font from matplotlib”. I installed Matplotlib using pip, and set the path to my install “Python37-32/Lib/site-packages/” in the “Python 64-bit module path” parameter in Touchdesigner. I tried a lot of solutions from the web but I can’t figure the right thing to do… Any idea? (I tried upgrade, reinstalling and so on…)

2- Face Detection using Haar Cascades
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html#face-detection

For this one I’m stuck with “error: (-215:Assertion failed) scaleFactor > 1 && _image.depth() == CV_8U in function ‘cv::CascadeClassifierImpl::detectMultiScale’” or “error: (-215:Assertion failed) !empty() in function ‘cv::CascadeClassifier::detectMultiScale’” (dependingon the source, photo or video from webcam)

I’m setting the path to “haarcascade_frontalface_default.xml” and “haarcascade_eye.xml” (which are included in Touchdesigner/OpenCV) with this:

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + ‘haarcascade_frontalface_default.xml’)
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + ‘haarcascade_eye.xml’)

It seems to be ok, but is the best way to point at these file in a script top? Any idea on why I’m getting these errors?

Sorry for the long post! And thank you in advance if you have any idea about this :slight_smile: I also included the two .toe files, simply plug any photo or video at the begining.

tuto-openCV-3-shitomasi.toe (1.2 MB)
tuto-openCV-4-faces.toe (1.2 MB)

Hi Kefon! Did you check out the other YouTube video we have about Script TOP? I mention in that about the image.depth() == CV_8U error in that one. I can’t remember which of these 2 it was in but they’re both good follow ups on using Script TOP with openCV in TouchDesigner:

1 Like

Hey @elburz! Thanks a lot for the quick answer, it’s really good to have an answer directly from you! Thanks to your magic trick (in the QR code tutorial), I managed to get the “Face Detection using Haar Cascades” working perfectly!

Screenshot_1

For those who encounter the same problem, the key to avoid this error is to convert the pixel format to give the function the right type of data it’s waiting for. Here, we are converting pixels value that goes from 0 to 1 as float (touchdesigner style) to value that goes from 0 to 255 as int8 (OpenCV style). The magic trick is a simple line of code (img is your input texture):

gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
gray = (gray * 255).astype(np.uint8)

And for the my matplotlib error in Shi-Tomasi Corner Detector & Good Features to Track, I discovered (to late) that this lib is only used for display at the end, not for detection, so I just removed the import line for this lib and it’s working like charm! (I will deal with this lib later if needed…)

So thanks again @elburz for the help on this one and thanks for your useful tutorials!

1 Like

Great to hear and my pleasure :slight_smile: Glad you got it up and running!

Hey again,
Just a little update for my error saying “Cannot import name ft2font from matplotlib”. I also encountered the same type of error trying to use Dlib, so I had to dive a little more to understand once and for all what was wrong. Well, the solution was pretty easy:

  • be sure to install python 3.7, and check the box to add PATH
  • update pip
  • install the modules you need (matplotlib, dlib and so on…) using pip
  • set your path to Python\Python37\Lib\site-packages in the “Python 64-bit module path” parameter in Touchdesigner

And boom! Everything is working.
This is pretty basic, but I had to spend some time finding the answer, so maybe this will help others :wink:

I’m using:

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + ‘haarcascade_frontalface_default.xml’)

but I’m getting:

AttributeError: module ‘cv2’ has no attribute ‘data’

My understanding was that TD has the full opencv package and would include the data (which includes the cascades). Any ideas about what could be causing this error? Thanks!

Still have the issue but my workaround was to just point to my own data folder (using the built-in project.folder var) with cascades in the project folder.