Custom Opencv python script on frames from VideoDeviceInTOP

I’ve created python script which takes image frames from a webcam, runs some opencv machine learning operations on each image and prints out a number for each frame. I would now like to use this script in TouchDesigner.

I’m trying to solve a few problems:

  1. How do I access images from the VideoDeviceIn TOP in a python script text DAT?
  2. How do I output variables from a python script text DAT so that they can be accessed in other scripts or operators?

Here is an abbreviate version of my script

import cv2

# need to change this so it takes frame from VideoDeviceIn TOP
cap = cv2.VideoCapture(0)
_ret, frame = cap.read()

# Very abbreviate. Here I run a bunch of custom OpenCV operations which result in an integer prediction
digit = resultOfOpenCVOperations

# I would like to pass the digit as ouput to be accessed by other operators
print(digit)

New in the latest stable build you can now access the contents of a TOP as a numpy array, instead of needing to write that to file.

docs.derivative.ca/TOP_Class

In the past I’ve just written to and read from file for these kinds of operations, but this approach should have some potential for you.

In terms of sending variables to other scripts you have a number of options - you can always write them to a table, or a constant CHOP. You might also look into storage as a way to solve this problem as well. Probably the more useful approach here would be to author an extension or python module to keep things running smoothly.

matthewragan.com/2016/07/12/pyt … hdesigner/

matthewragan.com/2016/07/14/pyt … hdesigner/

hope that helps!

Thanks for the quick response!
I tried accessing numpyArray using the op() method

myarray = op("myVideoDeviceIn1").numpyArray()

It returns with
“td.AttributeError: ‘td.videodeviceinTOP’ object has no attribute ‘numpyArray’.”

My build is 2018.24410 non-commercial license.

Should I some how be attaching the script directly to the TOP?

Wait, I see its available in 2018.26750. I’ll go ahead and update my software version!
Thanks again for the help!