Example Python Script - Face Detection
This introduction is written to show you a basic face detection script running on the OAK-D-Lite. In the first code snippet you find the required libraries and the necessary preprocessing steps.
import numpy as np
import cv2
import depthai as dai
import blobconverter
import time
NN_WIDTH, NN_HEIGHT = 672, 384
labelMap = ["background", "face"]
The parameters NN_WIDTH and NN_HEIGHT describe the format of the camera output and thus the input of the face detection model. The parameter labelMap contains the labels for the model.
These two lines must be customized for each model. In this case the used model “face-detection-adas-0001” expects images of size 672x384 pixels as input and the labels should be 0 for background and 1 for face. The Background must always be present at this position.
Any action from DepthAI, whether it’s a neural inference or color camera output, require a pipeline to be defined, including nodes and connections corresponding to our needs. The source nodes we add are the color camera node and the neural network detection node. To receive both camera frames and neural network inferenc results we add one output node for the camera and one for the neural network using the XLinkOut node from depthai. After that we name the streams.
pipline = dai.Pipeline()
cam_rgb = pipeline.create(dai.node.ColorCamera)
detection_nn = pipeline.create(dai.node.ModileDetectionnetwork)
xout_rgb = pipeline.create(dai.node.XLinkOut)
nn_out = pipeline.create(dai.node.XLinkOut)
xout_rgb.setStreamName("rgb")
nn_out.setStreamName("nn")
Next we set the properties of the camera. We define the preview size of the camera, set planar data and limit the Frames per second to 30. Next we set the camera resolution to 1080 pixels, specify which socket to use and set the color order to BGR (the default is BGR). The color order must be adaptet to the model.
cam_rgb.setPreviewSize(NN_WIDTH, NN_HEIGHT)
cam_rgb.setInterleaved(False)
cam_rgb.setFPS(30)
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
cam_rgb.setBoardSocket(dai.CameraBoardSocket.RGB)
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
After that we define the neural network that will make predictions based on the source frames. We set the confidence threshold to 0.5 at which to filter the detections and use to inference threads to run the neural network. Next we set our model with the blobconverter. To use another model from Open Model Zoo, just change the parameter name. Last we set the input queue to non-blocking. Default queue is blocking with size 5.
The last step is to connect the camera output to the neural network input and the detection output to the output node.
Now let’s define host side output queues to access the produced results and same placeholders.
Next we define a function to normalize the nn data with frame width/height to 0..1, and one function to display the frames with detections and their labels and the confindence of the detection.
In the following loop we fetch the latest results from the neural network node and the color camera. If we have results we receive the frame from the camera, calculate the FPS and receive the detections from the neural network. Last we display the frame until “q” is pressed.