first commit

This commit is contained in:
vl.arkhangelskii
2026-09-27 19:12:14 +03:00
commit ba3ba38fc9
318 changed files with 9377 additions and 0 deletions
@@ -0,0 +1,4 @@
import '../../domain/sign_catalog.dart';
import 'sign_definitions.dart';
final SignCatalog bundledSignCatalog = SignCatalog(signDefinitions);
File diff suppressed because it is too large Load Diff
+219
View File
@@ -0,0 +1,219 @@
// Generated by tools/fetch_drom_sign_svgs.py.
const Set<String> signImageAssets = {
'1.1',
'1.2',
'1.3.1',
'1.3.2',
'1.4.1',
'1.4.2',
'1.4.3',
'1.4.4',
'1.4.5',
'1.4.6',
'1.5',
'1.6',
'1.7',
'1.8',
'1.9',
'1.10',
'1.11.1',
'1.11.2',
'1.12.1',
'1.12.2',
'1.13',
'1.14',
'1.15',
'1.16',
'1.17',
'1.18',
'1.19',
'1.20.1',
'1.20.2',
'1.20.3',
'1.21',
'1.22',
'1.23',
'1.24',
'1.25',
'1.26',
'1.27',
'1.28',
'1.29',
'1.30',
'1.31',
'1.32',
'1.33',
'1.34.1',
'1.34.2',
'1.34.3',
'1.35',
'2.1',
'2.2',
'2.3.1',
'2.3.2',
'2.3.3',
'2.3.4',
'2.3.5',
'2.3.6',
'2.3.7',
'2.4',
'2.5',
'2.6',
'2.7',
'3.1',
'3.2',
'3.3',
'3.4',
'3.5',
'3.6',
'3.7',
'3.8',
'3.9',
'3.10',
'3.11',
'3.12',
'3.13',
'3.14',
'3.15',
'3.16',
'3.17.1',
'3.17.2',
'3.17.3',
'3.18.1',
'3.18.2',
'3.19',
'3.20',
'3.21',
'3.22',
'3.23',
'3.24',
'3.25',
'3.26',
'3.27',
'3.28',
'3.29',
'3.30',
'3.31',
'3.32',
'3.33',
'3.34',
'3.35',
'4.1.1',
'4.1.2',
'4.1.3',
'4.1.4',
'4.1.5',
'4.1.6',
'4.2.1',
'4.2.2',
'4.2.3',
'4.3',
'4.4.1',
'4.4.2',
'4.5.1',
'4.5.2',
'4.5.3',
'4.5.4',
'4.5.5',
'4.5.6',
'4.5.7',
'4.6',
'4.7',
'4.8.1',
'4.8.2',
'4.8.3',
'5.1',
'5.2',
'5.3',
'5.4',
'5.5',
'5.6',
'5.7.1',
'5.7.2',
'5.8',
'5.9',
'5.10',
'5.11.1',
'5.11.2',
'5.12.1',
'5.12.2',
'5.13.1',
'5.13.2',
'5.14.1',
'5.14.2',
'5.14.3',
'5.14.4',
'5.15.1',
'5.15.2',
'5.15.3',
'5.15.4',
'5.15.5',
'5.15.6',
'5.15.7',
'5.15.8',
'5.16',
'5.17',
'5.18',
'5.19.1',
'5.19.2',
'5.20',
'5.21',
'5.22',
'5.23.1',
'5.23.2',
'5.24.1',
'5.24.2',
'5.25',
'5.26',
'5.27',
'5.28',
'5.29',
'5.30',
'5.31',
'5.32',
'5.33',
'5.34',
'5.35',
'5.36',
'5.37',
'5.38',
'5.39',
'5.40',
'6.1',
'6.2',
'6.3.1',
'6.3.2',
'6.4',
'6.5',
'6.6',
'6.7',
'6.8.1',
'6.8.2',
'6.8.3',
'6.16',
'6.22',
'7.1',
'7.2',
'7.3',
'7.4',
'7.5',
'7.7',
'7.12',
'7.19',
'7.21',
'8.1.1',
'8.2.1',
'8.2.2',
'8.2.3',
'8.2.4',
'8.13',
'8.14',
'8.15',
'8.16',
'8.17',
'8.18',
'8.22.1',
'8.22.2',
'8.22.3',
'8.24',
};
+290
View File
@@ -0,0 +1,290 @@
import 'dart:async';
import 'dart:isolate';
import 'dart:typed_data';
import 'package:tflite_flutter/tflite_flutter.dart';
import '../../domain/letterbox.dart';
import '../../domain/norm_rect.dart';
import '../../domain/rotation.dart';
import '../../domain/yolo_decoder.dart';
import '../frame/yuv_sampler.dart';
class FrameHit {
const FrameHit({
required this.label,
required this.confidence,
required this.box,
});
final String label;
final double confidence;
final NormRect box;
}
class FrameOutcome {
const FrameOutcome({required this.hits, required this.elapsedMs});
final List<FrameHit> hits;
final int elapsedMs;
}
class DetectorWorker {
Isolate? _isolate;
SendPort? _send;
final ReceivePort _incoming = ReceivePort();
StreamSubscription<dynamic>? _subscription;
final Map<int, Completer<Object>> _pending = {};
var _nextId = 0;
var _ready = false;
bool get isReady => _ready;
Future<int> start({
required Uint8List modelBytes,
required List<String> labels,
}) async {
_isolate = await Isolate.spawn(detectorIsolateMain, _incoming.sendPort);
final sendCompleter = Completer<SendPort>();
_subscription = _incoming.listen((message) {
if (message is SendPort) {
if (!sendCompleter.isCompleted) {
sendCompleter.complete(message);
}
return;
}
_onMessage(message);
});
_send = await sendCompleter.future;
final id = _nextId++;
final pending = Completer<Object>();
_pending[id] = pending;
_send!.send(_LoadModel(id, modelBytes, labels));
final inputSize = await pending.future;
_ready = true;
return inputSize as int;
}
Future<FrameOutcome> detect({
required YuvFrame frame,
required int clockwiseDegrees,
required double confidenceThreshold,
double iouThreshold = 0.45,
}) {
final send = _send;
if (send == null || !_ready) {
return Future.error(StateError('Детектор не загружен'));
}
final id = _nextId++;
final pending = Completer<Object>();
_pending[id] = pending;
send.send(
_FrameJob(
id: id,
frame: frame,
clockwiseDegrees: clockwiseDegrees,
confidenceThreshold: confidenceThreshold,
iouThreshold: iouThreshold,
),
);
return pending.future.then((value) => value as FrameOutcome);
}
void _onMessage(Object? message) {
if (message is _Ready) {
_pending.remove(message.id)?.complete(message.inputSize);
return;
}
if (message is _FrameDone) {
_pending
.remove(message.id)
?.complete(
FrameOutcome(hits: message.hits, elapsedMs: message.elapsedMs),
);
return;
}
if (message is _Failed) {
_pending.remove(message.id)?.completeError(StateError(message.reason));
}
}
Future<void> dispose() async {
_isolate?.kill(priority: Isolate.immediate);
await _subscription?.cancel();
_incoming.close();
_isolate = null;
_send = null;
_ready = false;
}
}
class _LoadModel {
const _LoadModel(this.id, this.bytes, this.labels);
final int id;
final Uint8List bytes;
final List<String> labels;
}
class _FrameJob {
const _FrameJob({
required this.id,
required this.frame,
required this.clockwiseDegrees,
required this.confidenceThreshold,
required this.iouThreshold,
});
final int id;
final YuvFrame frame;
final int clockwiseDegrees;
final double confidenceThreshold;
final double iouThreshold;
}
class _Ready {
const _Ready(this.id, this.inputSize);
final int id;
final int inputSize;
}
class _FrameDone {
const _FrameDone(this.id, this.hits, this.elapsedMs);
final int id;
final List<FrameHit> hits;
final int elapsedMs;
}
class _Failed {
const _Failed(this.id, this.reason);
final int id;
final String reason;
}
void detectorIsolateMain(SendPort mainPort) {
final inbox = ReceivePort();
mainPort.send(inbox.sendPort);
const decoder = YoloDecoder();
final sampler = YuvSampler();
Interpreter? interpreter;
List<String> labels = const [];
var inputSize = 0;
List<int> outputShape = const [];
Uint8List? inputBytes;
Uint8List? outputBytes;
inbox.listen((message) {
if (message is _LoadModel) {
try {
final options = InterpreterOptions()..threads = 4;
final created = Interpreter.fromBuffer(message.bytes, options: options);
final inputTensor = created.getInputTensors().first;
final outputTensor = created.getOutputTensors().first;
if (inputTensor.type != TensorType.float32 ||
outputTensor.type != TensorType.float32) {
created.close();
mainPort.send(
_Failed(
message.id,
'Нужна модель float32. Экспортируйте без half и без int8.',
),
);
return;
}
final shape = inputTensor.shape;
if (shape.length != 4 || shape[3] != 3 || shape[1] != shape[2]) {
created.close();
mainPort.send(
_Failed(
message.id,
'Ожидался вход NHWC [1, N, N, 3], получено $shape.',
),
);
return;
}
interpreter = created;
labels = message.labels;
inputSize = shape[1];
outputShape = outputTensor.shape;
inputBytes = Uint8List(inputSize * inputSize * 3 * 4);
final elements = outputShape.fold<int>(1, (a, b) => a * b);
outputBytes = Uint8List(elements * 4);
mainPort.send(_Ready(message.id, inputSize));
} catch (error) {
mainPort.send(_Failed(message.id, '$error'));
}
return;
}
if (message is! _FrameJob) {
return;
}
final current = interpreter;
final input = inputBytes;
final output = outputBytes;
if (current == null || input == null || output == null) {
mainPort.send(_Failed(message.id, 'Модель не загружена'));
return;
}
try {
final started = DateTime.now().microsecondsSinceEpoch;
final floats = sampler.sample(
frame: message.frame,
clockwiseDegrees: message.clockwiseDegrees,
inputSize: inputSize,
);
input.setAll(
0,
floats.buffer.asUint8List(floats.offsetInBytes, floats.lengthInBytes),
);
current.run(input, output);
final scores = Float32List.view(output.buffer);
final boxes = decoder.decode(
data: scores,
shape: outputShape,
inputSize: inputSize,
confidenceThreshold: message.confidenceThreshold,
iouThreshold: message.iouThreshold,
);
final upright = uprightSize(
rawWidth: message.frame.width,
rawHeight: message.frame.height,
clockwiseDegrees: message.clockwiseDegrees,
);
final letterbox = Letterbox.fit(
srcWidth: upright.$1,
srcHeight: upright.$2,
inputSize: inputSize,
);
final hits = <FrameHit>[
for (final box in boxes)
FrameHit(
label: box.classIndex >= 0 && box.classIndex < labels.length
? labels[box.classIndex]
: 'class_${box.classIndex}',
confidence: box.score,
box: _toUpright(box, letterbox, upright.$1, upright.$2),
),
];
final elapsedMs =
((DateTime.now().microsecondsSinceEpoch - started) / 1000).round();
mainPort.send(_FrameDone(message.id, hits, elapsedMs));
} catch (error) {
mainPort.send(_Failed(message.id, '$error'));
}
});
}
NormRect _toUpright(YoloBox box, Letterbox letterbox, int width, int height) {
double nx(double value) => (value - letterbox.padX) / letterbox.scale / width;
double ny(double value) =>
(value - letterbox.padY) / letterbox.scale / height;
final left = nx(box.left);
final top = ny(box.top);
final right = nx(box.right);
final bottom = ny(box.bottom);
return NormRect(left, top, right - left, bottom - top).clamped();
}
+110
View File
@@ -0,0 +1,110 @@
import 'dart:typed_data';
import '../../domain/letterbox.dart';
import '../../domain/rotation.dart';
class YuvFrame {
const YuvFrame({
required this.width,
required this.height,
required this.y,
required this.u,
required this.v,
required this.yRowStride,
required this.yPixelStride,
required this.uvRowStride,
required this.uvPixelStride,
});
final int width;
final int height;
final Uint8List y;
final Uint8List u;
final Uint8List v;
final int yRowStride;
final int yPixelStride;
final int uvRowStride;
final int uvPixelStride;
}
/// Samples a camera frame directly into a square NHWC float tensor.
///
/// Padding uses 114/255, the letterbox color Ultralytics trains with.
class YuvSampler {
Float32List sample({
required YuvFrame frame,
required int clockwiseDegrees,
required int inputSize,
}) {
final upright = uprightSize(
rawWidth: frame.width,
rawHeight: frame.height,
clockwiseDegrees: clockwiseDegrees,
);
final letterbox = Letterbox.fit(
srcWidth: upright.$1,
srcHeight: upright.$2,
inputSize: inputSize,
);
final output = Float32List(inputSize * inputSize * 3);
var offset = 0;
for (var dy = 0; dy < inputSize; dy++) {
for (var dx = 0; dx < inputSize; dx++) {
final source = letterbox.uprightFromModelPixel(dx, dy);
final ux = source.$1.floor();
final uy = source.$2.floor();
if (ux < 0 || uy < 0 || ux >= upright.$1 || uy >= upright.$2) {
output[offset++] = _pad;
output[offset++] = _pad;
output[offset++] = _pad;
continue;
}
final raw = rawPixelFromUpright(
ux: ux,
uy: uy,
rawWidth: frame.width,
rawHeight: frame.height,
clockwiseDegrees: clockwiseDegrees,
);
final rgb = _rgbAt(frame, raw.$1, raw.$2);
output[offset++] = rgb.$1;
output[offset++] = rgb.$2;
output[offset++] = rgb.$3;
}
}
return output;
}
static const double _pad = 114 / 255;
(double, double, double) _rgbAt(YuvFrame frame, int x, int y) {
final yIndex = y * frame.yRowStride + x * frame.yPixelStride;
final uvX = x >> 1;
final uvY = y >> 1;
final uvIndex = uvY * frame.uvRowStride + uvX * frame.uvPixelStride;
final yp = _byte(frame.y, yIndex, 0);
final up = _byte(frame.u, uvIndex, 128) - 128;
final vp = _byte(frame.v, uvIndex, 128) - 128;
final r = _clamp8(yp + ((1436 * vp) >> 10));
final g = _clamp8(yp - ((352 * up + 731 * vp) >> 10));
final b = _clamp8(yp + ((1815 * up) >> 10));
return (r / 255, g / 255, b / 255);
}
int _byte(Uint8List plane, int index, int fallback) {
if (index < 0 || index >= plane.length) {
return fallback;
}
return plane[index];
}
int _clamp8(int value) {
if (value < 0) {
return 0;
}
if (value > 255) {
return 255;
}
return value;
}
}