Skip to content

Instantly share code, notes, and snippets.

@ibaiGorordo
Created March 1, 2022 04:44
Show Gist options
  • Select an option

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

Select an option

Save ibaiGorordo/e23bf57869ad48ada0701f76e4d307b6 to your computer and use it in GitHub Desktop.
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()",
"create(dai.node.VideoEncoder)" : "createVideoEncoder()",
"create(dai.node.SPIOut)" : "createSPIOut()",
"create(dai.node.SPIIn)" : "createSPIIn()",
"create(dai.node.ImageManip)" : "createImageManip()",
"create(dai.node.MonoCamera)" : "createMonoCamera()",
"create(dai.node.StereoDepth)" : "createStereoDepth()",
"create(dai.node.MobileNetDetectionNetwork)" : "createMobileNetDetectionNetwork()",
"create(dai.node.YoloDetectionNetwork)" : "createYoloDetectionNetwork()",
"create(dai.node.SystemLogger)" : "createSystemLogger()",
"create(dai.node.SpatialLocationCalculator)" : "createSpatialLocationCalculator()",
"create(dai.node.MobileNetSpatialDetectionNetwork)" : "createMobileNetSpatialDetectionNetwork()",
"create(dai.node.YoloSpatialDetectionNetwork)" : "createYoloSpatialDetectionNetwork()",
"create(dai.node.ObjectTracker)" : "createObjectTracker()",
"create(dai.node.EdgeDetector)" : "createEdgeDetector()",
"create(dai.node.FeatureTracker)" : "createFeatureTracker()",
"create(dai.node.AprilTag)" : "createAprilTag()"}
# Ref: https://stackoverflow.com/a/39110/13706271
def replace_create_call(file_path):
#Create temp file
fh, abs_path = mkstemp()
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:
new_pattern = pattern_replacement[old_pattern]
script = script.replace(old_pattern, new_pattern)
print(f"Replaced {old_pattern} with {new_pattern} in {file_path}")
new_file.write(script)
#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)
def has_create_call(file_path):
pattern = "pipeline.create("
with open(file_path, encoding="utf8") as f:
return True
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)
if has_create_call(file_path):
replace_create_call(file_path)
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment