Skip to content

Instantly share code, notes, and snippets.

@MKaptein
Created April 4, 2022 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MKaptein/dd56a0a222d4f7d6829cb02d2185279e to your computer and use it in GitHub Desktop.
Save MKaptein/dd56a0a222d4f7d6829cb02d2185279e to your computer and use it in GitHub Desktop.
## Imports:
from PIL import Image
import sclblonnx as so
import numpy as np
## Creating the image difference ONNX graph:
# Start with the empty graph:
g = so.empty_graph()
# Create the constant node encoding the empty image and add it to the graph:
# Note the type encoding as np.int64.
example_image = np.array(Image.open("images/frame_01.jpg"), dtype=np.int32)
# Add the first input (note, same shape):
g = so.add_input(g, 't0', "INT32", example_image.shape)
g = so.add_input(g, 't1', "INT32", example_image.shape)
g = so.add_input(g, 'cutoff', "INT32", [1])
# Add the Subtract, Absolute, ReduceSum, and Less nodes
# Node how the names again enforce the topology of the graph
n1 = so.node("Sub", inputs=['t0', 't1'], outputs=['sub'])
n2 = so.node("Abs", inputs=['sub'], outputs=['abs'])
n3 = so.node("ReduceSum", inputs=['abs'], outputs=['sum'], keepdims=0) # Note the keepdims additional parameter.
g = so.add_nodes(g, [n1, n2, n3])
n4 = so.node("Less", inputs=['cutoff', 'sum'], outputs=['change'])
g = so.add_node(g, n4)
# Add output:
g = so.add_output(g, "change", "BOOL", [1])
so.display(g)
## Example code for running the graph agains multiple images:
cutoff = 1280*720*3*2.6 # Cutoff where each of the color channels/pixels can be 2.6 off.
# Loop through the images
for i in range(2, 31):
t0_image = np.array(Image.open("images/frame_"+str(i-1).zfill(2)+".JPG"), dtype=np.int32)
t1_image = np.array(Image.open("images/frame_"+str(i).zfill(2)+".JPG"), dtype=np.int32)
example = {"t0": t0_image.astype(np.int32),
"t1": t1_image.astype(np.int32),
"cutoff" : np.array([cutoff]).astype(np.int32)}
result = so.run(g,
inputs=example,
outputs=['change'])
print("Comparing frame "+str(i-1).zfill(2)+" to "+str(i).zfill(2)+". Change: "+str(result[0])+".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment