subject_lift_kit 0.0.1
subject_lift_kit: ^0.0.1 copied to clipboard
A Flutter plugin for subject lifting using Apple's Vision Framework.
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:subject_lift_kit/subject_lift_kit.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Subject Lift Kit Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final ImagePicker _picker = ImagePicker();
bool _isProcessing = false;
SegmentationResult? _result;
XFile? _selectedImage;
Future<void> _pickImage() async {
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
_selectedImage = image;
_result = null;
});
}
}
Future<void> _processImage() async {
if (_selectedImage == null) return;
setState(() {
_isProcessing = true;
});
try {
final bytes = await _selectedImage!.readAsBytes();
final result = await SubjectLiftKit.extractForeground(bytes);
setState(() {
_result = result;
});
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $e')),
);
}
} finally {
setState(() {
_isProcessing = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Subject Lift Kit'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_selectedImage != null) ...[
const Text('Original Image'),
const SizedBox(height: 8),
FutureBuilder<Uint8List>(
future: _selectedImage!.readAsBytes(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Image.memory(
snapshot.data!,
height: 200,
);
}
return const CircularProgressIndicator();
},
),
const SizedBox(height: 16),
],
if (_result?.cutoutImageBytes != null) ...[
const Text('Cutout Result'),
const SizedBox(height: 8),
Image.memory(
_result!.cutoutImageBytes!,
height: 200,
),
const SizedBox(height: 16),
],
ElevatedButton(
onPressed: _pickImage,
child: const Text('Select Image'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _selectedImage != null && !_isProcessing ? _processImage : null,
child: _isProcessing
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Extract Foreground'),
),
],
),
),
),
);
}
}