Casting NN subtraction
This example demonstrates how to perform frame subtraction using a NeuralNetwork and the Cast node.Demo
![](/static/images/software/depthai/examples/cast_diff.gif)
Setup
Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the scriptCommand Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
Source code
Python
C++
PythonGitHub
1import cv2
2import depthai as dai
3from pathlib import Path
4
5SHAPE = 720
6
7p = dai.Pipeline()
8
9camRgb = p.create(dai.node.ColorCamera)
10nn = p.create(dai.node.NeuralNetwork)
11script = p.create(dai.node.Script)
12rgbXout = p.create(dai.node.XLinkOut)
13cast = p.create(dai.node.Cast)
14castXout = p.create(dai.node.XLinkOut)
15
16camRgb.setVideoSize(SHAPE, SHAPE)
17camRgb.setPreviewSize(SHAPE, SHAPE)
18camRgb.setInterleaved(False)
19
20nnBlobPath = (Path(__file__).parent / Path('../models/diff_openvino_2022.1_6shave.blob')).resolve().absolute()
21nn.setBlobPath(nnBlobPath)
22
23script.setScript("""
24old = node.io['in'].get()
25while True:
26 frame = node.io['in'].get()
27 node.io['img1'].send(old)
28 node.io['img2'].send(frame)
29 old = frame
30""")
31
32rgbXout.setStreamName("rgb")
33castXout.setStreamName("cast")
34cast.setOutputFrameType(dai.RawImgFrame.Type.GRAY8)
35
36# Linking
37camRgb.preview.link(script.inputs['in'])
38script.outputs['img1'].link(nn.inputs['img1'])
39script.outputs['img2'].link(nn.inputs['img2'])
40camRgb.video.link(rgbXout.input)
41nn.out.link(cast.input)
42cast.output.link(castXout.input)
43
44# Pipeline is defined, now we can connect to the device
45with dai.Device(p) as device:
46 qCam = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
47 qCast = device.getOutputQueue(name="cast", maxSize=4, blocking=False)
48
49
50 while True:
51 colorFrame = qCam.get()
52 assert isinstance(colorFrame, dai.ImgFrame)
53 cv2.imshow("Color", colorFrame.getCvFrame())
54
55 inCast = qCast.get()
56 assert isinstance(inCast, dai.ImgFrame)
57 cv2.imshow("Diff", inCast.getCvFrame())
58
59 if cv2.waitKey(1) == ord('q'):
60 break
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.