apple_vision_image_classification 0.0.2 apple_vision_image_classification: ^0.0.2 copied to clipboard
A Flutter plugin to use Apple Vision Image Classification the classifies an image.
import 'package:apple_vision_image_classification/apple_vision_image_classification.dart';
import 'package:flutter/material.dart';
import '../camera/camera_insert.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'camera/input_image.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const VisionIC(),
);
}
}
class VisionIC extends StatefulWidget {
const VisionIC({
Key? key,
this.onScanned
}):super(key: key);
final Function(dynamic data)? onScanned;
@override
_VisionIC createState() => _VisionIC();
}
class _VisionIC extends State<VisionIC>{
final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
AppleVisionImageClassificationController visionController = AppleVisionImageClassificationController();
InsertCamera camera = InsertCamera();
Size imageSize = const Size(640,640*9/16);
String? deviceId;
bool loading = true;
List<Label>? labels;
late double deviceWidth;
late double deviceHeight;
@override
void initState() {
camera.setupCameras().then((value){
setState(() {
loading = false;
});
camera.startLiveFeed((InputImage i){
if(i.metadata?.size != null){
imageSize = i.metadata!.size;
}
if(mounted) {
Uint8List? image = i.bytes;
visionController.processImage(image!, imageSize).then((data){
labels = data;
setState(() {
});
});
}
});
});
super.initState();
}
@override
void dispose() {
camera.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
deviceWidth = MediaQuery.of(context).size.width;
deviceHeight = MediaQuery.of(context).size.height;
return Stack(
children:<Widget>[
SizedBox(
width: imageSize.width,
height: imageSize.height,
child: loading?Container():CameraSetup(camera: camera, size: imageSize)
),
Column(children: showLabel(),)
]
);
}
List<Widget> showLabel(){
if(labels == null || labels!.isEmpty) return [];
List<Widget> widgets = [];
for(int i = 0; i < labels!.length; i++){
widgets.add(
Container(
width: 320,
height: 20,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(5)
),
child: Text(
'${labels![i].label}: ${labels![i].confidence}',
style: const TextStyle(
color: Colors.white,
fontSize: 12
),
),
)
);
}
return widgets;
}
Widget loadingWidget(){
return Container(
width: deviceWidth,
height:deviceHeight,
color: Theme.of(context).canvasColor,
alignment: Alignment.center,
child: const CircularProgressIndicator(color: Colors.blue)
);
}
}