forked from acalmll/Beyond-Sight-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.txt
More file actions
219 lines (182 loc) · 6.21 KB
/
files.txt
File metadata and controls
219 lines (182 loc) · 6.21 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
detector.dart
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:flutter_vision/flutter_vision.dart';
import 'boundingbox.dart';
List<CameraDescription>? cameras;
class ObjectDetectionScreen extends StatefulWidget {
final String objectToFind; // Pass the object to find from speech
const ObjectDetectionScreen({super.key, required this.objectToFind});
@override
_ObjectDetectionScreenState createState() => _ObjectDetectionScreenState();
}
class _ObjectDetectionScreenState extends State<ObjectDetectionScreen> {
late FlutterVision vision;
CameraController? controller;
bool isDetecting = false;
List<dynamic> detections = [];
bool modelLoaded = false;
String latency = "0 ms"; // Variable to hold latency value
@override
void initState() {
super.initState();
initializeCameras();
}
// Initialize the cameras and load the model
Future<void> initializeCameras() async {
try {
cameras = await availableCameras();
if (cameras == null || cameras!.isEmpty) {
throw Exception('No cameras found');
}
controller = CameraController(
cameras![0],
ResolutionPreset.high, // Lower resolution for faster processing
);
await controller?.initialize();
await loadYoloModel();
if (!mounted) return;
controller?.startImageStream((CameraImage image) {
if (!isDetecting && modelLoaded) {
isDetecting = true;
detectObjects(image);
}
});
setState(() {});
} catch (e) {
print('Error initializing cameras: $e');
}
}
// Load the YOLOv8 model
Future<void> loadYoloModel() async {
vision = FlutterVision();
await vision.loadYoloModel(
labels: 'assets/coco_classes.txt',
modelPath: 'assets/best_float16.tflite',
modelVersion: 'yolov8',
quantization: false,
numThreads: 4,
useGpu: true,
);
modelLoaded = true;
}
// Perform object detection on each frame
Future<void> detectObjects(CameraImage image) async {
final stopwatch = Stopwatch()..start();
final result = await vision.yoloOnFrame(
bytesList: image.planes.map((plane) => plane.bytes).toList(),
imageHeight: image.height,
imageWidth: image.width,
iouThreshold: 0.4,
confThreshold: 0.2,
classThreshold: 0.2,
);
stopwatch.stop();
setState(() {
// Filter detections based on speech command
detections = result.where((detection) {
return detection['tag'] == widget.objectToFind.toLowerCase();
}).toList();
latency = '${stopwatch.elapsed.inMilliseconds} ms';
});
isDetecting = false;
}
@override
void dispose() {
controller?.dispose();
vision.closeYoloModel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (controller == null || !controller!.value.isInitialized) {
return const Center(child: CircularProgressIndicator());
}
return Stack(
children: [
SizedBox(
width: constraints.maxWidth,
height: constraints.maxHeight,
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: controller!.value.previewSize!.height,
height: controller!.value.previewSize!.width,
child: CameraPreview(controller!),
),
),
),
// Include the bounding box overlay
_buildBoundingBoxOverlay(),
],
);
},
);
}
// Move this method to the separate bounding box file
Widget _buildBoundingBoxOverlay() {
return CustomPaint(
painter: BoundingBoxPainter(detections, controller!, latency),
child: Container(),
);
}
}
boundingbox.dart
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'dart:ui' as ui;
class BoundingBoxPainter extends CustomPainter {
final List<dynamic> detections;
final CameraController cameraController;
final String latency;
BoundingBoxPainter(this.detections, this.cameraController, this.latency);
@override
void paint(ui.Canvas canvas, ui.Size size) {
final paint = Paint()
..color = Colors.red // High contrast color for bounding box
..style = PaintingStyle.stroke
..strokeWidth = 3.0; // Increase stroke width for visibility
// Get the camera preview size and the size of the canvas where we draw
final previewSize = cameraController.value.previewSize!;
final double scaleX = size.width / previewSize.height;
final double scaleY = size.height / previewSize.width;
for (var detection in detections) {
final box = detection['box']; // Assuming this is [left, top, right, bottom, confidence]
if (box.length == 5) { // Ensure box format is correct
// Scale the bounding box coordinates to fit the screen
final left = box[0] * scaleX;
final top = box[1] * scaleY;
final right = box[2] * scaleX;
final bottom = box[3] * scaleY;
// Draw rectangle using the scaled coordinates
canvas.drawRect(Rect.fromLTRB(left, top, right, bottom), paint);
// Draw the confidence text above the bounding box
final textPainter = TextPainter(
text: TextSpan(
text: '${(box[4] * 100).toStringAsFixed(2)}% - ${detection['tag']}',
style: const TextStyle(color: Colors.red, fontSize: 16),
),
textDirection: ui.TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(canvas, Offset(left, top - 20)); // Adjust the position as needed
}
}
// Draw latency text on the screen
final latencyTextPainter = TextPainter(
text: TextSpan(
text: 'Latency: $latency',
style: const TextStyle(color: Colors.white, fontSize: 16),
),
textDirection: ui.TextDirection.ltr,
);
latencyTextPainter.layout();
latencyTextPainter.paint(canvas, const Offset(16, 16)); // Position the latency text
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true; // Always repaint for new detections
}
}