live_analysis_screen.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import 'dart:io';
  2. import 'dart:ui';
  3. import 'dart:async';
  4. import 'package:flutter/material.dart';
  5. import 'package:camera/camera.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import 'package:path_provider/path_provider.dart';
  8. import 'package:path/path.dart' as p;
  9. import '../services/tflite_service.dart';
  10. import '../services/database_helper.dart';
  11. import '../models/palm_record.dart';
  12. import '../widgets/palm_bounding_box.dart';
  13. enum DetectionState { searching, locking, capturing, cooldown }
  14. class LiveAnalysisScreen extends StatefulWidget {
  15. const LiveAnalysisScreen({super.key});
  16. @override
  17. State<LiveAnalysisScreen> createState() => _LiveAnalysisScreenState();
  18. }
  19. class _LiveAnalysisScreenState extends State<LiveAnalysisScreen> {
  20. CameraController? _controller;
  21. final TfliteService _tfliteService = TfliteService();
  22. final DatabaseHelper _dbHelper = DatabaseHelper();
  23. bool _isInitialized = false;
  24. bool _isProcessing = false;
  25. List<DetectionResult>? _detections;
  26. // Detection Lock Logic
  27. DetectionState _state = DetectionState.searching;
  28. static const double _lockThreshold = 0.60;
  29. // Frame Throttle removed: Atomic Idle Lock active.
  30. final List<bool> _detectionHistory = List.filled(15, false, growable: true); // 15 frames buffer
  31. static const int _requiredHits = 5; // Target hits for momentum lock
  32. int _currentHits = 0; // Track hits for the timer
  33. Timer? _lockTimer;
  34. Timer? _cooldownTimer;
  35. double _lockProgress = 0.0;
  36. bool _showFlash = false;
  37. @override
  38. void initState() {
  39. super.initState();
  40. _initializeCamera();
  41. }
  42. Future<void> _initializeCamera() async {
  43. final status = await Permission.camera.request();
  44. if (status.isDenied) return;
  45. final cameras = await availableCameras();
  46. if (cameras.isEmpty) return;
  47. _controller = CameraController(
  48. cameras[0],
  49. ResolutionPreset.medium, // Upgraded resolution for YOLO26 performance
  50. enableAudio: false,
  51. imageFormatGroup: Platform.isAndroid ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888,
  52. );
  53. try {
  54. await _controller!.initialize();
  55. await _tfliteService.initModel();
  56. _controller!.startImageStream(_handleImageStream);
  57. if (mounted) {
  58. setState(() {
  59. _isInitialized = true;
  60. });
  61. }
  62. } catch (e) {
  63. print("Camera init error: $e");
  64. }
  65. }
  66. void _handleImageStream(CameraImage image) {
  67. // Atomic Idle Lock: Only process if the Persistent Isolate is truly idle
  68. if (_isProcessing || _tfliteService.isIsolateBusy || _state == DetectionState.capturing || _state == DetectionState.cooldown) {
  69. return;
  70. }
  71. _processStreamFrame(image);
  72. }
  73. Future<void> _processStreamFrame(CameraImage image) async {
  74. setState(() => _isProcessing = true);
  75. try {
  76. final detections = await _tfliteService.runInferenceOnStream(image);
  77. bool currentFrameHasFruit = false;
  78. if (detections.isNotEmpty) {
  79. currentFrameHasFruit = detections.any((d) => d.confidence > _lockThreshold);
  80. }
  81. // Update Sliding Window Buffer
  82. _detectionHistory.removeAt(0);
  83. _detectionHistory.add(currentFrameHasFruit);
  84. _currentHits = _detectionHistory.where((h) => h).length;
  85. if (!mounted) return;
  86. setState(() {
  87. _detections = detections;
  88. });
  89. if (_state == DetectionState.searching) {
  90. if (_currentHits >= _requiredHits) {
  91. setState(() {
  92. _state = DetectionState.locking;
  93. _lockProgress = 0.0;
  94. });
  95. _startLockTimer();
  96. }
  97. }
  98. // Removed the old strict cancel logic.
  99. // _startLockTimer now safely handles momentum drain.
  100. } catch (e) {
  101. print("Stream processing error: $e");
  102. } finally {
  103. if (mounted) {
  104. setState(() => _isProcessing = false);
  105. }
  106. }
  107. }
  108. void _startLockTimer() {
  109. _lockTimer?.cancel();
  110. const duration = Duration(milliseconds: 100);
  111. int momentumTicks = 0;
  112. _lockTimer = Timer.periodic(duration, (timer) {
  113. if (!mounted) {
  114. timer.cancel();
  115. return;
  116. }
  117. // Momentum logic: add or subtract
  118. if (_currentHits >= _requiredHits) {
  119. momentumTicks++;
  120. } else {
  121. momentumTicks--;
  122. }
  123. if (momentumTicks < 0) momentumTicks = 0;
  124. setState(() {
  125. _lockProgress = (momentumTicks / 2.0).clamp(0.0, 1.0); // 2 ticks target
  126. });
  127. if (momentumTicks >= 2) {
  128. timer.cancel();
  129. if (_state == DetectionState.locking) {
  130. _triggerCapture();
  131. }
  132. } else if (momentumTicks <= 0 && _state == DetectionState.locking) {
  133. // Complete momentum loss -> Cancel lock
  134. timer.cancel();
  135. setState(() {
  136. _state = DetectionState.searching;
  137. _lockProgress = 0.0;
  138. });
  139. }
  140. });
  141. }
  142. void _cancelLockTimer() {
  143. _lockTimer?.cancel();
  144. _lockTimer = null;
  145. }
  146. Future<void> _triggerCapture() async {
  147. setState(() {
  148. _state = DetectionState.capturing;
  149. _lockProgress = 1.0;
  150. _showFlash = true;
  151. });
  152. // Quick 200ms white flash without blocking
  153. Future.delayed(const Duration(milliseconds: 200), () {
  154. if (mounted) setState(() => _showFlash = false);
  155. });
  156. await _captureAndAnalyze();
  157. }
  158. Future<void> _captureAndAnalyze() async {
  159. if (_controller == null || !_controller!.value.isInitialized) return;
  160. // 1. Stop stream to avoid resource conflict
  161. await _controller!.stopImageStream();
  162. if (!mounted) return;
  163. try {
  164. // 2. Take high-res picture
  165. final XFile photo = await _controller!.takePicture();
  166. // 3. Run final inference on high-res
  167. final detections = await _tfliteService.runInference(photo.path);
  168. if (detections.isNotEmpty) {
  169. // 4. Archive
  170. final appDocDir = await getApplicationDocumentsDirectory();
  171. final fileName = p.basename(photo.path);
  172. final persistentPath = p.join(appDocDir.path, 'palm_live_${DateTime.now().millisecondsSinceEpoch}_$fileName');
  173. await File(photo.path).copy(persistentPath);
  174. final best = detections.first;
  175. final record = PalmRecord(
  176. imagePath: persistentPath,
  177. ripenessClass: best.className,
  178. confidence: best.confidence,
  179. timestamp: DateTime.now(),
  180. x1: best.normalizedBox.left,
  181. y1: best.normalizedBox.top,
  182. x2: best.normalizedBox.right,
  183. y2: best.normalizedBox.bottom,
  184. detections: detections.map((d) => {
  185. 'className': d.className,
  186. 'classIndex': d.classIndex,
  187. 'confidence': d.confidence,
  188. 'x1': d.normalizedBox.left,
  189. 'y1': d.normalizedBox.top,
  190. 'x2': d.normalizedBox.right,
  191. 'y2': d.normalizedBox.bottom,
  192. }).toList(),
  193. );
  194. await _dbHelper.insertRecord(record);
  195. // 5. Show result and resume camera
  196. if (mounted) {
  197. await _showResultSheet(record);
  198. _startCooldown();
  199. }
  200. } else {
  201. if (mounted) {
  202. ScaffoldMessenger.of(context).showSnackBar(
  203. const SnackBar(content: Text("No palm bunches detected in final snap."))
  204. );
  205. _startCooldown();
  206. }
  207. }
  208. } catch (e) {
  209. print("Capture error: $e");
  210. if (mounted) _startCooldown();
  211. }
  212. }
  213. void _startCooldown() {
  214. if (!mounted) return;
  215. setState(() {
  216. _state = DetectionState.cooldown;
  217. _detections = null; // Clear boxes
  218. });
  219. // Clear detection history to ignore old hits
  220. _detectionHistory.fillRange(0, _detectionHistory.length, false);
  221. _cooldownTimer?.cancel();
  222. _cooldownTimer = Timer(const Duration(seconds: 3), () {
  223. if (!mounted) return;
  224. setState(() {
  225. _state = DetectionState.searching;
  226. });
  227. _controller?.startImageStream(_handleImageStream);
  228. });
  229. }
  230. Future<void> _showResultSheet(PalmRecord record) async {
  231. Color statusColor = const Color(0xFFFF9800); // Default orange
  232. if (record.ripenessClass == 'Empty_Bunch' || record.ripenessClass == 'Abnormal') {
  233. statusColor = const Color(0xFFF44336);
  234. } else if (record.ripenessClass == 'Ripe' || record.ripenessClass == 'Overripe') {
  235. statusColor = const Color(0xFF4CAF50);
  236. }
  237. await showModalBottomSheet(
  238. context: context,
  239. isScrollControlled: true,
  240. isDismissible: false,
  241. enableDrag: false,
  242. shape: const RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(20))),
  243. builder: (context) => Container(
  244. padding: const EdgeInsets.all(24),
  245. child: Column(
  246. mainAxisSize: MainAxisSize.min,
  247. children: [
  248. Icon(Icons.check_circle, color: statusColor, size: 64),
  249. const SizedBox(height: 16),
  250. Text(record.ripenessClass, style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold)),
  251. Text("Confidence: ${(record.confidence * 100).toStringAsFixed(1)}%", style: const TextStyle(color: Colors.grey)),
  252. const SizedBox(height: 24),
  253. if (record.ripenessClass == 'Empty_Bunch' || record.ripenessClass == 'Abnormal')
  254. Container(
  255. padding: const EdgeInsets.all(12),
  256. decoration: BoxDecoration(color: Colors.red.shade50, borderRadius: BorderRadius.circular(8)),
  257. child: const Text("HEALTH ALERT: Abnormal detected!", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
  258. ),
  259. const SizedBox(height: 24),
  260. SizedBox(
  261. width: double.infinity,
  262. child: ElevatedButton(
  263. onPressed: () => Navigator.pop(context),
  264. child: const Text("Done"),
  265. ),
  266. ),
  267. ],
  268. ),
  269. ),
  270. );
  271. }
  272. @override
  273. Widget build(BuildContext context) {
  274. if (!_isInitialized || _controller == null) {
  275. return const Scaffold(body: Center(child: CircularProgressIndicator()));
  276. }
  277. final isLockedVisual = _state == DetectionState.locking || _state == DetectionState.capturing;
  278. return Scaffold(
  279. backgroundColor: Colors.black,
  280. body: Stack(
  281. children: [
  282. // Camera Preview
  283. Center(
  284. child: CameraPreview(_controller!),
  285. ),
  286. // Bounding Box Overlays
  287. if (_detections != null && _state != DetectionState.capturing && _state != DetectionState.cooldown)
  288. Positioned.fill(
  289. child: LayoutBuilder(
  290. builder: (context, constraints) {
  291. return Stack(
  292. children: _detections!
  293. .map((d) => PalmBoundingBox(
  294. normalizedRect: d.normalizedBox,
  295. label: d.className,
  296. confidence: d.confidence,
  297. constraints: constraints,
  298. isLocked: (_state == DetectionState.locking || _state == DetectionState.capturing) && d.confidence > _lockThreshold,
  299. ))
  300. .toList(),
  301. );
  302. },
  303. ),
  304. ),
  305. // Top Info Bar
  306. Positioned(
  307. top: 40,
  308. left: 20,
  309. right: 20,
  310. child: Container(
  311. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  312. decoration: BoxDecoration(
  313. color: Colors.black54,
  314. borderRadius: BorderRadius.circular(20),
  315. ),
  316. child: Row(
  317. children: [
  318. Icon(
  319. _state == DetectionState.cooldown ? Icons.pause_circle_filled :
  320. isLockedVisual ? Icons.lock : Icons.center_focus_weak,
  321. color: _state == DetectionState.cooldown ? Colors.blue :
  322. isLockedVisual ? Colors.green : Colors.yellow,
  323. ),
  324. const SizedBox(width: 8),
  325. Text(
  326. _state == DetectionState.cooldown ? "COOLDOWN" :
  327. isLockedVisual ? "LOCKING" : "SEARCHING...",
  328. style: TextStyle(
  329. color: _state == DetectionState.cooldown ? Colors.blue :
  330. isLockedVisual ? Colors.green : Colors.yellow,
  331. fontWeight: FontWeight.bold,
  332. ),
  333. ),
  334. const Spacer(),
  335. IconButton(
  336. icon: const Icon(Icons.close, color: Colors.white),
  337. onPressed: () => Navigator.pop(context),
  338. ),
  339. ],
  340. ),
  341. ),
  342. ),
  343. // Progress Overlay for Locking
  344. if (_state == DetectionState.locking)
  345. Positioned.fill(
  346. child: Center(
  347. child: SizedBox(
  348. width: 120,
  349. height: 120,
  350. child: TweenAnimationBuilder<double>(
  351. tween: Tween<double>(begin: 0.0, end: _lockProgress),
  352. duration: const Duration(milliseconds: 100),
  353. builder: (context, value, _) => CircularProgressIndicator(
  354. value: value,
  355. strokeWidth: 8,
  356. color: Colors.greenAccent,
  357. backgroundColor: Colors.white24,
  358. ),
  359. ),
  360. ),
  361. ),
  362. ),
  363. // White flash overlay
  364. Positioned.fill(
  365. child: IgnorePointer(
  366. child: AnimatedOpacity(
  367. opacity: _showFlash ? 1.0 : 0.0,
  368. duration: const Duration(milliseconds: 200),
  369. child: Container(color: Colors.white),
  370. ),
  371. ),
  372. ),
  373. if (_state == DetectionState.capturing && !_showFlash)
  374. Positioned.fill(
  375. child: Container(
  376. color: Colors.black45,
  377. child: const Center(
  378. child: CircularProgressIndicator(color: Colors.white),
  379. ),
  380. ),
  381. ),
  382. if (_state == DetectionState.cooldown)
  383. Positioned.fill(
  384. child: Container(
  385. color: Colors.black45,
  386. child: const Center(
  387. child: Text(
  388. "Resuming scan...",
  389. style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
  390. ),
  391. ),
  392. ),
  393. ),
  394. // Bottom Hint
  395. if (_state == DetectionState.searching)
  396. const Positioned(
  397. bottom: 40,
  398. left: 0,
  399. right: 0,
  400. child: Center(
  401. child: Text(
  402. "Hold steady to lock target",
  403. style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
  404. ),
  405. ),
  406. ),
  407. ],
  408. ),
  409. );
  410. }
  411. @override
  412. void dispose() {
  413. _lockTimer?.cancel();
  414. _cooldownTimer?.cancel();
  415. _controller?.dispose();
  416. _tfliteService.dispose();
  417. super.dispose();
  418. }
  419. }