Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Custom Opencv python script on frames from VideoDeviceInTOP

Still a little green? Ask your beginner's questions here

Custom Opencv python script on frames from VideoDeviceInTOP

Postby newfieldslab » Thu Nov 08, 2018 5:32 pm

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
Code: Select all
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)
newfieldslab
 
Posts: 3
Joined: Wed Oct 10, 2018 10:11 am

Re: Custom Opencv python script on frames from VideoDeviceIn

Postby raganmd » Thu Nov 08, 2018 5:38 pm

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.

numpyArray(delayed=False, writable=False)→ numpy.array:

Returns the TOP image as a Python NumPy array. Note that since NumPy arrays are referenced by line first, pixels are addressed as [h, w]. Currently data will always be in floating point, regardless of what the texture data format is on the GPU.

delayed - (Keyword, Optional) If set to True, the result may not be immediately available, waiting for the result to be downloaded from the GPU. This is useful to avoid long stalls that occur if immediately asking for the result. None will be returned if the result isn't ready yet. Call numpyArray() again, ideally on the next frame or later, to get the result.
writable - (Keyword, Optional) If set to True, the memory in the numpy array will be allocated in such a way that writes to it arn't slow. By default the memory the numpy array holds can be allocated in such a way that is very slow to write to. Note that in either case, writing to the numpy array will *not* change the data in the TOP.

https://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.

https://matthewragan.com/2016/07/12/pyt ... hdesigner/

https://matthewragan.com/2016/07/14/pyt ... hdesigner/

hope that helps!
raganmd
 
Posts: 1322
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: Custom Opencv python script on frames from VideoDeviceIn

Postby newfieldslab » Thu Nov 08, 2018 6:19 pm

Thanks for the quick response!
I tried accessing numpyArray using the op() method
Code: Select all
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?
newfieldslab
 
Posts: 3
Joined: Wed Oct 10, 2018 10:11 am

Re: Custom Opencv python script on frames from VideoDeviceIn

Postby newfieldslab » Thu Nov 08, 2018 6:21 pm

Wait, I see its available in 2018.26750. I'll go ahead and update my software version!
Thanks again for the help!
newfieldslab
 
Posts: 3
Joined: Wed Oct 10, 2018 10:11 am


Return to Beginners

Who is online

Users browsing this forum: Google [Bot] and 8 guests