predict method
Runs inference on a single image.
Takes raw image bytes as input and returns a map containing the inference results. The returned map contains:
- 'boxes': List of detected objects with bounding box coordinates
- 'detections': List of detections in YOLOResult-compatible format
- Task-specific data (keypoints for pose, mask for segmentation, etc.)
The model must be loaded with loadModel before calling this method.
Example:
// Basic detection usage
final results = await yolo.predict(imageBytes);
final boxes = results['boxes'] as List<dynamic>;
for (var box in boxes) {
print('Class: ${box['class']}, Confidence: ${box['confidence']}');
}
// Pose estimation with YOLOResult
final results = await yolo.predict(imageBytes);
final detections = results['detections'] as List<dynamic>;
for (var detection in detections) {
final result = YOLOResult.fromMap(detection);
if (result.keypoints != null) {
print('Found ${result.keypoints!.length} keypoints');
for (int i = 0; i < result.keypoints!.length; i++) {
final kp = result.keypoints![i];
final conf = result.keypointConfidences![i];
print('Keypoint $i: (${kp.x}, ${kp.y}) confidence: $conf');
}
}
}
imageBytes The raw image data as a Uint8List
confidenceThreshold Optional confidence threshold (0.0-1.0). Defaults to 0.25 if not specified.
iouThreshold Optional IoU threshold for NMS (0.0-1.0). Defaults to 0.4 if not specified.
returns A map containing:
- 'boxes': List of bounding boxes
- 'detections': List of YOLOResult-compatible detection maps
- 'keypoints': (pose only) Raw keypoints data from platform throws ModelNotLoadedException if the model has not been loaded throws InferenceException if there's an error during inference
Implementation
Future<Map<String, dynamic>> predict(
Uint8List imageBytes, {
double? confidenceThreshold,
double? iouThreshold,
}) async {
if (!_isInitialized) {
await loadModel();
}
return await _inference.predict(
imageBytes,
confidenceThreshold: confidenceThreshold,
iouThreshold: iouThreshold,
);
}