faces_item_widget.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import "package:flutter/material.dart";
  2. import "package:logging/logging.dart";
  3. import "package:photos/face/db.dart";
  4. import "package:photos/face/model/face.dart";
  5. import "package:photos/face/model/person.dart";
  6. import "package:photos/models/file/file.dart";
  7. import "package:photos/ui/components/buttons/chip_button_widget.dart";
  8. import "package:photos/ui/components/info_item_widget.dart";
  9. import "package:photos/ui/viewer/file_details/face_widget.dart";
  10. class FacesItemWidget extends StatelessWidget {
  11. final EnteFile file;
  12. const FacesItemWidget(this.file, {super.key});
  13. @override
  14. Widget build(BuildContext context) {
  15. return InfoItemWidget(
  16. key: const ValueKey("Faces"),
  17. leadingIcon: Icons.face_retouching_natural_outlined,
  18. subtitleSection: _faceWidgets(context, file),
  19. hasChipButtons: true,
  20. );
  21. }
  22. Future<List<Widget>> _faceWidgets(
  23. BuildContext context,
  24. EnteFile file,
  25. ) async {
  26. try {
  27. if (file.uploadedFileID == null) {
  28. return [
  29. const ChipButtonWidget(
  30. "File not uploaded yet",
  31. noChips: true,
  32. ),
  33. ];
  34. }
  35. final List<Face> faces = await FaceMLDataDB.instance
  36. .getFacesForGivenFileID(file.uploadedFileID!);
  37. if (faces.isEmpty || faces.every((face) => face.score < 0.5)) {
  38. return [
  39. const ChipButtonWidget(
  40. "No faces found",
  41. noChips: true,
  42. ),
  43. ];
  44. }
  45. // Sort the faces by score in descending order, so that the highest scoring face is first.
  46. faces.sort((Face a, Face b) => b.score.compareTo(a.score));
  47. // TODO: add deduplication of faces of same person
  48. final faceIdsToClusterIds = await FaceMLDataDB.instance
  49. .getFaceIdsToClusterIds(faces.map((face) => face.faceID));
  50. final (clusterIDToPerson, personIdToPerson) =
  51. await FaceMLDataDB.instance.getClusterIdToPerson();
  52. final faceWidgets = <FaceWidget>[];
  53. for (final Face face in faces) {
  54. final int? clusterID = faceIdsToClusterIds[face.faceID];
  55. final Person? person = clusterIDToPerson[clusterID];
  56. faceWidgets.add(
  57. FaceWidget(
  58. file,
  59. face,
  60. clusterID: clusterID,
  61. person: person,
  62. ),
  63. );
  64. }
  65. return faceWidgets;
  66. } catch (e, s) {
  67. Logger("FacesItemWidget").info(e, s);
  68. return <FaceWidget>[];
  69. }
  70. }
  71. }