# Post-Processing

Post-processing is a critical phase in the inference process. It involves refining the raw outputs of the models to derive
meaningful insights and actionable data.

## Output Interpretation and Conversion

In the DepthAI library, we support a range of predefined models as well as custom ones. This section will guide you through
interpreting and parsing outputs from both types of models.

### Predefined Models

#### Object Detection

We provide decoding support for some predefined models withoutputs that can be easily parsed using the Python API. These models
provide a set of data for each detected object. This includes the following:

 * Bounding Box Coordinates (xmin, ymin, xmax, ymax): These define the rectangular area in which the detected object is located
   within the image. The coordinates are normalized, meaning they are expressed as values between 0 and 1 relative to the
   dimensions of the image.
 * Confidence Scores: Represent the model's certainty about the detection. A higher score indicates greater confidence in the
   accuracy of the detection.
 * Class Labels: Indicate the category of the detected object, as learned by the model during training.

Supported predefined models include:

 * MobileNet: can be used through the
   [MobileNetDetectionNetwork](https://docs.luxonis.com/software/depthai-components/nodes/mobilenet_detection_network.md#mobilenetdetectionnetwork)
   node.
 * Yolo: can be used through the
   [YoloDetectionNetwork](https://docs.luxonis.com/software/depthai-components/nodes/yolo_detection_network.md#yolodetectionnetwork)
   node. Available for export via [Luxonis Tools](https://tools.luxonis.com/). We support several versions of Yolo models, each
   tailored for different detection needs:
   * YoloV5
   * YoloV6 (R1, R2, R3, R4)
   * YoloV7
   * YoloV8
   * GoldYolo

For a more detailed understanding of model outputs and predefined model nodes, refer to the
[ImgDetections](https://docs.luxonis.com/software/depthai-components/messages/img_detections.md#imgdetections) documentation. An
example of parsing the data is as follows:

```python
import depthai as dai

# Assuming the pipeline and model setup are already done
# For details on pipeline creation, refer to DepthAI documentation

# Connect to the device and start the pipeline
with dai.Device(pipeline) as device:

    # Retrieve detections from the output queue
    qDet = device.getOutputQueue(name="network_node_name", maxSize=4, blocking=False)
    detections = qDet.get().detections

    # Parse each detection
    for detection in detections:
        xmin, ymin, xmax, ymax = detection.bbox
        confidence = detection.confidence
        class_id = detection.label
        # Further processing or utilizing...
```

### Custom Models

For custom models, outputs can still be retrieved using methods like getLayerFp16(layer_name) or getLayerInt8(layer_name), where
you need to provide the name of the specific layer. Given that these methods return a flattened output from the selected layer,
it's necessary to reshape the output for subsequent use. You can find an example in
[the EfficientDet experiment](https://github.com/luxonis/oak-examples/tree/master/gen2-efficientDet/main.py#L67). The following
code snippet briefly illustrates the process:

```python
import depthai as dai
import numpy as np

# Assuming the pipeline and model setup are already done
# For details on pipeline creation, refer to DepthAI documentation
output_shape=(1,1000) # replace with your own shape

# Connect to the device and start the pipeline
with dai.Device(pipeline) as device:

    # Define the queue
    qDet = device.getOutputQueue(name="network_node_name", maxSize=4, blocking=False)

    # Retrieve the list of outputs and reshape it to the desired dimensions
    outputs = in_nn.getLayerFp16('network_output_name')
    outputs = np.array(outputs).reshape(output_shape)

    # Further parsing, processing or utilizing...
```

## Practical Applications Showcase

Once the model's outputs are parsed, the resulting data can be employed in various meaningful ways:

 * Visual Feedback: Use the bounding box coordinates to draw rectangles around detected objects in real-time video streams. You
   can find the example in [our YOLO
   experiment](https://github.com/luxonis/oak-examples/tree/master/gen2-yolo/device-decoding/main_api.py#L116).
 * Data Analytics: The [people tracking experiment](https://github.com/luxonis/oak-examples/tree/master/gen2-people-tracker)
   illustrates the application of data for tracking, analyzing movement trends, and counting people. It can be useful in
   environments like retail spaces, public venues, or transport hubs, where understanding foot traffic patterns and density is
   essential for operational efficiency, safety, and customer experience optimization.
 * Anomaly Detection: [This experiment](https://github.com/luxonis/oak-examples/tree/master/gen2-anomaly-detection) showcases
   real-time anomaly detection capabilities. This is particularly useful in scenarios like quality control in manufacturing, where
   detecting deviations from the norm in products or processes is crucial.
