face_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import "dart:developer" show log;
  2. import "dart:io" show Platform;
  3. import "dart:typed_data";
  4. import "package:flutter/cupertino.dart";
  5. import "package:flutter/foundation.dart" show kDebugMode;
  6. import "package:flutter/material.dart";
  7. import "package:photos/face/db.dart";
  8. import "package:photos/face/model/face.dart";
  9. import "package:photos/face/model/person.dart";
  10. import 'package:photos/models/file/file.dart';
  11. import "package:photos/services/machine_learning/face_ml/feedback/cluster_feedback.dart";
  12. import "package:photos/services/search_service.dart";
  13. import "package:photos/theme/ente_theme.dart";
  14. import "package:photos/ui/viewer/file/no_thumbnail_widget.dart";
  15. import "package:photos/ui/viewer/people/cluster_page.dart";
  16. import "package:photos/ui/viewer/people/cropped_face_image_view.dart";
  17. import "package:photos/ui/viewer/people/people_page.dart";
  18. import "package:photos/utils/face/face_box_crop.dart";
  19. import "package:photos/utils/thumbnail_util.dart";
  20. // import "package:photos/utils/toast_util.dart";
  21. class FaceWidget extends StatefulWidget {
  22. final EnteFile file;
  23. final Face face;
  24. final Person? person;
  25. final int? clusterID;
  26. final bool highlight;
  27. final bool editMode;
  28. const FaceWidget(
  29. this.file,
  30. this.face, {
  31. this.person,
  32. this.clusterID,
  33. this.highlight = false,
  34. this.editMode = false,
  35. Key? key,
  36. }) : super(key: key);
  37. @override
  38. State<FaceWidget> createState() => _FaceWidgetState();
  39. }
  40. class _FaceWidgetState extends State<FaceWidget> {
  41. bool isJustRemoved = false;
  42. @override
  43. Widget build(BuildContext context) {
  44. if (Platform.isIOS || Platform.isAndroid) {
  45. return FutureBuilder<Uint8List?>(
  46. future: getFaceCrop(),
  47. builder: (context, snapshot) {
  48. if (snapshot.hasData) {
  49. final ImageProvider imageProvider = MemoryImage(snapshot.data!);
  50. return GestureDetector(
  51. onTap: () async {
  52. if (widget.editMode) return;
  53. log(
  54. "FaceWidget is tapped, with person ${widget.person} and clusterID ${widget.clusterID}",
  55. name: "FaceWidget",
  56. );
  57. if (widget.person == null && widget.clusterID == null) {
  58. return;
  59. }
  60. if (widget.person != null) {
  61. await Navigator.of(context).push(
  62. MaterialPageRoute(
  63. builder: (context) => PeoplePage(
  64. person: widget.person!,
  65. ),
  66. ),
  67. );
  68. } else if (widget.clusterID != null) {
  69. final fileIdsToClusterIds =
  70. await FaceMLDataDB.instance.getFileIdToClusterIds();
  71. final files = await SearchService.instance.getAllFiles();
  72. final clusterFiles = files
  73. .where(
  74. (file) =>
  75. fileIdsToClusterIds[file.uploadedFileID]
  76. ?.contains(widget.clusterID) ??
  77. false,
  78. )
  79. .toList();
  80. await Navigator.of(context).push(
  81. MaterialPageRoute(
  82. builder: (context) => ClusterPage(
  83. clusterFiles,
  84. clusterID: widget.clusterID!,
  85. ),
  86. ),
  87. );
  88. }
  89. },
  90. child: Column(
  91. children: [
  92. Stack(
  93. children: [
  94. Container(
  95. height: 60,
  96. width: 60,
  97. decoration: ShapeDecoration(
  98. shape: RoundedRectangleBorder(
  99. borderRadius: const BorderRadius.all(
  100. Radius.elliptical(16, 12),
  101. ),
  102. side: widget.highlight
  103. ? BorderSide(
  104. color:
  105. getEnteColorScheme(context).primary700,
  106. width: 1.0,
  107. )
  108. : BorderSide.none,
  109. ),
  110. ),
  111. child: ClipRRect(
  112. borderRadius:
  113. const BorderRadius.all(Radius.elliptical(16, 12)),
  114. child: SizedBox(
  115. width: 60,
  116. height: 60,
  117. child: Image(
  118. image: imageProvider,
  119. fit: BoxFit.cover,
  120. ),
  121. ),
  122. ),
  123. ),
  124. // TODO: the edges of the green line are still not properly rounded around ClipRRect
  125. if (widget.editMode)
  126. Positioned(
  127. right: 0,
  128. top: 0,
  129. child: GestureDetector(
  130. onTap: _cornerIconPressed,
  131. child: isJustRemoved
  132. ? const Icon(
  133. CupertinoIcons.add_circled_solid,
  134. color: Colors.green,
  135. )
  136. : const Icon(
  137. Icons.cancel,
  138. color: Colors.red,
  139. ),
  140. ),
  141. ),
  142. ],
  143. ),
  144. const SizedBox(height: 8),
  145. if (widget.person != null)
  146. Text(
  147. widget.person!.attr.name.trim(),
  148. style: Theme.of(context).textTheme.bodySmall,
  149. overflow: TextOverflow.ellipsis,
  150. maxLines: 1,
  151. ),
  152. if (kDebugMode)
  153. Text(
  154. 'S: ${widget.face.score.toStringAsFixed(3)}',
  155. style: Theme.of(context).textTheme.bodySmall,
  156. maxLines: 1,
  157. ),
  158. if (kDebugMode)
  159. Text(
  160. 'B: ${widget.face.blur.toStringAsFixed(3)}',
  161. style: Theme.of(context).textTheme.bodySmall,
  162. maxLines: 1,
  163. ),
  164. if (kDebugMode)
  165. Text(
  166. 'V: ${widget.face.detection.getVisibilityScore()}',
  167. style: Theme.of(context).textTheme.bodySmall,
  168. maxLines: 1,
  169. ),
  170. // if (kDebugMode)
  171. // if (highlight)
  172. // const Text(
  173. // "Highlighted",
  174. // style: TextStyle(
  175. // color: Colors.red,
  176. // fontSize: 12,
  177. // ),
  178. // ),
  179. ],
  180. ),
  181. );
  182. } else {
  183. if (snapshot.connectionState == ConnectionState.waiting) {
  184. return const ClipRRect(
  185. borderRadius: BorderRadius.all(Radius.elliptical(16, 12)),
  186. child: SizedBox(
  187. width: 60, // Ensure consistent sizing
  188. height: 60,
  189. child: CircularProgressIndicator(),
  190. ),
  191. );
  192. }
  193. if (snapshot.hasError) {
  194. log('Error getting face: ${snapshot.error}');
  195. }
  196. return const ClipRRect(
  197. borderRadius: BorderRadius.all(Radius.elliptical(16, 12)),
  198. child: SizedBox(
  199. width: 60, // Ensure consistent sizing
  200. height: 60,
  201. child: NoThumbnailWidget(),
  202. ),
  203. );
  204. }
  205. },
  206. );
  207. } else {
  208. return Builder(
  209. builder: (context) {
  210. return GestureDetector(
  211. onTap: () async {
  212. log(
  213. "FaceWidget is tapped, with person ${widget.person} and clusterID ${widget.clusterID}",
  214. name: "FaceWidget",
  215. );
  216. if (widget.person == null && widget.clusterID == null) {
  217. return;
  218. }
  219. if (widget.person != null) {
  220. await Navigator.of(context).push(
  221. MaterialPageRoute(
  222. builder: (context) => PeoplePage(
  223. person: widget.person!,
  224. ),
  225. ),
  226. );
  227. } else if (widget.clusterID != null) {
  228. final fileIdsToClusterIds =
  229. await FaceMLDataDB.instance.getFileIdToClusterIds();
  230. final files = await SearchService.instance.getAllFiles();
  231. final clusterFiles = files
  232. .where(
  233. (file) =>
  234. fileIdsToClusterIds[file.uploadedFileID]
  235. ?.contains(widget.clusterID) ??
  236. false,
  237. )
  238. .toList();
  239. await Navigator.of(context).push(
  240. MaterialPageRoute(
  241. builder: (context) => ClusterPage(
  242. clusterFiles,
  243. clusterID: widget.clusterID!,
  244. ),
  245. ),
  246. );
  247. }
  248. },
  249. child: Column(
  250. children: [
  251. Container(
  252. height: 60,
  253. width: 60,
  254. decoration: ShapeDecoration(
  255. shape: RoundedRectangleBorder(
  256. borderRadius:
  257. const BorderRadius.all(Radius.elliptical(16, 12)),
  258. side: widget.highlight
  259. ? BorderSide(
  260. color: getEnteColorScheme(context).primary700,
  261. width: 2.0,
  262. )
  263. : BorderSide.none,
  264. ),
  265. ),
  266. child: ClipRRect(
  267. borderRadius:
  268. const BorderRadius.all(Radius.elliptical(16, 12)),
  269. child: SizedBox(
  270. width: 60,
  271. height: 60,
  272. child: CroppedFaceImageView(
  273. enteFile: widget.file,
  274. face: widget.face,
  275. ),
  276. ),
  277. ),
  278. ),
  279. const SizedBox(height: 8),
  280. if (widget.person != null)
  281. Text(
  282. widget.person!.attr.name.trim(),
  283. style: Theme.of(context).textTheme.bodySmall,
  284. overflow: TextOverflow.ellipsis,
  285. maxLines: 1,
  286. ),
  287. if (kDebugMode)
  288. Text(
  289. 'S: ${widget.face.score.toStringAsFixed(3)}',
  290. style: Theme.of(context).textTheme.bodySmall,
  291. maxLines: 1,
  292. ),
  293. ],
  294. ),
  295. );
  296. },
  297. );
  298. }
  299. }
  300. void _cornerIconPressed() async {
  301. log('face widget (file info) corner icon is pressed');
  302. try {
  303. if (isJustRemoved) {
  304. await ClusterFeedbackService.instance
  305. .addFilesToCluster([widget.face.faceID], widget.clusterID!);
  306. } else {
  307. await ClusterFeedbackService.instance
  308. .removeFilesFromCluster([widget.file], widget.clusterID!);
  309. }
  310. setState(() {
  311. isJustRemoved = !isJustRemoved;
  312. });
  313. } catch (e, s) {
  314. log("removing face/file from cluster from file info widget failed: $e, \n $s");
  315. }
  316. }
  317. Future<Uint8List?> getFaceCrop() async {
  318. try {
  319. final Uint8List? cachedFace = faceCropCache.get(widget.face.faceID);
  320. if (cachedFace != null) {
  321. return cachedFace;
  322. }
  323. final faceCropCacheFile = cachedFaceCropPath(widget.face.faceID);
  324. if ((await faceCropCacheFile.exists())) {
  325. final data = await faceCropCacheFile.readAsBytes();
  326. faceCropCache.put(widget.face.faceID, data);
  327. return data;
  328. }
  329. final result = await pool.withResource(
  330. () async => await getFaceCrops(
  331. widget.file,
  332. {
  333. widget.face.faceID: widget.face.detection.box,
  334. },
  335. ),
  336. );
  337. final Uint8List? computedCrop = result?[widget.face.faceID];
  338. if (computedCrop != null) {
  339. faceCropCache.put(widget.face.faceID, computedCrop);
  340. faceCropCacheFile.writeAsBytes(computedCrop).ignore();
  341. }
  342. return computedCrop;
  343. } catch (e, s) {
  344. log(
  345. "Error getting face for faceID: ${widget.face.faceID}",
  346. error: e,
  347. stackTrace: s,
  348. );
  349. return null;
  350. }
  351. }
  352. }