Hi there, I have an OAK-D Lite camera and I want to use it to detect drones and their distance relative to the camera. I am using a YOLOv6-N model as it is lightweight and seems to be a pretty popular choice amongst OAK-D Lite projects. The script that I am running to detect drone's and their distance relative to the camera is:

from depthai_sdk
import OakCamera, ArgsParser
import argparse

# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-conf", "--config", help="Trained YOLO json config path", default='models/v1/16shaves/droneDetection_v1.json', type=str)
args = ArgsParser.parseArgs(parser)

with OakCamera(args=args) as oak:
    color = oak.create_camera('color')
    nn = oak.create_nn(args['config'], color, nn_type='yolo', spatial=True)
    oak.visualize(nn, fps=True)
    oak.start(blocking=True)

This code outputs the following, the bottom-right of the bounding box lies the coveted Z-parameter - "0.9m":
(sorry for the low-res)

I want to extract the Z-parameter into a variable, and I am very unsure of how to do so. I've tried using many different examples from the documentation and other sources, but to no avail.

Any help would be appreciated!

  • jakaskerl replied to this.
  • Nevermind. This script works:

    from depthai_sdk import OakCamera, ArgsParser
    import argparse
    
    latest_z_value = None  # Store the most recent Z distance
    
    def process_detections(packet):
        global latest_z_value
    
        if packet is None or not hasattr(packet, 'detections'):
            print("No detections found.")
            return  
    
        for det in packet.detections:
            if hasattr(det, 'img_detection') and hasattr(det.img_detection, 'spatialCoordinates'):
                latest_z_value = det.img_detection.spatialCoordinates.z / 1000
                print(f"Drone detected {latest_z_value:.2f}m away.")
            else:
                print("No spatial data available.")
    
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("-conf", "--config", help="Trained YOLO json config path", default='models/v1/16shaves/droneDetection_v1.json', type=str)
        args = ArgsParser.parseArgs(parser)
    
        with OakCamera(args=args) as oak:
            color = oak.create_camera('color')
            nn = oak.create_nn(args['config'], color, nn_type='yolo', spatial=True)
            nn.config_nn(resize_mode='stretch')
            
            oak.callback(nn, process_detections)
            oak.visualize(nn, fps=True)
            oak.start(blocking=True)
    
    if __name__ == "__main__":
        main()

    I tried using the callback function example found in the link and that doesn't work with my model. I tried switching out DetectionPacket for DepthPacket and still no bounding boxes were drawn.

    In my script, where is that Z-parameter being stored and how can I store that in a variable for handling? Here is a better image of the output:

    Nevermind. This script works:

    from depthai_sdk import OakCamera, ArgsParser
    import argparse
    
    latest_z_value = None  # Store the most recent Z distance
    
    def process_detections(packet):
        global latest_z_value
    
        if packet is None or not hasattr(packet, 'detections'):
            print("No detections found.")
            return  
    
        for det in packet.detections:
            if hasattr(det, 'img_detection') and hasattr(det.img_detection, 'spatialCoordinates'):
                latest_z_value = det.img_detection.spatialCoordinates.z / 1000
                print(f"Drone detected {latest_z_value:.2f}m away.")
            else:
                print("No spatial data available.")
    
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("-conf", "--config", help="Trained YOLO json config path", default='models/v1/16shaves/droneDetection_v1.json', type=str)
        args = ArgsParser.parseArgs(parser)
    
        with OakCamera(args=args) as oak:
            color = oak.create_camera('color')
            nn = oak.create_nn(args['config'], color, nn_type='yolo', spatial=True)
            nn.config_nn(resize_mode='stretch')
            
            oak.callback(nn, process_detections)
            oak.visualize(nn, fps=True)
            oak.start(blocking=True)
    
    if __name__ == "__main__":
        main()