face_widget.dart 13 KB

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