face_widget.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/face_detection/detection.dart";
  12. import "package:photos/services/machine_learning/face_ml/feedback/cluster_feedback.dart";
  13. import "package:photos/services/search_service.dart";
  14. import "package:photos/theme/ente_theme.dart";
  15. import "package:photos/ui/viewer/file/no_thumbnail_widget.dart";
  16. import "package:photos/ui/viewer/people/cluster_page.dart";
  17. import "package:photos/ui/viewer/people/cropped_face_image_view.dart";
  18. import "package:photos/ui/viewer/people/people_page.dart";
  19. import "package:photos/utils/face/face_box_crop.dart";
  20. import "package:photos/utils/thumbnail_util.dart";
  21. // import "package:photos/utils/toast_util.dart";
  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 (Platform.isIOS) {
  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. Container(
  259. height: 60,
  260. width: 60,
  261. decoration: ShapeDecoration(
  262. shape: RoundedRectangleBorder(
  263. borderRadius:
  264. const BorderRadius.all(Radius.elliptical(16, 12)),
  265. side: widget.highlight
  266. ? BorderSide(
  267. color: getEnteColorScheme(context).primary700,
  268. width: 2.0,
  269. )
  270. : BorderSide.none,
  271. ),
  272. ),
  273. child: ClipRRect(
  274. borderRadius:
  275. const BorderRadius.all(Radius.elliptical(16, 12)),
  276. child: SizedBox(
  277. width: 60,
  278. height: 60,
  279. child: CroppedFaceImageView(
  280. enteFile: widget.file,
  281. face: widget.face,
  282. ),
  283. ),
  284. ),
  285. ),
  286. const SizedBox(height: 8),
  287. if (widget.person != null)
  288. Text(
  289. widget.person!.data.name.trim(),
  290. style: Theme.of(context).textTheme.bodySmall,
  291. overflow: TextOverflow.ellipsis,
  292. maxLines: 1,
  293. ),
  294. if (kDebugMode)
  295. Text(
  296. 'S: ${widget.face.score.toStringAsFixed(3)}',
  297. style: Theme.of(context).textTheme.bodySmall,
  298. maxLines: 1,
  299. ),
  300. if (kDebugMode)
  301. Text(
  302. 'B: ${widget.face.blur.toStringAsFixed(0)}',
  303. style: Theme.of(context).textTheme.bodySmall,
  304. maxLines: 1,
  305. ),
  306. if (kDebugMode)
  307. Text(
  308. 'D: ${widget.face.detection.getFaceDirection().toDirectionString()}',
  309. style: Theme.of(context).textTheme.bodySmall,
  310. maxLines: 1,
  311. ),
  312. if (kDebugMode)
  313. Text(
  314. 'Sideways: ${widget.face.detection.faceIsSideways().toString()}',
  315. style: Theme.of(context).textTheme.bodySmall,
  316. maxLines: 1,
  317. ),
  318. ],
  319. ),
  320. );
  321. },
  322. );
  323. }
  324. }
  325. void _cornerIconPressed() async {
  326. log('face widget (file info) corner icon is pressed');
  327. try {
  328. if (isJustRemoved) {
  329. await ClusterFeedbackService.instance
  330. .addFilesToCluster([widget.face.faceID], widget.clusterID!);
  331. } else {
  332. await ClusterFeedbackService.instance
  333. .removeFilesFromCluster([widget.file], widget.clusterID!);
  334. }
  335. setState(() {
  336. isJustRemoved = !isJustRemoved;
  337. });
  338. } catch (e, s) {
  339. log("removing face/file from cluster from file info widget failed: $e, \n $s");
  340. }
  341. }
  342. Future<Uint8List?> getFaceCrop() async {
  343. try {
  344. final Uint8List? cachedFace = faceCropCache.get(widget.face.faceID);
  345. if (cachedFace != null) {
  346. return cachedFace;
  347. }
  348. final faceCropCacheFile = cachedFaceCropPath(widget.face.faceID);
  349. if ((await faceCropCacheFile.exists())) {
  350. final data = await faceCropCacheFile.readAsBytes();
  351. faceCropCache.put(widget.face.faceID, data);
  352. return data;
  353. }
  354. final result = await pool.withResource(
  355. () async => await getFaceCrops(
  356. widget.file,
  357. {
  358. widget.face.faceID: widget.face.detection.box,
  359. },
  360. ),
  361. );
  362. final Uint8List? computedCrop = result?[widget.face.faceID];
  363. if (computedCrop != null) {
  364. faceCropCache.put(widget.face.faceID, computedCrop);
  365. faceCropCacheFile.writeAsBytes(computedCrop).ignore();
  366. }
  367. return computedCrop;
  368. } catch (e, s) {
  369. log(
  370. "Error getting face for faceID: ${widget.face.faceID}",
  371. error: e,
  372. stackTrace: s,
  373. );
  374. return null;
  375. }
  376. }
  377. }