Skip to content

Example Workflows - Data analytics in Workflows

Below you can find example workflows you can use as inspiration to build your apps.

Workflow producing CSV

This example showcases how to export CSV file out of Workflow. Object detection results are processed with CSV Formatter block to produce aggregated results.

Workflow definition
{
    "version": "1.0",
    "inputs": [
        {
            "type": "WorkflowImage",
            "name": "image"
        },
        {
            "type": "WorkflowParameter",
            "name": "additional_column_value"
        }
    ],
    "steps": [
        {
            "type": "roboflow_core/roboflow_object_detection_model@v2",
            "name": "model",
            "images": "$inputs.image",
            "model_id": "yolov8n-640"
        },
        {
            "type": "roboflow_core/csv_formatter@v1",
            "name": "csv_formatter",
            "columns_data": {
                "predicted_classes": "$steps.model.predictions",
                "number_of_bounding_boxes": "$steps.model.predictions",
                "additional_column": "$inputs.additional_column_value"
            },
            "columns_operations": {
                "predicted_classes": [
                    {
                        "type": "DetectionsPropertyExtract",
                        "property_name": "class_name"
                    }
                ],
                "number_of_bounding_boxes": [
                    {
                        "type": "SequenceLength"
                    }
                ]
            }
        }
    ],
    "outputs": [
        {
            "type": "JsonField",
            "name": "csv",
            "coordinates_system": "own",
            "selector": "$steps.csv_formatter.csv_content"
        }
    ]
}

Aggregation of results over time

This example shows how to aggregate and analyse predictions using Workflows.

The key for data analytics in this example is Data Aggregator block which is fed with model predictions and perform the following aggregations on each 6 consecutive predictions:

  • taking classes names from bounding boxes, it outputs unique classes names, number of unique classes and number of bounding boxes for each class

  • taking the number of detected bounding boxes in each prediction, it outputs minimum, maximum and total number of bounding boxes per prediction in aggregated time window

Run on video to produce meaningful results

This workflow will not work using the docs preview. You must run it on video file. Copy the template into your Roboflow app, start inference server and use video preview to get the results.

Workflow definition
{
    "version": "1.0",
    "inputs": [
        {
            "type": "WorkflowImage",
            "name": "image"
        },
        {
            "type": "WorkflowParameter",
            "name": "model_id",
            "default_value": "yolov8n-640"
        }
    ],
    "steps": [
        {
            "type": "roboflow_core/roboflow_object_detection_model@v2",
            "name": "model",
            "images": "$inputs.image",
            "model_id": "$inputs.model_id"
        },
        {
            "type": "roboflow_core/data_aggregator@v1",
            "name": "data_aggregation",
            "data": {
                "predicted_classes": "$steps.model.predictions",
                "number_of_predictions": "$steps.model.predictions"
            },
            "data_operations": {
                "predicted_classes": [
                    {
                        "type": "DetectionsPropertyExtract",
                        "property_name": "class_name"
                    }
                ],
                "number_of_predictions": [
                    {
                        "type": "SequenceLength"
                    }
                ]
            },
            "aggregation_mode": {
                "predicted_classes": [
                    "distinct",
                    "count_distinct",
                    "values_counts"
                ],
                "number_of_predictions": [
                    "min",
                    "max",
                    "sum"
                ]
            },
            "interval": 6,
            "interval_unit": "runs"
        }
    ],
    "outputs": [
        {
            "type": "JsonField",
            "name": "aggregation_results",
            "selector": "$steps.data_aggregation.*"
        }
    ]
}

Saving Workflow results into file

This Workflow was created to achieve few ends:

  • getting predictions from object detection model and returning them to the caller

  • persisting the predictions - each one in separate JSON file

  • aggregating the predictions data - producing report on each 6th input image

  • saving the results in CSV file, appending rows until file size is exceeded

Run on video to produce meaningful results

This workflow will not work using the docs preview. You must run it on video file. Copy the template into your Roboflow app, start inference server and use video preview to get the results.

Workflow definition
{
    "version": "1.0",
    "inputs": [
        {
            "type": "WorkflowImage",
            "name": "image"
        },
        {
            "type": "WorkflowParameter",
            "name": "target_directory"
        },
        {
            "type": "WorkflowParameter",
            "name": "model_id",
            "default_value": "yolov8n-640"
        }
    ],
    "steps": [
        {
            "type": "roboflow_core/roboflow_object_detection_model@v2",
            "name": "model",
            "images": "$inputs.image",
            "model_id": "$inputs.model_id"
        },
        {
            "type": "roboflow_core/expression@v1",
            "name": "json_formatter",
            "data": {
                "predictions": "$steps.model.predictions"
            },
            "switch": {
                "type": "CasesDefinition",
                "cases": [],
                "default": {
                    "type": "DynamicCaseResult",
                    "parameter_name": "predictions",
                    "operations": [
                        {
                            "type": "DetectionsToDictionary"
                        },
                        {
                            "type": "ConvertDictionaryToJSON"
                        }
                    ]
                }
            }
        },
        {
            "type": "roboflow_core/local_file_sink@v1",
            "name": "predictions_sink",
            "content": "$steps.json_formatter.output",
            "file_type": "json",
            "output_mode": "separate_files",
            "target_directory": "$inputs.target_directory",
            "file_name_prefix": "prediction"
        },
        {
            "type": "roboflow_core/data_aggregator@v1",
            "name": "data_aggregation",
            "data": {
                "predicted_classes": "$steps.model.predictions",
                "number_of_predictions": "$steps.model.predictions"
            },
            "data_operations": {
                "predicted_classes": [
                    {
                        "type": "DetectionsPropertyExtract",
                        "property_name": "class_name"
                    }
                ],
                "number_of_predictions": [
                    {
                        "type": "SequenceLength"
                    }
                ]
            },
            "aggregation_mode": {
                "predicted_classes": [
                    "count_distinct"
                ],
                "number_of_predictions": [
                    "min",
                    "max",
                    "sum"
                ]
            },
            "interval": 6,
            "interval_unit": "runs"
        },
        {
            "type": "roboflow_core/csv_formatter@v1",
            "name": "csv_formatter",
            "columns_data": {
                "number_of_distinct_classes": "$steps.data_aggregation.predicted_classes_count_distinct",
                "min_number_of_bounding_boxes": "$steps.data_aggregation.number_of_predictions_min",
                "max_number_of_bounding_boxes": "$steps.data_aggregation.number_of_predictions_max",
                "total_number_of_bounding_boxes": "$steps.data_aggregation.number_of_predictions_sum"
            }
        },
        {
            "type": "roboflow_core/local_file_sink@v1",
            "name": "reports_sink",
            "content": "$steps.csv_formatter.csv_content",
            "file_type": "csv",
            "output_mode": "append_log",
            "target_directory": "$inputs.target_directory",
            "file_name_prefix": "aggregation_report"
        }
    ],
    "outputs": [
        {
            "type": "JsonField",
            "name": "predictions",
            "selector": "$steps.model.predictions"
        }
    ]
}