pose_detection_tflite 0.0.1
pose_detection_tflite: ^0.0.1 copied to clipboard
Pose & landmark detection using on-device TFLite models.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pose_detection_tflite/pose_detection_tflite.dart';
void main() {
runApp(const PoseDetectionApp());
}
class PoseDetectionApp extends StatelessWidget {
const PoseDetectionApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Pose Detection Demo',
theme: ThemeData(
colorSchemeSeed: Colors.blue,
useMaterial3: true,
),
home: const PoseDetectionScreen(),
);
}
}
class PoseDetectionScreen extends StatefulWidget {
const PoseDetectionScreen({super.key});
@override
State<PoseDetectionScreen> createState() => _PoseDetectionScreenState();
}
class _PoseDetectionScreenState extends State<PoseDetectionScreen> {
final PoseDetector _poseDetector = PoseDetector();
final ImagePicker _picker = ImagePicker();
bool _isInitialized = false;
bool _isProcessing = false;
File? _imageFile;
List<PoseResult> _results = [];
String? _errorMessage;
@override
void initState() {
super.initState();
_initializeDetectors();
}
Future<void> _initializeDetectors() async {
setState(() {
_isProcessing = true;
_errorMessage = null;
});
try {
await _poseDetector.initialize(
options: const PoseOptions(
mode: PoseMode.boxesAndLandmarks,
landmarkModel: PoseLandmarkModel.heavy,
detectorConf: 0.6,
detectorIou: 0.4,
maxDetections: 10,
minLandmarkScore: 0.5,
),
);
setState(() {
_isInitialized = true;
_isProcessing = false;
});
} catch (e) {
setState(() {
_isProcessing = false;
_errorMessage = 'Failed to initialize: $e';
});
}
}
Future<void> _pickImage(ImageSource source) async {
try {
final XFile? pickedFile = await _picker.pickImage(source: source);
if (pickedFile == null) return;
setState(() {
_imageFile = File(pickedFile.path);
_results = [];
_isProcessing = true;
_errorMessage = null;
});
final bytes = await _imageFile!.readAsBytes();
final results = await _poseDetector.detect(bytes);
setState(() {
_results = results;
_isProcessing = false;
if (results.isEmpty) _errorMessage = 'No people detected in image';
});
} catch (e) {
setState(() {
_isProcessing = false;
_errorMessage = 'Error: $e';
});
}
}
void _showImageSourceDialog() {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Select Image Source'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Gallery'),
onTap: () {
Navigator.pop(context);
_pickImage(ImageSource.gallery);
},
),
ListTile(
leading: const Icon(Icons.camera_alt),
title: const Text('Camera'),
onTap: () {
Navigator.pop(context);
_pickImage(ImageSource.camera);
},
),
],
),
);
},
);
}
@override
void dispose() {
_poseDetector.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Pose Detection Demo'),
actions: [
if (_isInitialized && _imageFile != null)
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: _showPoseInfo,
),
],
),
body: _buildBody(),
floatingActionButton: _isInitialized && !_isProcessing
? FloatingActionButton.extended(
onPressed: _showImageSourceDialog,
icon: const Icon(Icons.add_photo_alternate),
label: const Text('Select Image'),
)
: null,
);
}
Widget _buildBody() {
if (!_isInitialized && _isProcessing) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Initializing pose detector...'),
],
),
);
}
if (_errorMessage != null && _imageFile == null) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error_outline, size: 64, color: Colors.red),
const SizedBox(height: 16),
Text(
_errorMessage!,
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.red),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _initializeDetectors,
child: const Text('Retry'),
),
],
),
);
}
if (_imageFile == null) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person_outline, size: 100, color: Colors.grey[400]),
const SizedBox(height: 24),
Text('Select an image to detect pose',
style: TextStyle(fontSize: 18, color: Colors.grey[600])),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _showImageSourceDialog,
icon: const Icon(Icons.add_photo_alternate),
label: const Text('Select Image'),
),
],
),
);
}
return SingleChildScrollView(
child: Column(
children: [
PoseVisualizerWidget(
imageFile: _imageFile!,
results: _results,
),
if (_isProcessing)
const Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
CircularProgressIndicator(),
SizedBox(height: 8),
Text('Detecting pose...'),
],
),
),
if (_errorMessage != null && !_isProcessing)
Padding(
padding: const EdgeInsets.all(16),
child: Card(
color: Colors.red[50],
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
const Icon(Icons.error_outline, color: Colors.red),
const SizedBox(width: 8),
Expanded(child: Text(_errorMessage!)),
],
),
),
),
),
if (_results.isNotEmpty)
Padding(
padding: const EdgeInsets.all(16),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Detections: ${_results.length} ✓',
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(color: Colors.green, fontWeight: FontWeight.bold)),
],
),
),
),
),
],
),
);
}
void _showPoseInfo() {
if (_results.isEmpty) return;
final first = _results.first;
showModalBottomSheet(
context: context,
builder: (context) => DraggableScrollableSheet(
initialChildSize: 0.7,
minChildSize: 0.5,
maxChildSize: 0.95,
expand: false,
builder: (context, scrollController) => ListView(
controller: scrollController,
padding: const EdgeInsets.all(16),
children: [
Text('Landmark Details (first pose)',
style: Theme.of(context).textTheme.headlineSmall),
const SizedBox(height: 16),
..._buildLandmarkListFor(first),
],
),
),
);
}
List<Widget> _buildLandmarkListFor(PoseResult result) {
final lm = result.landmarks ?? const <PoseLandmark>[];
return lm.map((landmark) {
final pixel = landmark.toPixel(result.imageWidth, result.imageHeight);
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: ListTile(
leading: CircleAvatar(
backgroundColor: landmark.visibility > 0.5 ? Colors.green : Colors.orange,
child: Text(landmark.type.index.toString(), style: const TextStyle(fontSize: 12)),
),
title: Text(_landmarkName(landmark.type), style: const TextStyle(fontWeight: FontWeight.w500)),
subtitle: Text('Position: (${pixel.x}, ${pixel.y})\nVisibility: ${(landmark.visibility * 100).toStringAsFixed(0)}%'),
isThreeLine: true,
),
);
}).toList();
}
String _landmarkName(PoseLandmarkType type) {
return type.toString().split('.').last.replaceAllMapped(
RegExp(r'[A-Z]'),
(match) => ' ${match.group(0)}',
).trim();
}
}
class PoseVisualizerWidget extends StatelessWidget {
final File imageFile;
final List<PoseResult> results;
const PoseVisualizerWidget({super.key, required this.imageFile, required this.results});
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
return Stack(
children: [
Image.file(imageFile, fit: BoxFit.contain),
Positioned.fill(child: CustomPaint(painter: MultiOverlayPainter(results: results))),
],
);
});
}
}
class MultiOverlayPainter extends CustomPainter {
final List<PoseResult> results;
MultiOverlayPainter({required this.results});
@override
void paint(Canvas canvas, Size size) {
if (results.isEmpty) return;
final iw = results.first.imageWidth;
final ih = results.first.imageHeight;
final imageAspect = iw / ih;
final canvasAspect = size.width / size.height;
double scaleX, scaleY;
double offsetX = 0, offsetY = 0;
if (canvasAspect > imageAspect) {
scaleY = size.height / ih;
scaleX = scaleY;
offsetX = (size.width - iw * scaleX) / 2;
} else {
scaleX = size.width / iw;
scaleY = scaleX;
offsetY = (size.height - ih * scaleY) / 2;
}
for (final r in results) {
_drawBbox(canvas, r, scaleX, scaleY, offsetX, offsetY);
if (r.hasLandmarks) {
_drawConnections(canvas, r, scaleX, scaleY, offsetX, offsetY);
_drawLandmarks(canvas, r, scaleX, scaleY, offsetX, offsetY);
}
}
}
void _drawConnections(Canvas canvas, PoseResult result, double scaleX, double scaleY, double offsetX, double offsetY) {
final paint = Paint()
..color = Colors.green.withOpacity(0.8)
..strokeWidth = 3
..strokeCap = StrokeCap.round;
final connections = [
[PoseLandmarkType.leftEye, PoseLandmarkType.nose],
[PoseLandmarkType.rightEye, PoseLandmarkType.nose],
[PoseLandmarkType.leftEye, PoseLandmarkType.leftEar],
[PoseLandmarkType.rightEye, PoseLandmarkType.rightEar],
[PoseLandmarkType.mouthLeft, PoseLandmarkType.mouthRight],
[PoseLandmarkType.leftShoulder, PoseLandmarkType.rightShoulder],
[PoseLandmarkType.leftShoulder, PoseLandmarkType.leftHip],
[PoseLandmarkType.rightShoulder, PoseLandmarkType.rightHip],
[PoseLandmarkType.leftHip, PoseLandmarkType.rightHip],
[PoseLandmarkType.leftShoulder, PoseLandmarkType.leftElbow],
[PoseLandmarkType.leftElbow, PoseLandmarkType.leftWrist],
[PoseLandmarkType.leftWrist, PoseLandmarkType.leftPinky],
[PoseLandmarkType.leftWrist, PoseLandmarkType.leftIndex],
[PoseLandmarkType.leftWrist, PoseLandmarkType.leftThumb],
[PoseLandmarkType.rightShoulder, PoseLandmarkType.rightElbow],
[PoseLandmarkType.rightElbow, PoseLandmarkType.rightWrist],
[PoseLandmarkType.rightWrist, PoseLandmarkType.rightPinky],
[PoseLandmarkType.rightWrist, PoseLandmarkType.rightIndex],
[PoseLandmarkType.rightWrist, PoseLandmarkType.rightThumb],
[PoseLandmarkType.leftHip, PoseLandmarkType.leftKnee],
[PoseLandmarkType.leftKnee, PoseLandmarkType.leftAnkle],
[PoseLandmarkType.leftAnkle, PoseLandmarkType.leftHeel],
[PoseLandmarkType.leftAnkle, PoseLandmarkType.leftFootIndex],
[PoseLandmarkType.rightHip, PoseLandmarkType.rightKnee],
[PoseLandmarkType.rightKnee, PoseLandmarkType.rightAnkle],
[PoseLandmarkType.rightAnkle, PoseLandmarkType.rightHeel],
[PoseLandmarkType.rightAnkle, PoseLandmarkType.rightFootIndex],
];
for (final c in connections) {
final start = result.getLandmark(c[0]);
final end = result.getLandmark(c[1]);
if (start != null && end != null && start.visibility > 0.5 && end.visibility > 0.5) {
canvas.drawLine(
Offset(start.x * scaleX + offsetX, start.y * scaleY + offsetY),
Offset(end.x * scaleX + offsetX, end.y * scaleY + offsetY),
paint,
);
}
}
}
void _drawLandmarks(Canvas canvas, PoseResult result, double scaleX, double scaleY, double offsetX, double offsetY) {
for (final l in result.landmarks ?? const <PoseLandmark>[]) {
if (l.visibility > 0.5) {
final center = Offset(l.x * scaleX + offsetX, l.y * scaleY + offsetY);
final glow = Paint()..color = Colors.blue.withOpacity(0.3);
final point = Paint()..color = Colors.red;
final centerDot = Paint()..color = Colors.white;
canvas.drawCircle(center, 8, glow);
canvas.drawCircle(center, 5, point);
canvas.drawCircle(center, 2, centerDot);
}
}
}
void _drawBbox(Canvas canvas, PoseResult r, double scaleX, double scaleY, double offsetX, double offsetY) {
final boxPaint = Paint()
..color = Colors.orangeAccent.withOpacity(0.9)
..style = PaintingStyle.stroke
..strokeWidth = 3;
final fillPaint = Paint()
..color = Colors.orangeAccent.withOpacity(0.08)
..style = PaintingStyle.fill;
final x1 = r.bboxPx.left * scaleX + offsetX;
final y1 = r.bboxPx.top * scaleY + offsetY;
final x2 = r.bboxPx.right * scaleX + offsetX;
final y2 = r.bboxPx.bottom * scaleY + offsetY;
final rect = Rect.fromLTRB(x1, y1, x2, y2);
canvas.drawRect(rect, fillPaint);
canvas.drawRect(rect, boxPaint);
}
@override
bool shouldRepaint(MultiOverlayPainter oldDelegate) => true;
}