dog_detection 2.0.0 copy "dog_detection: ^2.0.0" to clipboard
dog_detection: ^2.0.0 copied to clipboard

Dog face and landmark detection using on-device LiteRT (formerly TensorFlow Lite) models.

dog_detection

Platform Language: Dart
Pub Version pub points License

Demo

On-device dog detection using TFLite models. Detects dogs in images with breed identification, body pose estimation, face localization, and 46-point facial landmarks, all running locally with no remote API.

Features #

  • Dog body detection with bounding box (SSD-based)
  • Breed identification with confidence score
  • Body pose estimation via SuperAnimal keypoints
  • Face localization and 46-point facial landmark extraction (DogFLW)
  • Truly cross-platform: compatible with Android, iOS, macOS, Windows, and Linux
  • Detection always runs in a background isolate that DogDetector owns, so the UI never blocks
  • Configurable performance with XNNPACK, GPU, and CoreML acceleration

Quick Start #

import 'package:dog_detection/dog_detection.dart';

final detector = DogDetector(mode: DogDetectionMode.full);
await detector.initialize();

final dogs = await detector.detect(imageBytes);
for (final dog in dogs) {
  print('${dog.species} at ${dog.boundingBox}');
  print('Breed: ${dog.breed} (${(dog.speciesConfidence! * 100).toStringAsFixed(0)}%)');
  print('Pose keypoints: ${dog.pose?.landmarks.length}');
  print('Face landmarks: ${dog.face?.landmarks.length}');
}

await detector.dispose();

Dog Face Landmarks (46-Point) #

The landmarks property returns a list of 46 DogLandmark objects representing key points on the detected dog face.

Landmark Groups #

Group Count Points
Left ear 7 Ear outline and tip
Right ear 7 Ear outline and tip
Left eye 4 Eye corners and center
Right eye 4 Eye corners and center
Nose bridge 2 Bridge top and bottom
Nose ring 8 Nostril outline
Mouth/chin 14 Lips, jaw, and chin

Accessing Landmarks #

final DogFace face = faces.first;

// Iterate through all landmarks
for (final landmark in face.landmarks) {
  print('${landmark.type.name}: (${landmark.x}, ${landmark.y})');
}

Breed Identification #

In full and poseOnly modes, each detected dog includes a predicted breed label and confidence score from the species classifier.

final dogs = await detector.detect(imageBytes);
for (final dog in dogs) {
  if (dog.breed != null) {
    print('Breed: ${dog.breed}');
    print('Confidence: ${(dog.speciesConfidence! * 100).toStringAsFixed(1)}%');
  }
}

Bounding Boxes #

The boundingBox property returns a BoundingBox object representing the dog face bounding box in absolute pixel coordinates.

final BoundingBox boundingBox = face.boundingBox;

// Access edges
final double left = boundingBox.left;
final double top = boundingBox.top;
final double right = boundingBox.right;
final double bottom = boundingBox.bottom;

// Calculate dimensions
final double width = boundingBox.right - boundingBox.left;
final double height = boundingBox.bottom - boundingBox.top;

print('Box: ($left, $top) to ($right, $bottom)');
print('Size: $width x $height');

Model Details #

Model Size Input Purpose
Face localizer 16 MB 224×224 Dog face detection and bounding box
Landmark model (full) 11 MB 384×384 46-point facial landmark extraction

Configuration Options #

The DogDetector constructor accepts several configuration options:

final detector = DogDetector(
  mode: DogDetectionMode.full,               // Detection mode
  poseModel: AnimalPoseModel.rtmpose,        // Body pose model variant
  landmarkModel: DogLandmarkModel.full,      // Face landmark model variant
  cropMargin: 0.20,                          // Margin around detected body for crop
  detThreshold: 0.5,                         // SSD detection confidence threshold
  interpreterPoolSize: 1,                    // TFLite interpreter pool size
  performanceConfig: PerformanceConfig.disabled, // Performance optimization
);
Option Type Default Description
mode DogDetectionMode full Detection mode
poseModel AnimalPoseModel rtmpose Body pose model variant
landmarkModel DogLandmarkModel full Face landmark model variant
cropMargin double 0.20 Margin around detected body crop (0.0-1.0)
detThreshold double 0.5 SSD detection confidence threshold
interpreterPoolSize int 1 TFLite interpreter pool size
performanceConfig PerformanceConfig disabled Hardware acceleration config

Detection Modes #

Mode Features Speed
full Body detection + breed ID + body pose + face landmarks Standard
poseOnly Body detection + breed ID + body pose (no face) Faster
faceOnly Face localizer + face landmarks only (legacy, no SSD) Fastest

Background Isolate Detection #

Detection always runs in a background isolate. DogDetector spawns and owns that isolate during initialize(), so the whole pipeline (decode, SSD, species, pose, localizer, landmarks) stays off the main thread and the UI is never blocked. There is nothing extra to opt into:

import 'package:dog_detection/dog_detection.dart';

// initialize() loads the models and spawns the worker isolate
final detector = DogDetector(mode: DogDetectionMode.full);
await detector.initialize();

// Runs in the background isolate; the UI thread stays free
final dogs = await detector.detect(imageBytes);

for (final dog in dogs) {
  print('${dog.breed} at ${dog.boundingBox}');
  print('Face landmarks: ${dog.face?.landmarks.length}');
}

// Tears down the isolate and frees the native interpreters
await detector.dispose();

Model bytes are transferred into the isolate with TransferableTypedData, so the ~70MB of weights in the default configuration move without being copied.

Migrating from DogDetectorIsolate #

DogDetectorIsolate is deprecated and will be removed in the next major release. It now just delegates to DogDetector, so migration is a rename:

Before After
await DogDetectorIsolate.spawn(...) DogDetector(...) then await initialize()
detector.detectDogs(bytes) detector.detect(bytes)
detector.detectDogsFromMat(mat) detector.detectFromMat(mat)
detector.dispose() detector.dispose() (unchanged)

onDownloadProgress moves from spawn() to initialize(); every other configuration argument stays on the DogDetector constructor.

Performance #

Hardware Acceleration #

The package automatically selects the best acceleration strategy for each platform:

Platform Default Delegate Speedup Notes
macOS XNNPACK 2-5x SIMD vectorization (NEON on ARM, AVX on x86)
Linux XNNPACK 2-5x SIMD vectorization
iOS Metal GPU 2-4x Hardware GPU acceleration
Android XNNPACK 2-5x ARM NEON SIMD acceleration
Windows XNNPACK 2-5x SIMD vectorization (AVX on x86)

No configuration needed, just call initialize() and you get the optimal performance for your platform.

Advanced Performance Configuration #

// Auto mode (default), optimal for each platform
await detector.initialize();

// Force XNNPACK (all native platforms)
final detector = DogDetector(
  performanceConfig: PerformanceConfig.xnnpack(numThreads: 4),
);
await detector.initialize();

// Force GPU delegate (iOS recommended, Android experimental)
final detector = DogDetector(
  performanceConfig: PerformanceConfig.gpu(),
);
await detector.initialize();

// CPU-only (maximum compatibility)
final detector = DogDetector(
  performanceConfig: PerformanceConfig.disabled,
);
await detector.initialize();

Credits #

Models trained on the DogFLW dataset.

Example #

The sample code from the pub.dev example tab includes a Flutter app that paints detections onto an image: bounding boxes and 46-point dog facial landmarks.