ADD ALOT OF POLISHING AND UPDATES...

This commit is contained in:
Wowieee4
2026-05-26 05:52:12 +07:00
parent 2e3ecdf1d1
commit b8ad8df993
53 changed files with 3194 additions and 3860 deletions
@@ -1,17 +1,14 @@
// ignore_for_file: use_build_context_synchronously, deprecated_member_use
import 'dart:async';
import 'dart:convert';
import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../app/injection_container.dart';
import '../../core/ai/detection_export.dart';
import '../../core/constants/app_constants.dart';
import '../../core/network/api_client.dart';
import '../../core/services/haptic_service.dart';
import '../../core/services/location_reporter_service.dart';
@@ -36,6 +33,7 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
bool _processingFrame = false;
DateTime _lastInferenceAt = DateTime.fromMillisecondsSinceEpoch(0);
DateTime _lastAlertAt = DateTime.fromMillisecondsSinceEpoch(0);
DateTime _lastModelWarningAt = DateTime.fromMillisecondsSinceEpoch(0);
@override
void dispose() {
@@ -59,7 +57,7 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
}
setState(() {
_active = next;
_status = next ? 'Camera stream active. YOLO ready.' : 'Stopped';
_status = next ? _activeStatusText() : 'Stopped';
});
await sl<ApiClient>()
.dio
@@ -67,13 +65,30 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
sl<TtsService>().speak(next ? 'WalkGuide dimulai' : 'WalkGuide dihentikan');
}
String _activeStatusText() {
final detector = sl<YoloDetector>();
if (kIsWeb) {
return 'Camera preview aktif, tapi YOLO TFLite tidak jalan di Chrome/web. Test AI harus lewat APK Android.';
}
if (!detector.isReady) {
return detector.lastError == null
? 'YOLO model belum siap. Pastikan assets/models/yolov8n.tflite ada.'
: 'YOLO model error: ${detector.lastError}';
}
return 'Camera stream active. YOLO ready. Waiting for camera frames...';
}
Future<void> _startCamera() async {
if (_camera != null) return;
try {
final cameras = await availableCameras();
if (cameras.isEmpty) return;
final backCamera = cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.back,
orElse: () => cameras.first,
);
final controller = CameraController(
cameras.first,
backCamera,
ResolutionPreset.medium,
enableAudio: false,
imageFormatGroup: ImageFormatGroup.yuv420,
@@ -86,11 +101,13 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
try {
await controller.startImageStream(_onCameraImage);
} catch (_) {
// Preview still works; manual Demo Detect remains available.
setState(() => _status = kIsWeb
? 'Camera preview aktif, tapi image stream YOLO tidak tersedia di Chrome/web.'
: 'Camera preview aktif, tapi image stream belum tersedia.');
}
setState(() => _camera = controller);
} catch (_) {
setState(() => _status = 'Camera unavailable. Demo mode active.');
setState(() => _status = 'Camera unavailable.');
}
}
@@ -118,24 +135,28 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
}
Future<void> _runYolo(CameraImage image) async {
final detection = await sl<YoloDetector>().detect(image);
if (detection == null || !mounted) return;
final detector = sl<YoloDetector>();
final detection = await detector.detect(image, confidenceThreshold: 0.25);
if (detection == null || !mounted) {
final now = DateTime.now();
if (now.difference(_lastModelWarningAt) > const Duration(seconds: 3)) {
_lastModelWarningAt = now;
setState(() => _status = detector.isReady
? 'Scanning... ${detector.diagnosticsSummary}'
: 'YOLO model belum siap. Pastikan file model tersedia di assets/models.');
}
return;
}
await _handleDetection(detection);
}
Future<void> _simulateObstacle() async {
final detection = await sl<YoloDetector>().detectFallback();
if (detection == null) return;
await _handleDetection(detection, forceAlert: true);
}
Future<void> _handleDetection(
DetectionResult detection, {
bool forceAlert = false,
}) async {
_lastDetection = detection;
setState(() => _status =
'Obstacle: ${detection.label} ${detection.directionName} ${detection.estimatedDistance}');
'Obstacle: ${ObstacleAnalyzer.spokenLabel(detection.label)} ${detection.directionName} ${detection.estimatedDistance}');
final now = DateTime.now();
if (!forceAlert &&
@@ -187,6 +208,12 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
const Center(
child: Icon(Icons.videocam_outlined,
color: Colors.white30, size: 96)),
if (_lastDetection?.box != null)
Positioned.fill(
child: CustomPaint(
painter: _DetectionOverlayPainter(_lastDetection!),
),
),
Positioned(
top: 16,
left: 16,
@@ -199,7 +226,7 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
left: 16,
child: _Pill(
text:
'${_lastDetection!.label} ${_lastDetection!.directionName}',
'${ObstacleAnalyzer.spokenLabel(_lastDetection!.label)} ${_lastDetection!.directionName}',
color: Colors.redAccent),
),
Positioned(
@@ -223,12 +250,6 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
onPressed: _toggle,
icon: Icon(_active ? Icons.stop : Icons.play_arrow),
label: Text(_active ? 'Stop' : 'Start'))),
const SizedBox(width: 10),
Expanded(
child: OutlinedButton.icon(
onPressed: _simulateObstacle,
icon: const Icon(Icons.radar),
label: const Text('Demo Detect'))),
],
),
],
@@ -237,229 +258,74 @@ class _WalkGuideScreenState extends State<WalkGuideScreen> {
}
}
// ---------------------------------------------------------------------------
// AiBenchmarkScreen
// ---------------------------------------------------------------------------
class AiBenchmarkScreen extends StatefulWidget {
const AiBenchmarkScreen({super.key});
class _DetectionOverlayPainter extends CustomPainter {
final DetectionResult detection;
const _DetectionOverlayPainter(this.detection);
@override
State<AiBenchmarkScreen> createState() => _AiBenchmarkScreenState();
}
void paint(Canvas canvas, Size size) {
final box = detection.box;
if (box == null) return;
class _AiBenchmarkScreenState extends State<AiBenchmarkScreen> {
static const _runsKey = 'ai_benchmark_runs';
List<String> _models = const [];
String _selectedModel = AppConstants.yoloModelPath;
List<Map<String, dynamic>> _runs = const [];
bool _running = false;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final models = await _discoverTfliteModels();
final selected = await AppConstants.getSelectedYoloModelPath();
final prefs = await SharedPreferences.getInstance();
final rawRuns = prefs.getStringList(_runsKey) ?? const [];
setState(() {
_models = models.isEmpty ? [selected] : models;
_selectedModel = models.contains(selected) ? selected : _models.first;
_runs = rawRuns
.map((e) => Map<String, dynamic>.from(jsonDecode(e) as Map))
.toList()
.reversed
.toList();
});
}
Future<void> _setModel(String? value) async {
if (value == null) return;
await AppConstants.setSelectedYoloModelPath(value);
sl<YoloDetector>().dispose();
await sl<YoloDetector>().init();
setState(() => _selectedModel = value);
_snack(context, 'Model aktif: ${value.split('/').last}');
}
Future<void> _runBenchmark() async {
setState(() => _running = true);
final started = DateTime.now();
final captureMs = await _measureCapture();
final inferenceWatch = Stopwatch()..start();
String label = 'person';
String direction = 'CENTER';
String distance = 'Demo';
final modelLoaded = sl<YoloDetector>().isReady;
final detection = await sl<YoloDetector>().detectSynthetic();
if (detection != null) {
label = detection.label;
direction = detection.directionName;
distance = detection.estimatedDistance;
}
inferenceWatch.stop();
final notifWatch = Stopwatch()..start();
final text = 'Obstacle $label di $direction, jarak $distance';
notifWatch.stop();
final ttsWatch = Stopwatch()..start();
try {
await sl<TtsService>()
.speakImmediate(text)
.timeout(const Duration(seconds: 3));
} catch (_) {}
ttsWatch.stop();
final run = {
'time': started.toIso8601String(),
'model': _selectedModel,
'modelLoaded': modelLoaded,
'captureMs': captureMs,
'inferenceMs': inferenceWatch.elapsedMilliseconds,
'notificationMs': notifWatch.elapsedMicroseconds / 1000,
'ttsMs': ttsWatch.elapsedMilliseconds,
'label': label,
'direction': direction,
final sx = size.width / ObstacleAnalyzer.frameWidth;
final sy = size.height / ObstacleAnalyzer.frameHeight;
final rect = Rect.fromLTRB(
box.left * sx,
box.top * sy,
box.right * sx,
box.bottom * sy,
);
final color = switch (detection.direction) {
ObstacleDirection.center => const Color(0xFFEF4444),
ObstacleDirection.left => const Color(0xFFF59E0B),
ObstacleDirection.right => const Color(0xFFF59E0B),
};
final prefs = await SharedPreferences.getInstance();
final next = [
jsonEncode(run),
...((prefs.getStringList(_runsKey) ?? const []).take(24))
];
await prefs.setStringList(_runsKey, next);
if (mounted) {
setState(() {
_runs = [run, ..._runs].take(25).toList();
_running = false;
});
}
}
Future<int> _measureCapture() async {
final watch = Stopwatch()..start();
CameraController? controller;
try {
final cameras =
await availableCameras().timeout(const Duration(seconds: 3));
if (cameras.isNotEmpty) {
controller = CameraController(cameras.first, ResolutionPreset.low,
enableAudio: false);
await controller.initialize().timeout(const Duration(seconds: 5));
await controller.takePicture().timeout(const Duration(seconds: 5));
}
} catch (_) {
await Future<void>.delayed(const Duration(milliseconds: 16));
} finally {
await controller?.dispose();
}
watch.stop();
return watch.elapsedMilliseconds;
}
Future<void> _clearRuns() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_runsKey);
setState(() => _runs = const []);
}
@override
Widget build(BuildContext context) {
final hasRealModel = _models.any((model) => model.endsWith('.tflite'));
return _Page(
title: 'AI Benchmark',
subtitle: 'Capture, model, notification text, and TTS timing',
child: ListView(
children: [
DropdownButtonFormField<String>(
value: _selectedModel,
decoration: const InputDecoration(labelText: 'Model file'),
items: [
for (final model in _models)
DropdownMenuItem(
value: model, child: Text(model.split('/').last))
],
onChanged: _setModel,
),
if (!hasRealModel) ...[
const SizedBox(height: 10),
const _StatusBox(
success: false,
message:
'Belum ada file .tflite di assets/models. Taruh 3-5 model di folder itu, lalu restart app untuk muncul di dropdown.',
),
],
const SizedBox(height: 12),
FilledButton.icon(
onPressed: _running ? null : _runBenchmark,
icon: _running
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2))
: const Icon(Icons.play_arrow),
label: Text(_running ? 'Running benchmark...' : 'Run benchmark'),
),
const SizedBox(height: 8),
OutlinedButton.icon(
onPressed: _clearRuns,
icon: const Icon(Icons.delete_outline),
label: const Text('Clear log')),
const SizedBox(height: 16),
for (final run in _runs) _BenchmarkCard(run: run),
if (_runs.isEmpty)
const _EmptyPanel(
icon: Icons.speed,
title: 'Belum Ada Log',
message:
'Klik Run benchmark untuk mencatat waktu capture, model/inference, text notification, dan TTS.',
),
],
),
canvas.drawRRect(
RRect.fromRectAndRadius(rect, const Radius.circular(10)),
Paint()
..color = color.withValues(alpha: 0.12)
..style = PaintingStyle.fill,
);
canvas.drawRRect(
RRect.fromRectAndRadius(rect, const Radius.circular(10)),
Paint()
..color = color
..strokeWidth = 3
..style = PaintingStyle.stroke,
);
}
}
class _BenchmarkCard extends StatelessWidget {
final Map<String, dynamic> run;
const _BenchmarkCard({required this.run});
@override
Widget build(BuildContext context) {
final time = DateTime.tryParse(run['time']?.toString() ?? '')?.toLocal();
return Card(
elevation: 0,
margin: const EdgeInsets.only(bottom: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: Color(0xFFE2E8F0))),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
time == null
? 'Benchmark run'
: '${_two(time.hour)}:${_two(time.minute)}:${_two(time.second)}',
style: const TextStyle(fontWeight: FontWeight.w800)),
const SizedBox(height: 8),
Text(
'Model: ${run['model'].toString().split('/').last} (${run['modelLoaded'] == true ? 'loaded' : 'fallback'})'),
Text('Capture: ${run['captureMs']} ms'),
Text('Model/inference: ${run['inferenceMs']} ms'),
Text('Notification text: ${run['notificationMs']} ms'),
Text('TTS start: ${run['ttsMs']} ms'),
Text('Result: ${run['label']} ${run['direction']}'),
],
final label =
'${ObstacleAnalyzer.spokenLabel(detection.label)} ${(detection.confidence * 100).round()}% ${detection.directionName}';
final textPainter = TextPainter(
text: TextSpan(
text: label,
style: const TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w800,
),
),
textDirection: TextDirection.ltr,
maxLines: 1,
)..layout(maxWidth: size.width - 24);
final labelRect = Rect.fromLTWH(
rect.left.clamp(8.0, size.width - textPainter.width - 16),
(rect.top - textPainter.height - 10).clamp(8.0, size.height - 32),
textPainter.width + 14,
textPainter.height + 8,
);
canvas.drawRRect(
RRect.fromRectAndRadius(labelRect, const Radius.circular(8)),
Paint()..color = color.withValues(alpha: 0.92),
);
textPainter.paint(canvas, labelRect.topLeft + const Offset(7, 4));
}
@override
bool shouldRepaint(covariant _DetectionOverlayPainter oldDelegate) {
return oldDelegate.detection != detection;
}
}
@@ -533,88 +399,3 @@ class _Pill extends StatelessWidget {
);
}
}
class _StatusBox extends StatelessWidget {
final bool success;
final String message;
const _StatusBox({required this.success, required this.message});
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
color: success ? const Color(0xFFF0FDF4) : const Color(0xFFFEF2F2),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(message,
style: TextStyle(
color: success
? const Color(0xFF166534)
: const Color(0xFF991B1B))),
),
);
}
}
class _EmptyPanel extends StatelessWidget {
final IconData icon;
final String title;
final String message;
const _EmptyPanel(
{required this.icon, required this.title, required this.message});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
constraints: const BoxConstraints(minHeight: 180),
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: const Color(0xFFF8FAFC),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: const Color(0xFFE2E8F0))),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, color: const Color(0xFF64748B), size: 48),
const SizedBox(height: 12),
Text(title,
style:
const TextStyle(fontWeight: FontWeight.w800, fontSize: 18)),
const SizedBox(height: 6),
Text(message, textAlign: TextAlign.center),
],
),
);
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
Future<List<String>> _discoverTfliteModels() async {
try {
final manifestRaw = await rootBundle.loadString('AssetManifest.json');
final manifest = jsonDecode(manifestRaw) as Map<String, dynamic>;
final models = manifest.keys
.where((key) =>
key.startsWith('assets/models/') && key.endsWith('.tflite'))
.toList()
..sort();
return models;
} catch (_) {
return const [];
}
}
String _two(int value) => value.toString().padLeft(2, '0');
void _snack(BuildContext context, String message) {
if (context.mounted) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(message)));
}
}