This is my basic template and it runs video through the on-device NN. Feel free to modify it or use any part of it if you find it helpful:
from depthai_sdk import OakCamera
import depthai as dai
import datetime
from depthai_sdk.fps import FPSHandler
import shutil
import os
import getopt
import sys
short_options = "hvfp:"
long_options = ["help", "visualize", "fps", "play="]
playToggle = False
vis = False
showFPSToggle = False
play = ''
currentFPS = 0
fps = FPSHandler()
def print_help_and_exit():
print("-v, --visualize\n Make a visualizer widow")
print("-f, --fps\n Display FPS on console")
print("-p <file>, --play <file>\n Use video file as input")
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:], short_options, long_options)
except getopt.GetoptError:
print("Error: invalid argument")
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print_help_and_exit()
elif opt in ("-v", "--visualize"):
vis = True
elif opt in ("-f", "--fps"):
showFPSToggle = True
elif opt in ("-p", "--play"):
play = str(arg)
playToggle = True
#reset flags; depthai uses arguments in imported libraries which
#get confused if you add your own
sys.argv = [sys.argv[0]]
def prompt_Delete_Color(path):
while True:
response = input("Color.mp4 exists in: " + path +"\nDelete it? Choose NO to rename (yes/no): ").strip().lower()
if response in ("yes", "y"):
return True
elif response in ("no", "n"):
return False
else:
print("Invalid input. Please enter 'yes' or 'no'.")
# This function copies and renames the video to be played to color.mp4
# because depthai won't play it otherwise
def make_stream():
global newPath
filePath = play
dir_path, filename = os.path.split(filePath)
print(f"Video found in directory: {dir_path}")
print(f"Video named: {filename}")
if filename == "color.mp4":
newPath = filePath
return
color_video = "color.mp4"
newPath = os.path.join(dir_path, color_video)
if os.path.exists(newPath):
deleteExisting = prompt_Delete_Color(newPath)
#deleteExisting = True
if deleteExisting is True:
print(f"Deleting {newPath}")
os.remove(newPath)
else:
added_time = datetime.time()
formatted_date = added_time.strftime("%H%d%m%Y")
new_filename = "color_" + formatted_date + ".mp4"
renamed = os.path.join(dir_path, new_filename)
print(f"Renaming {newPath} to: {renamed}")
shutil.move(newPath, renamed)
#return
print(f"Copying {filePath} to: {newPath}")
shutil.copyfile(filePath, newPath)
if playToggle:
make_stream()
oak = OakCamera(replay = newPath)
else:
oak = OakCamera()
def show_FPS(placeholder):
fps.nextIter()
currentFPS = round(fps.fps(), 1)
print(f"\rFPS: {currentFPS}", end="")
with oak:
color = oak.create_camera('color', resolution=dai.ColorCameraProperties.SensorResolution.THE_1080_P)
yolo = oak.create_nn('yolov6n_coco_640x640', input=color)
if showFPSToggle: oak.callback(yolo, callback=show_FPS)
if vis: oak.visualize(yolo, fps=True, scale=2/3)
oak.start(blocking=True)