edge_vision_core 0.1.0
edge_vision_core: ^0.1.0 copied to clipboard
Deterministic C++17 image & camera frame preprocessor, EXIF orienter, letterbox resizer, tensor normalizer, and YOLO postprocessor for Flutter Edge AI.
import 'dart:typed_data';
import 'package:edge_vision_core/edge_vision_core.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const EdgeVisionExampleApp());
}
class EdgeVisionExampleApp extends StatefulWidget {
const EdgeVisionExampleApp({super.key});
@override
State<EdgeVisionExampleApp> createState() => _EdgeVisionExampleAppState();
}
class _EdgeVisionExampleAppState extends State<EdgeVisionExampleApp> {
final VisionPreprocessor _preprocessor = VisionPreprocessor();
String _status = 'Ready to run deterministic C++ preprocessing';
@override
void dispose() {
_preprocessor.dispose();
super.dispose();
}
Future<void> _runDemo() async {
final bmp = _buildDemoBmp(160, 90, 40, 160, 240);
final prep = await _preprocessor.preprocessBytes(
bmp,
PreprocessConfig.yolo(size: 640),
);
final encoded = await _preprocessor.resizeAndEncodeJpeg(
bmp,
targetWidth: 320,
targetHeight: 320,
);
// Simulated end-to-end [1, 1, 6] YOLO output in 640x640 letterbox space
final rawOutput = Float32List.fromList([
32.0, 160.0, 608.0, 480.0, 0.91, 0.0,
]);
final detections = _preprocessor.postprocessYolo(
rawOutput,
numClasses: 80,
numAnchors: 1,
isEndToEnd: true,
meta: prep.meta,
);
final det = detections.single;
setState(() {
_status =
'Input: ${prep.meta.origWidth}x${prep.meta.origHeight}\n'
'Tensor shape: ${prep.shape} (${prep.totalMs.toStringAsFixed(2)} ms)\n'
'Encoded JPEG: ${encoded.bytes.length} bytes\n'
'Unscaled box: [${det.box.x1.toStringAsFixed(1)}, '
'${det.box.y1.toStringAsFixed(1)}, '
'${det.box.x2.toStringAsFixed(1)}, '
'${det.box.y2.toStringAsFixed(1)}] '
'(conf=${det.confidence.toStringAsFixed(2)})';
});
}
Uint8List _buildDemoBmp(int width, int height, int r, int g, int b) {
final rowStride = ((width * 3 + 3) ~/ 4) * 4;
final pixelBytes = rowStride * height;
final fileSize = 54 + pixelBytes;
final data = ByteData(fileSize);
data.setUint8(0, 0x42);
data.setUint8(1, 0x4D);
data.setUint32(2, fileSize, Endian.little);
data.setUint32(10, 54, Endian.little);
data.setUint32(14, 40, Endian.little);
data.setInt32(18, width, Endian.little);
data.setInt32(22, height, Endian.little);
data.setUint16(26, 1, Endian.little);
data.setUint16(28, 24, Endian.little);
data.setUint32(34, pixelBytes, Endian.little);
final bytes = data.buffer.asUint8List();
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
final idx = 54 + y * rowStride + x * 3;
bytes[idx + 0] = b;
bytes[idx + 1] = g;
bytes[idx + 2] = r;
}
}
return bytes;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('edge_vision_core Demo')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_status, textAlign: TextAlign.center),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _runDemo,
child: const Text('Run C++ Vision Pipeline'),
),
],
),
),
),
),
);
}
}