|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import sys |
| 5 | +import numpy as np |
| 6 | +import cv2 |
| 7 | +import depthai as dai |
| 8 | +SHAPE = 300 |
| 9 | + |
| 10 | +# Get argument first |
| 11 | +nnPath = str((Path(__file__).parent / Path('../models/normalize_openvino_2021.4_4shave.blob')).resolve().absolute()) |
| 12 | +if len(sys.argv) > 1: |
| 13 | + nnPath = sys.argv[1] |
| 14 | + |
| 15 | +if not Path(nnPath).exists(): |
| 16 | + import sys |
| 17 | + raise FileNotFoundError(f'Required file/s not found, please run "{sys.executable} install_requirements.py"') |
| 18 | + |
| 19 | +p = dai.Pipeline() |
| 20 | +p.setOpenVINOVersion(dai.OpenVINO.VERSION_2021_4) |
| 21 | + |
| 22 | +camRgb = p.createColorCamera() |
| 23 | +# Model expects values in FP16, as we have compiled it with `-ip FP16` |
| 24 | +camRgb.setFp16(True) |
| 25 | +camRgb.setInterleaved(False) |
| 26 | +camRgb.setPreviewSize(SHAPE, SHAPE) |
| 27 | + |
| 28 | +nn = p.createNeuralNetwork() |
| 29 | +nn.setBlobPath(nnPath) |
| 30 | +nn.setNumInferenceThreads(2) |
| 31 | + |
| 32 | +script = p.create(dai.node.Script) |
| 33 | +script.setScript(""" |
| 34 | +# Run script only once. We could also send these values from host. |
| 35 | +# Model formula: |
| 36 | +# output = (input - mean) / scale |
| 37 | +
|
| 38 | +# This configuration will subtract all frame values (pixels) by 127.5 |
| 39 | +# 0.0 .. 255.0 -> -127.5 .. 127.5 |
| 40 | +data = NNData(2) |
| 41 | +data.setLayer("mean", [127.5]) |
| 42 | +node.io['mean'].send(data) |
| 43 | +
|
| 44 | +# This configuration will divide all frame values (pixels) by 255.0 |
| 45 | +# -127.5 .. 127.5 -> -0.5 .. 0.5 |
| 46 | +data = NNData(2) |
| 47 | +data.setLayer("scale", [255.0]) |
| 48 | +node.io['scale'].send(data) |
| 49 | +""") |
| 50 | + |
| 51 | +# Re-use the initial values for multiplier/addend |
| 52 | +script.outputs['mean'].link(nn.inputs['mean']) |
| 53 | +nn.inputs['mean'].setWaitForMessage(False) |
| 54 | + |
| 55 | +script.outputs['scale'].link(nn.inputs['scale']) |
| 56 | +nn.inputs['scale'].setWaitForMessage(False) |
| 57 | +# Always wait for the new frame before starting inference |
| 58 | +camRgb.preview.link(nn.inputs['frame']) |
| 59 | + |
| 60 | +# Send normalized frame values to host |
| 61 | +nn_xout = p.createXLinkOut() |
| 62 | +nn_xout.setStreamName("nn") |
| 63 | +nn.out.link(nn_xout.input) |
| 64 | + |
| 65 | +# Pipeline is defined, now we can connect to the device |
| 66 | +with dai.Device(p) as device: |
| 67 | + qNn = device.getOutputQueue(name="nn", maxSize=4, blocking=False) |
| 68 | + shape = (3, SHAPE, SHAPE) |
| 69 | + while True: |
| 70 | + inNn = np.array(qNn.get().getData()) |
| 71 | + # Get back the frame. It's currently normalized to -0.5 - 0.5 |
| 72 | + frame = inNn.view(np.float16).reshape(shape).transpose(1, 2, 0) |
| 73 | + # To get original frame back (0-255), we add multiply all frame values (pixels) by 255 and then add 127.5 to them |
| 74 | + frame = (frame * 255.0 + 127.5).astype(np.uint8) |
| 75 | + # Show the initial frame |
| 76 | + cv2.imshow("Original frame", frame) |
| 77 | + |
| 78 | + if cv2.waitKey(1) == ord('q'): |
| 79 | + break |
0 commit comments