Skip to content

Instantly share code, notes, and snippets.

@ibaiGorordo
Created March 2, 2022 14:37
Show Gist options
  • Select an option

  • Save ibaiGorordo/1e351a058e3e95972f83c9774b18a248 to your computer and use it in GitHub Desktop.

Select an option

Save ibaiGorordo/1e351a058e3e95972f83c9774b18a248 to your computer and use it in GitHub Desktop.
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)",
"createVideoEncoder()" : "create(dai.node.VideoEncoder)",
"createSPIOut()" : "create(dai.node.SPIOut)",
"createSPIIn()" : "create(dai.node.SPIIn)",
"createImageManip()" : "create(dai.node.ImageManip)",
"createMonoCamera()" : "create(dai.node.MonoCamera)",
"createStereoDepth()" : "create(dai.node.StereoDepth)",
"createMobileNetDetectionNetwork()" : "create(dai.node.MobileNetDetectionNetwork)",
"createYoloDetectionNetwork()" : "create(dai.node.YoloDetectionNetwork)",
"createSystemLogger()" : "create(dai.node.SystemLogger)",
"createSpatialLocationCalculator()" : "create(dai.node.SpatialLocationCalculator)",
"createMobileNetSpatialDetectionNetwork()" : "create(dai.node.MobileNetSpatialDetectionNetwork)",
"createYoloSpatialDetectionNetwork()" : "create(dai.node.YoloSpatialDetectionNetwork)",
"createObjectTracker()" : "create(dai.node.ObjectTracker)",
"createEdgeDetector()" : "create(dai.node.EdgeDetector)",
"createFeatureTracker()" : "create(dai.node.FeatureTracker)",
"createAprilTag()" : "create(dai.node.AprilTag)"}
# Ref: https://stackoverflow.com/a/39110/13706271
def replace_createNode_call(file_path):
#Create temp file
fh, abs_path = mkstemp()
contains_old_pattern = False
with fdopen(fh,'w',encoding="utf8") as new_file:
with open(file_path,encoding="utf8") as old_file:
# Read the old script
script = old_file.read()
for old_pattern in pattern_replacement.keys():
# Replace the pattern if any it contains any of the old create patterns
if old_pattern in script:
contains_old_pattern = True
new_pattern = pattern_replacement[old_pattern]
script = script.replace(old_pattern, new_pattern)
print(f"Replaced {old_pattern} with {new_pattern} in {file_path}")
# Only rewrite scripts that have the old pattern
if contains_old_pattern:
new_file.write(script)
if contains_old_pattern:
#Copy the file permissions from the old file to the new file
copymode(file_path, abs_path)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
if __name__ == '__main__':
rootdir = 'depthai-experiments'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".py"):
file_path = os.path.join(subdir, file)
replace_createNode_call(file_path)
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment