objects_item_widget.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import "package:flutter/material.dart";
  2. import "package:logging/logging.dart";
  3. import "package:photos/models/file.dart";
  4. import "package:photos/services/object_detection/object_detection_service.dart";
  5. import "package:photos/ui/components/buttons/chip_button_widget.dart";
  6. import "package:photos/ui/components/info_item_widget.dart";
  7. import "package:photos/utils/thumbnail_util.dart";
  8. class ObjectsItemWidget extends StatelessWidget {
  9. final File file;
  10. const ObjectsItemWidget(this.file, {super.key});
  11. @override
  12. Widget build(BuildContext context) {
  13. return InfoItemWidget(
  14. key: const ValueKey("Objects"),
  15. leadingIcon: Icons.image_search_outlined,
  16. title: "Objects",
  17. subtitleSection: _objectTags(file),
  18. hasChipButtons: true,
  19. );
  20. }
  21. Future<List<ChipButtonWidget>> _objectTags(File file) async {
  22. try {
  23. final chipButtons = <ChipButtonWidget>[];
  24. var objectTags = <String>[];
  25. final thumbnail = await getThumbnail(file);
  26. if (thumbnail != null) {
  27. objectTags = await ObjectDetectionService.instance.predict(thumbnail);
  28. }
  29. if (objectTags.isEmpty) {
  30. return const [
  31. ChipButtonWidget(
  32. "No results",
  33. noChips: true,
  34. )
  35. ];
  36. }
  37. for (String objectTag in objectTags) {
  38. chipButtons.add(ChipButtonWidget(objectTag));
  39. }
  40. return chipButtons;
  41. } catch (e, s) {
  42. Logger("ObjctsItemWidget").info(e, s);
  43. return [];
  44. }
  45. }
  46. }