Accessing split NumPy arrays in ScriptTop

Hi! I have been diving into the world of script ops, and I am having so much fun learning how to code with python!

I am currently working on a lil project that I view as “low-tech Computer Vision”. My goal is to slice a video feed in a grid, and script operations on each of these ‘grid cells’. I have a created a prototype using a bunch of Crop TOPs, but I am now trying to using a Script TOP and NumPy instead because it’ll be easier to modify the grid size (among a few other reasons)… Here is my current file: coloranalyse_v2.37.toe (14.1 KB).

From the TD Numpy Documentation (NumPy - Derivative):
When using Script TOPs, use the .copyNumpyArray() method to write a NumPy array back as a texture.

# copy the NumPy array into the texture
scriptOp.copyNumpyArray(npArray)

This is my current ScriptTop:


My problem is that the for loop does not seem to iterate through the rows and only displays the last index of the array. i also want to copy these cells as textures on other Tops to be able to perform different operations on them.

  1. Is there something wrong with my for loop?
  2. Is there a way to use the .copyNumpyArray() method on other TOPs than the ScriptOP? op('constant1').copyNumpyArray(currentCell[0]) does not work.

Thank you in advance for your help!

Hi @hd.maxi

copyNumpyArray takes a NumPy array as input, and fills the TOP with the given image hence calling this in a loop will keep overwriting the TOP with the last call “sticking”.

What you would want to do is first split the image into rows, then loop over each row and split it into columns. Now loop over each part of the row and do whatever you need to do before first reassembling each row out of the columns and finally reassemble the whole image from all the rows.

The script here should do that:

# me - this DAT
# scriptOp - the OP which is cooking

import numpy
import random

# press 'Setup Parameters' in the OP to call this function to re-create the parameters.
def onSetupParameters(scriptOp):
	page = scriptOp.appendCustomPage('Custom')
	p = page.appendPulse('Pulse', label='Pulse')
	return

# called whenever custom pulse parameter is pushed
def onPulse(par):		
	return


def onCook(scriptOp):
	
	# read the input as a numpy Array
	inputArray = scriptOp.inputs[0].numpyArray()
	
	# split the numpy array into rows
	splitRows = numpy.split(inputArray, 4, axis=0)

	# loop through each row
	for i, row in enumerate(splitRows):
		# split each row into columns
		splitCols = numpy.split(row, 4, axis=1)
		# loop through each column
		for j, col in enumerate(splitCols):
			# set red to a random value
			randomRed = random.random()
			col[:,:,0] = randomRed
			
		# reassamble each row
		splitRows[i] = numpy.concatenate(splitCols, axis=1)
	# reassamble the whole image
	newArray = numpy.concatenate(splitRows)

	# set the TOP texture to the numpy array
	scriptOp.copyNumpyArray(newArray)
		

	return

.copyNumpyArray is only supported by the Script TOP.

cheers
Markus

1 Like

Hey Markus!

This is exactly what I needed to progress with this project. Thank you it works beautifully!