analysis_screen.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import 'dart:io';
  2. import 'dart:ui';
  3. import 'package:flutter/material.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:path/path.dart' as p;
  6. import '../services/tflite_service.dart';
  7. import '../services/database_helper.dart';
  8. import '../models/palm_record.dart';
  9. class AnalysisScreen extends StatefulWidget {
  10. const AnalysisScreen({super.key});
  11. @override
  12. State<AnalysisScreen> createState() => _AnalysisScreenState();
  13. }
  14. class _AnalysisScreenState extends State<AnalysisScreen> with SingleTickerProviderStateMixin {
  15. final TfliteService _tfliteService = TfliteService();
  16. final DatabaseHelper _dbHelper = DatabaseHelper();
  17. late AnimationController _scanningController;
  18. bool _isAnalyzing = false;
  19. File? _selectedImage;
  20. List<DetectionResult>? _detections;
  21. String? _errorMessage;
  22. @override
  23. void initState() {
  24. super.initState();
  25. _scanningController = AnimationController(
  26. vsync: this,
  27. duration: const Duration(seconds: 2),
  28. );
  29. _initModel();
  30. }
  31. Future<void> _initModel() async {
  32. try {
  33. await _tfliteService.initModel();
  34. } catch (e) {
  35. if (mounted) {
  36. setState(() {
  37. _errorMessage = "Failed to initialize AI model: $e";
  38. });
  39. }
  40. }
  41. }
  42. Future<void> _processGalleryImage() async {
  43. final image = await _tfliteService.pickImage();
  44. if (image == null) return;
  45. if (mounted) {
  46. setState(() {
  47. _isAnalyzing = true;
  48. _selectedImage = File(image.path);
  49. _detections = null;
  50. _errorMessage = null;
  51. });
  52. _scanningController.repeat(reverse: true);
  53. // Give the UI a frame to paint the loading state
  54. await Future.delayed(const Duration(milliseconds: 50));
  55. }
  56. try {
  57. // 1. Run Inference
  58. final detections = await _tfliteService.runInference(image.path);
  59. if (detections.isNotEmpty) {
  60. // 2. Persist Image to Documents Directory
  61. final appDocDir = await getApplicationDocumentsDirectory();
  62. final fileName = p.basename(image.path);
  63. final persistentPath = p.join(appDocDir.path, 'palm_${DateTime.now().millisecondsSinceEpoch}_$fileName');
  64. await File(image.path).copy(persistentPath);
  65. // 3. Update UI
  66. if (mounted) {
  67. setState(() {
  68. _detections = detections;
  69. _selectedImage = File(persistentPath);
  70. });
  71. }
  72. // 4. Save to Database
  73. final best = detections.first;
  74. final record = PalmRecord(
  75. imagePath: persistentPath,
  76. ripenessClass: best.className,
  77. confidence: best.confidence,
  78. timestamp: DateTime.now(),
  79. x1: best.normalizedBox.left,
  80. y1: best.normalizedBox.top,
  81. x2: best.normalizedBox.right,
  82. y2: best.normalizedBox.bottom,
  83. detections: detections.map((d) => {
  84. 'className': d.className,
  85. 'classIndex': d.classIndex,
  86. 'confidence': d.confidence,
  87. 'x1': d.normalizedBox.left,
  88. 'y1': d.normalizedBox.top,
  89. 'x2': d.normalizedBox.right,
  90. 'y2': d.normalizedBox.bottom,
  91. }).toList(),
  92. );
  93. await _dbHelper.insertRecord(record);
  94. } else {
  95. if (mounted) {
  96. setState(() {
  97. _errorMessage = "No palm bunches detected.";
  98. });
  99. }
  100. }
  101. } catch (e) {
  102. if (mounted) {
  103. setState(() {
  104. _errorMessage = "Analysis error: $e";
  105. });
  106. }
  107. } finally {
  108. if (mounted) {
  109. setState(() {
  110. _isAnalyzing = false;
  111. });
  112. _scanningController.stop();
  113. _scanningController.reset();
  114. }
  115. }
  116. }
  117. bool _isHealthAlert(String label) {
  118. return label == 'Empty_Bunch' || label == 'Abnormal';
  119. }
  120. @override
  121. Widget build(BuildContext context) {
  122. return Scaffold(
  123. appBar: AppBar(title: const Text('AI Analysis')),
  124. body: Stack(
  125. children: [
  126. Column(
  127. children: [
  128. Expanded(
  129. child: Center(
  130. child: _selectedImage == null
  131. ? const Text("Select a photo to start AI analysis")
  132. : AspectRatio(
  133. aspectRatio: 1.0,
  134. child: Padding(
  135. padding: const EdgeInsets.all(8.0),
  136. child: Stack(
  137. children: [
  138. // Main Image
  139. ClipRRect(
  140. borderRadius: BorderRadius.circular(12),
  141. child: Image.file(
  142. _selectedImage!,
  143. fit: BoxFit.contain,
  144. width: double.infinity,
  145. height: double.infinity,
  146. ),
  147. ),
  148. // Bounding Box Overlays
  149. if (_detections != null)
  150. Positioned.fill(
  151. child: LayoutBuilder(
  152. builder: (context, constraints) {
  153. return Stack(
  154. children: _detections!
  155. .map((d) => _buildBoundingBox(d, constraints))
  156. .toList(),
  157. );
  158. },
  159. ),
  160. ),
  161. // Scanning Animation
  162. if (_isAnalyzing)
  163. _ScanningOverlay(animation: _scanningController),
  164. ],
  165. ),
  166. ),
  167. ),
  168. ),
  169. ),
  170. if (_detections != null) _buildResultSummary(),
  171. Padding(
  172. padding: const EdgeInsets.all(32.0),
  173. child: ElevatedButton.icon(
  174. onPressed: _isAnalyzing ? null : _processGalleryImage,
  175. icon: const Icon(Icons.add_a_photo),
  176. label: const Text("Pick Image from Gallery"),
  177. style: ElevatedButton.styleFrom(
  178. minimumSize: const Size(double.infinity, 54),
  179. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
  180. ),
  181. ),
  182. ),
  183. ],
  184. ),
  185. if (_isAnalyzing) _buildLoadingOverlay(),
  186. if (_errorMessage != null) _buildErrorToast(),
  187. ],
  188. ),
  189. );
  190. }
  191. Widget _buildBoundingBox(DetectionResult detection, BoxConstraints constraints) {
  192. final rect = detection.normalizedBox;
  193. final color = _getStatusColor(detection.className);
  194. return Positioned(
  195. left: rect.left * constraints.maxWidth,
  196. top: rect.top * constraints.maxHeight,
  197. width: rect.width * constraints.maxWidth,
  198. height: rect.height * constraints.maxHeight,
  199. child: Container(
  200. decoration: BoxDecoration(
  201. border: Border.all(color: color, width: 3),
  202. borderRadius: BorderRadius.circular(4),
  203. ),
  204. child: Align(
  205. alignment: Alignment.topLeft,
  206. child: Container(
  207. padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  208. color: color,
  209. child: Text(
  210. "${detection.className} ${(detection.confidence * 100).toStringAsFixed(0)}%",
  211. style: const TextStyle(color: Colors.white, fontSize: 10, fontWeight: FontWeight.bold),
  212. ),
  213. ),
  214. ),
  215. ),
  216. );
  217. }
  218. Color _getStatusColor(String label) {
  219. if (label == 'Empty_Bunch' || label == 'Abnormal') return Colors.red;
  220. if (label == 'Ripe' || label == 'Overripe') return Colors.green;
  221. return Colors.orange;
  222. }
  223. Widget _buildResultSummary() {
  224. final best = _detections!.first;
  225. return Container(
  226. padding: const EdgeInsets.all(16),
  227. margin: const EdgeInsets.symmetric(horizontal: 16),
  228. decoration: BoxDecoration(
  229. color: Colors.white,
  230. borderRadius: BorderRadius.circular(12),
  231. boxShadow: [const BoxShadow(color: Colors.black12, blurRadius: 10)],
  232. ),
  233. child: Column(
  234. children: [
  235. Row(
  236. mainAxisAlignment: MainAxisAlignment.center,
  237. children: [
  238. Icon(Icons.check_circle, color: _getStatusColor(best.className), size: 32),
  239. const SizedBox(width: 12),
  240. Text(
  241. best.className,
  242. style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
  243. ),
  244. ],
  245. ),
  246. Text(
  247. "Confidence: ${(best.confidence * 100).toStringAsFixed(1)}%",
  248. style: const TextStyle(fontSize: 14, color: Colors.grey),
  249. ),
  250. if (_isHealthAlert(best.className))
  251. const Padding(
  252. padding: EdgeInsets.only(top: 8.0),
  253. child: Text(
  254. "HEALTH ALERT: Abnormal detected!",
  255. style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
  256. ),
  257. ),
  258. if (_detections!.length > 1)
  259. Padding(
  260. padding: const EdgeInsets.only(top: 8.0),
  261. child: Text(
  262. "${_detections!.length} bunches detected",
  263. style: const TextStyle(fontSize: 13, color: Colors.grey),
  264. ),
  265. ),
  266. ],
  267. ),
  268. );
  269. }
  270. Widget _buildLoadingOverlay() {
  271. return Positioned.fill(
  272. child: BackdropFilter(
  273. filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
  274. child: Container(
  275. color: Colors.black.withValues(alpha: 0.3),
  276. child: const Center(
  277. child: Column(
  278. mainAxisSize: MainAxisSize.min,
  279. children: [
  280. CircularProgressIndicator(
  281. color: Colors.white,
  282. strokeWidth: 6,
  283. ),
  284. SizedBox(height: 24),
  285. Text(
  286. "AI is Analyzing...",
  287. style: TextStyle(
  288. color: Colors.white,
  289. fontSize: 22,
  290. fontWeight: FontWeight.bold,
  291. letterSpacing: 1.2,
  292. ),
  293. ),
  294. Text(
  295. "Optimizing detection parameters",
  296. style: TextStyle(color: Colors.white70, fontSize: 14),
  297. ),
  298. ],
  299. ),
  300. ),
  301. ),
  302. ),
  303. );
  304. }
  305. Widget _buildErrorToast() {
  306. return Positioned(
  307. bottom: 20,
  308. left: 20,
  309. right: 20,
  310. child: Container(
  311. padding: const EdgeInsets.all(16),
  312. decoration: BoxDecoration(
  313. color: Colors.red.shade800,
  314. borderRadius: BorderRadius.circular(12),
  315. boxShadow: [const BoxShadow(color: Colors.black26, blurRadius: 8)],
  316. ),
  317. child: Column(
  318. mainAxisSize: MainAxisSize.min,
  319. crossAxisAlignment: CrossAxisAlignment.start,
  320. children: [
  321. const Row(
  322. children: [
  323. Icon(Icons.error_outline, color: Colors.white),
  324. SizedBox(width: 8),
  325. Text("Error Details", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
  326. ],
  327. ),
  328. const SizedBox(height: 8),
  329. Text(
  330. _errorMessage!,
  331. style: const TextStyle(color: Colors.white, fontSize: 12),
  332. ),
  333. const SizedBox(height: 8),
  334. TextButton(
  335. onPressed: () => setState(() => _errorMessage = null),
  336. child: const Text("Dismiss", style: TextStyle(color: Colors.white)),
  337. ),
  338. ],
  339. ),
  340. ),
  341. );
  342. }
  343. @override
  344. void dispose() {
  345. _scanningController.dispose();
  346. _tfliteService.dispose();
  347. super.dispose();
  348. }
  349. }
  350. class _ScanningOverlay extends StatelessWidget {
  351. final Animation<double> animation;
  352. const _ScanningOverlay({required this.animation});
  353. @override
  354. Widget build(BuildContext context) {
  355. return AnimatedBuilder(
  356. animation: animation,
  357. builder: (context, child) {
  358. return Stack(
  359. children: [
  360. Positioned(
  361. top: animation.value * MediaQuery.of(context).size.width, // Rough estimation since AspectRatio is 1.0
  362. left: 0,
  363. right: 0,
  364. child: Container(
  365. height: 4,
  366. decoration: BoxDecoration(
  367. gradient: LinearGradient(
  368. colors: [
  369. Colors.transparent,
  370. Colors.greenAccent.withValues(alpha: 0.8),
  371. Colors.transparent,
  372. ],
  373. ),
  374. boxShadow: [
  375. BoxShadow(
  376. color: Colors.greenAccent.withValues(alpha: 0.6),
  377. blurRadius: 15,
  378. spreadRadius: 2,
  379. ),
  380. ],
  381. ),
  382. ),
  383. ),
  384. ],
  385. );
  386. },
  387. );
  388. }
  389. }