ultralytics_yolo 0.0.4
ultralytics_yolo: ^0.0.4 copied to clipboard
Flutter plugin for YOLO (You Only Look Once) models, supporting object detection, segmentation, classification, pose estimation and oriented bounding boxes (OBB) on both Android and iOS.
Ultralytics YOLO Flutter Package #
Flutter plugin for YOLO (You Only Look Once) models, supporting object detection, segmentation, classification, pose estimation and oriented bounding boxes (OBB) on both Android and iOS.
Features #
- Object Detection: Identify and locate objects in images and camera feeds with bounding boxes
- Segmentation: Perform pixel-level segmentation of objects
- Classification: Classify objects in images
- Pose Estimation: Detect human poses and keypoints
- Oriented Bounding Boxes (OBB): Detect rotated or oriented bounding boxes for objects
- Cross-Platform: Works on both Android and iOS
- Real-time Processing: Optimized for real-time inference on mobile devices
- Camera Integration: Easy integration with device cameras
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
ultralytics_yolo: ^0.0.4
Then run:
flutter pub get
Platform-Specific Setup #
Android #
Add the following permissions to your AndroidManifest.xml
file:
<!-- For camera access -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- For accessing images from storage -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Set minimum SDK version in your android/app/build.gradle
:
minSdkVersion 21
iOS #
Add these entries to your Info.plist
:
<key>NSCameraUsageDescription</key>
<string>This app needs camera access to detect objects</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs photos access to get images for object detection</string>
Usage #
Basic Example #
import 'package:flutter/material.dart';
import 'package:ultralytics_yolo/yolo.dart';
import 'package:ultralytics_yolo/yolo_view.dart';
import 'package:ultralytics_yolo/yolo_task.dart';
class YoloDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('YOLO Object Detection')),
body: Center(
child: YoloView(
task: YOLOTask.detect,
modelPath: 'assets/models/yolo11n.tflite',
threshold: 0.5,
onResult: (results) {
// Handle detection results
print('Detected ${results.length} objects');
},
),
),
);
}
}
Object Detection with Camera Feed #
YoloView(
task: YOLOTask.detect,
modelPath: 'assets/models/yolo11n.tflite',
useCamera: true,
cameraResolution: '720p',
threshold: 0.5,
onResult: (results) {
for (var result in results) {
print('Detected: ${result.className}, Confidence: ${result.confidence}');
}
},
)
Image Segmentation #
YoloView(
task: YOLOTask.segment,
modelPath: 'assets/models/yolo11n-seg.tflite',
threshold: 0.5,
onResult: (results) {
// Process segmentation results
},
)
Pose Estimation #
YoloView(
task: YOLOTask.pose,
modelPath: 'assets/models/yolo11n-pose.tflite',
threshold: 0.5,
onResult: (results) {
// Process pose keypoints
},
)
API Reference #
Classes #
YOLO
Main class for YOLO operations.
YOLO({
required String modelPath,
required YOLOTask task,
double threshold = 0.5,
});
YoloView
Flutter widget to display YOLO detection results.
YoloView({
required YOLOTask task,
required String modelPath,
double threshold = 0.5,
bool useCamera = false,
String cameraResolution = '720p',
Function(List<YOLOResult>)? onResult,
});
YOLOResult
Contains detection results.
class YOLOResult {
final int classIndex;
final String className;
final double confidence;
final Rect boundingBox;
// For segmentation
final List<List<double>>? mask;
// For pose estimation
final List<Point>? keypoints;
}
Enums #
YOLOTask
enum YOLOTask {
detect, // Object detection
segment, // Image segmentation
classify, // Image classification
pose, // Pose estimation
obb, // Oriented bounding boxes
}
Platform Support #
Android | iOS | Web | macOS | Windows | Linux |
---|---|---|---|---|---|
✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
Troubleshooting #
Common Issues #
-
Model loading fails
- Make sure your model file is correctly placed in the assets directory
- Verify that the model path is correctly specified
- Check that the model format is compatible with TFLite
-
Low performance on older devices
- Try using smaller models (e.g., YOLOv8n instead of YOLOv8l)
- Reduce input image resolution
- Adjust threshold values to reduce the number of detections
-
Camera permission issues
- Ensure that your app has the proper permissions in the manifest or Info.plist
- Handle runtime permissions properly in your app
License #
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0) - see the LICENSE file for details.