Skip to content

Instantly share code, notes, and snippets.

View ibaiGorordo's full-sized avatar

Ibai Gorordo ibaiGorordo

View GitHub Profile
@ibaiGorordo
ibaiGorordo / depthai_stereo_from_host_images.py
Last active August 5, 2022 04:04
Perform stereo depth estimation using the depthai API on images from the host computer in Python.
import depthai as dai
import cv2
import numpy as np
from imread_from_url import imread_from_url
def add_host_depth(pipeline):
monoLeftNode = pipeline.create(dai.node.XLinkIn)
monoRightNode = pipeline.create(dai.node.XLinkIn)
@ibaiGorordo
ibaiGorordo / depthai-android.Dockerfile
Last active July 7, 2022 12:15
Dockerfile for building the depthai-core library for Android
#
# Run in the terminal
# - Build:
# ```
# docker build . -t depthai-builder -f depthai-android.Dockerfile
# ```
# - Create and Copy the prebuilt libraries into the host computer
# ```
@ibaiGorordo
ibaiGorordo / replace_onnx_input.py
Last active April 3, 2022 02:43
Python script for replacing a combined input into two inputs and adding a concatenate node at the beginning using ONNX graph surgeon
import onnx_graphsurgeon as gs
import numpy as np
import onnx
@gs.Graph.register()
def replace_input(self, new_inputs, old_input_name):
tensors = self.tensors()
# Create concat node to combine the new inputs
@ibaiGorordo
ibaiGorordo / onnx_create_concat.py
Created April 2, 2022 07:38
Python script for creating a model that concatenates two inputs along the channel axis using ONNX graph surgeon
import onnx_graphsurgeon as gs
import numpy as np
import onnx
left_rect_img = gs.Variable(name="left_rect", dtype=np.float32, shape=(1, 3, 240, 320))
right_rect_img = gs.Variable(name="right_rect", dtype=np.float32, shape=(1, 3, 240, 320))
concat_img = gs.Variable(name="concat_img", dtype=np.float32, shape=(1, 6, 240, 320))
concat_node = gs.Node(op="Concat", attrs={"axis": 1}, inputs=[left_rect_img, right_rect_img], outputs=[concat_img])
graph = gs.Graph(nodes=[concat_node], inputs=[left_rect_img, right_rect_img], outputs=[concat_img])
@ibaiGorordo
ibaiGorordo / split_onnx_model.py
Created April 2, 2022 02:29
Python script for splitting an ONNX model (HITNET Sceneflow in this case) into two parts
import onnx
import numpy as np
import onnx_graphsurgeon as gs
def split_model(onnx_model, input_names, output_names):
graph = gs.import_onnx(onnx_model)
tensors = graph.tensors()
graph.inputs = [tensors[input_name] for input_name in input_names]
graph.outputs = [tensors[output_name] for output_name in output_names]
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove
import os
import re
def has_old_style(file_path):
pattern = "CODE="
@ibaiGorordo
ibaiGorordo / replace_depthai_createNode_calls.py
Created March 2, 2022 14:37
Replace the depthai pipeline.createNode() calls with create([node])
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove
import os
# Ref: https://github.com/luxonis/depthai-python/blob/main/src/pipeline/PipelineBindings.cpp
pattern_replacement = { "createXLinkIn()" : "create(dai.node.XLinkIn)",
"createXLinkOut()" : "create(dai.node.XLinkOut)",
"createNeuralNetwork()" : "create(dai.node.NeuralNetwork)",
"createColorCamera()" : "create(dai.node.ColorCamera)",
@ibaiGorordo
ibaiGorordo / replace_depthai_pipeline_create.py
Created March 1, 2022 04:44
Replaces the old pipeline.create with the new create functions.
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove
import os
# Ref: https://github.com/luxonis/depthai-python/blob/main/src/pipeline/PipelineBindings.cpp
pattern_replacement = { "create(dai.node.XLinkIn)" : "createXLinkIn()",
"create(dai.node.XLinkOut)" : "createXLinkOut()",
"create(dai.node.NeuralNetwork)" : "createNeuralNetwork()",
"create(dai.node.ColorCamera)" : "createColorCamera()",
images = np.zeros((1, 3, 360, 640), dtype=np.float32)
masks = np.ones((1, 1, 360, 640), dtype=np.float32)
torch.onnx.export(nnet.model,
(images, masks),
"model_float32.onnx",
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names = ['input_rgb','input_mask'],
@ibaiGorordo
ibaiGorordo / MediapipeModelRawOutputCheckExample.py
Created November 7, 2021 12:45
Python script to analyze the what the model inference is doing in Mediapipe.
# Referenes:
# - Mediapipe face detection models: https://github.com/google/mediapipe/tree/master/mediapipe/modules/face_detection
import os
import urllib
import mediapipe as mp
from mediapipe.python import solution_base
import cv2
import numpy as np