-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
160 lines (142 loc) · 4.86 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:image_picker/image_picker.dart';
import 'package:google_mlkit_image_labeling/google_mlkit_image_labeling.dart';
import 'dart:io' as io;
import 'package:flutter/services.dart';
//import 'package:google_ml_kit/google_ml_kit.dart';
Future<String> _getModel(String assetPath) async {
if (io.Platform.isAndroid) {
return 'flutter_assets/$assetPath';
}
final path = '${(await getApplicationSupportDirectory()).path}/$assetPath';
await io.Directory(dirname(path)).create(recursive: true);
final file = io.File(path);
if (!await file.exists()) {
final byteData = await rootBundle.load(assetPath);
await file.writeAsBytes(byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
}
return file.path;
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Object Detection',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: ObjectDetection(),
);
}
}
class ObjectDetection extends StatefulWidget {
@override
State<StatefulWidget> createState() => _ObjectDetectionState();
}
class _ObjectDetectionState extends State<ObjectDetection> {
File? _imageFile;
String? _result;
String? modelPath;
ImageLabeler? _imageLabeler;
final ImagePicker _picker = ImagePicker();
@override
void initState() {
super.initState();
_getModelAndStartImageLabeler();
}
void _getModelAndStartImageLabeler() async {
modelPath = await _getModel('assets/model.tflite');
if (modelPath != null) {
final options = LocalLabelerOptions(modelPath: modelPath!, confidenceThreshold: 0.7);
_imageLabeler = ImageLabeler(options: options);
}
}
void _getImageAndDetectObjects(ImageSource source) async {
final PickedFile? pickedImage = await _picker.getImage(source: source);
if (pickedImage != null) {
// 元の画像ファイルを保存
File originalImageFile = File(pickedImage.path);
// 画像をリサイズ
final Uint8List? compressedImage = await FlutterImageCompress.compressWithFile(
pickedImage.path,
minWidth: 400,
minHeight: 300,
);
// リサイズした画像を一時的なファイルに保存
final tempDir = await getTemporaryDirectory();
final path = '${tempDir.path}/temp.jpg';
File file = File(path)..writeAsBytesSync(compressedImage!);
// 一時的なファイルのパスを使用して入力画像を作成
final inputImage = InputImage.fromFilePath(file.path);
final List<ImageLabel> labels = await _imageLabeler?.processImage(inputImage) ?? [];
ImageLabel? highestConfidenceLabel;
for (final label in labels) {
if (label.confidence > 0.7 && (highestConfidenceLabel == null || label.confidence > highestConfidenceLabel.confidence)) {
highestConfidenceLabel = label;
}
}
String result;
if (highestConfidenceLabel != null) {
final confidenceInPercent = (highestConfidenceLabel.confidence * 100).toStringAsFixed(1);
result = 'Label: ${highestConfidenceLabel.label}, Confidence: $confidenceInPercent%';
} else {
result = "Can't detect";
}
// 結果の更新
setState(() {
_imageFile = originalImageFile; // 元の画像を使用
_result = result;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Object Detection'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
_imageFile == null
? Container()
: Image.file(_imageFile!),
SizedBox(height: 10),
_result == null
? Container()
: Text('Detected Objects:\n$_result'),
],
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed:(){_getImageAndDetectObjects(ImageSource.gallery);} ,
tooltip: "Select Image",
child: Icon(Icons.add_photo_alternate),
),
Padding(padding: EdgeInsets.all(10.0)),
FloatingActionButton(
onPressed:(){_getImageAndDetectObjects(ImageSource.camera);} ,
tooltip: "Take Photo",
child: Icon(Icons.add_a_photo),
),
],
)
);
}
@override
void dispose() {
_imageLabeler?.close();
super.dispose();
}
}