face_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. Text(
  172. 'A: ${widget.face.detection.getFaceArea(widget.file.width, widget.file.height)}',
  173. style: Theme.of(context).textTheme.bodySmall,
  174. maxLines: 1,
  175. ),
  176. // if (kDebugMode)
  177. // if (highlight)
  178. // const Text(
  179. // "Highlighted",
  180. // style: TextStyle(
  181. // color: Colors.red,
  182. // fontSize: 12,
  183. // ),
  184. // ),
  185. ],
  186. ),
  187. );
  188. } else {
  189. if (snapshot.connectionState == ConnectionState.waiting) {
  190. return const ClipRRect(
  191. borderRadius: BorderRadius.all(Radius.elliptical(16, 12)),
  192. child: SizedBox(
  193. width: 60, // Ensure consistent sizing
  194. height: 60,
  195. child: CircularProgressIndicator(),
  196. ),
  197. );
  198. }
  199. if (snapshot.hasError) {
  200. log('Error getting face: ${snapshot.error}');
  201. }
  202. return const ClipRRect(
  203. borderRadius: BorderRadius.all(Radius.elliptical(16, 12)),
  204. child: SizedBox(
  205. width: 60, // Ensure consistent sizing
  206. height: 60,
  207. child: NoThumbnailWidget(),
  208. ),
  209. );
  210. }
  211. },
  212. );
  213. } else {
  214. return Builder(
  215. builder: (context) {
  216. return GestureDetector(
  217. onTap: () async {
  218. log(
  219. "FaceWidget is tapped, with person ${widget.person} and clusterID ${widget.clusterID}",
  220. name: "FaceWidget",
  221. );
  222. if (widget.person == null && widget.clusterID == null) {
  223. return;
  224. }
  225. if (widget.person != null) {
  226. await Navigator.of(context).push(
  227. MaterialPageRoute(
  228. builder: (context) => PeoplePage(
  229. person: widget.person!,
  230. ),
  231. ),
  232. );
  233. } else if (widget.clusterID != null) {
  234. final fileIdsToClusterIds =
  235. await FaceMLDataDB.instance.getFileIdToClusterIds();
  236. final files = await SearchService.instance.getAllFiles();
  237. final clusterFiles = files
  238. .where(
  239. (file) =>
  240. fileIdsToClusterIds[file.uploadedFileID]
  241. ?.contains(widget.clusterID) ??
  242. false,
  243. )
  244. .toList();
  245. await Navigator.of(context).push(
  246. MaterialPageRoute(
  247. builder: (context) => ClusterPage(
  248. clusterFiles,
  249. clusterID: widget.clusterID!,
  250. ),
  251. ),
  252. );
  253. }
  254. },
  255. child: Column(
  256. children: [
  257. Container(
  258. height: 60,
  259. width: 60,
  260. decoration: ShapeDecoration(
  261. shape: RoundedRectangleBorder(
  262. borderRadius:
  263. const BorderRadius.all(Radius.elliptical(16, 12)),
  264. side: widget.highlight
  265. ? BorderSide(
  266. color: getEnteColorScheme(context).primary700,
  267. width: 2.0,
  268. )
  269. : BorderSide.none,
  270. ),
  271. ),
  272. child: ClipRRect(
  273. borderRadius:
  274. const BorderRadius.all(Radius.elliptical(16, 12)),
  275. child: SizedBox(
  276. width: 60,
  277. height: 60,
  278. child: CroppedFaceImageView(
  279. enteFile: widget.file,
  280. face: widget.face,
  281. ),
  282. ),
  283. ),
  284. ),
  285. const SizedBox(height: 8),
  286. if (widget.person != null)
  287. Text(
  288. widget.person!.attr.name.trim(),
  289. style: Theme.of(context).textTheme.bodySmall,
  290. overflow: TextOverflow.ellipsis,
  291. maxLines: 1,
  292. ),
  293. if (kDebugMode)
  294. Text(
  295. 'S: ${widget.face.score.toStringAsFixed(3)}',
  296. style: Theme.of(context).textTheme.bodySmall,
  297. maxLines: 1,
  298. ),
  299. ],
  300. ),
  301. );
  302. },
  303. );
  304. }
  305. }
  306. void _cornerIconPressed() async {
  307. log('face widget (file info) corner icon is pressed');
  308. try {
  309. if (isJustRemoved) {
  310. await ClusterFeedbackService.instance
  311. .addFilesToCluster([widget.face.faceID], widget.clusterID!);
  312. } else {
  313. await ClusterFeedbackService.instance
  314. .removeFilesFromCluster([widget.file], widget.clusterID!);
  315. }
  316. setState(() {
  317. isJustRemoved = !isJustRemoved;
  318. });
  319. } catch (e, s) {
  320. log("removing face/file from cluster from file info widget failed: $e, \n $s");
  321. }
  322. }
  323. Future<Uint8List?> getFaceCrop() async {
  324. try {
  325. final Uint8List? cachedFace = faceCropCache.get(widget.face.faceID);
  326. if (cachedFace != null) {
  327. return cachedFace;
  328. }
  329. final faceCropCacheFile = cachedFaceCropPath(widget.face.faceID);
  330. if ((await faceCropCacheFile.exists())) {
  331. final data = await faceCropCacheFile.readAsBytes();
  332. faceCropCache.put(widget.face.faceID, data);
  333. return data;
  334. }
  335. final result = await pool.withResource(
  336. () async => await getFaceCrops(
  337. widget.file,
  338. {
  339. widget.face.faceID: widget.face.detection.box,
  340. },
  341. ),
  342. );
  343. final Uint8List? computedCrop = result?[widget.face.faceID];
  344. if (computedCrop != null) {
  345. faceCropCache.put(widget.face.faceID, computedCrop);
  346. faceCropCacheFile.writeAsBytes(computedCrop).ignore();
  347. }
  348. return computedCrop;
  349. } catch (e, s) {
  350. log(
  351. "Error getting face for faceID: ${widget.face.faceID}",
  352. error: e,
  353. stackTrace: s,
  354. );
  355. return null;
  356. }
  357. }
  358. }