> ## Documentation Index
> Fetch the complete documentation index at: https://docs.phones.farm/llms.txt
> Use this file to discover all available pages before exploring further.

# ONNX Model

> Deploying embedded ONNX neural network models for real-time visual UI detection and on-screen element classification.

## 1.Overview

> [!NOTE]
>
> 💡 Introduction for Beginners
>
> Compared to general image search and OCR, by loading specifically trained models, you can easily achieve more powerful and versatile **image recognition and object detection** capabilities.

### API List

| API | Description |
| - | - |
| loadOnnxModel | Load ONNX model |
| predictScreenshot | Perform inference prediction on device screen |
| unloadOnnxModel | Unload ONNX model |

## 2.Load ONNX Model

Load the specified ONNX model file, start an independent inference process, and return the unique identifier (onnxId) of the model.

💡 Hardware Acceleration

This API automatically detects whether the host machine supports CUDA. If not supported, it defaults to CPU inference without manual configuration.

### API Description

#### API Type

```
loadOnnxModel
```

#### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| file | string | Yes | Full path to the ONNX model file |

#### Response Payload

Returns a string type model ID (onnxId) in the format: `onnx_` + random string

```
// Return example
"onnx_8k3j2h9s1d4f6"
```

### Basic Usage

```
// Load ONNX model
const onnxId = await apiInvoke('loadOnnxModel', {
    file: 'C:\\models\\yolov5s.onnx'
});

console.log('Model loaded successfully, ID:', onnxId);
// Output: Model loaded successfully, ID: onnx_8k3j2h9s1d4f6
```

### Performance Recommendations

1. **Unload as needed** - For models that are not frequently used, unload them promptly after completing tasks
2. **Reasonable allocation** - Limited by host performance, don't load too many models

## 3.Unload ONNX Model

Unload the specified ONNX model, terminate related processes and release system resources.

### API Description

#### API Type

```
unloadOnnxModel
```

#### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| onnxid | string | Yes | Model ID returned by loadOnnxModel |

#### Response Payload

No return value (void)

### Basic Usage

```
// Load model first
const onnxid = await apiInvoke('loadOnnxModel', {
    file: 'C:\\models\\yolov8s.onnx'
});

// Use model for inference...
...

// Unload model after use
await apiInvoke('unloadOnnxModel', {
    onnxid: onnxid
});

console.log('Model unloaded');
```

## 4.Predict Model

### Model Inference

Use the loaded ONNX model to perform AI inference prediction on the specified device and return the model's inference results.

### API Description

#### API Type

```
predictOnnxModel
```

#### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| onnxid | string | Yes | Model ID returned by loadOnnxModel |
| deviceId | string | Yes | Device ID for getting screenshot |
| rect | array | No | Prediction region `[x1, y1, x2, y2]`, defaults to full screen |

#### Response Payload

Returns the model's inference results.

```
// Typical return format for YOLO object detection model
[
    {
        "classId": 0,
        "x": 100,
        "y": 200,
        "confidence": 0.95,
        "boundingBox": { ... }  // [x, y, width, height]
    },
    {
        "classId": 1,
        "x": 100,
        "y": 200,
        "confidence": 0.87,
        "boundingBox": { ... }
    }
]
```

### Basic Usage

```
// 1. Load model
const onnxid = await apiInvoke('loadOnnxModel', {
    file: 'C:\\models\\yolov5s.onnx'
});

// 2. Perform inference
const result = await apiInvoke('predictOnnxModel', {
    onnxid: onnxid,
    deviceId: 'P72578581E07'
});

console.log('Detection results:', result);
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.