analysis_screen.dart 12 KB

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