123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- import "dart:async";
- import "package:flutter/foundation.dart";
- import 'package:flutter/material.dart';
- import "package:logging/logging.dart";
- import "package:photos/core/event_bus.dart";
- import "package:photos/events/people_changed_event.dart";
- import "package:photos/extensions/stop_watch.dart";
- import "package:photos/face/db.dart";
- import "package:photos/face/model/person.dart";
- import "package:photos/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart";
- import 'package:photos/services/machine_learning/face_ml/face_ml_service.dart';
- import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
- import 'package:photos/theme/ente_theme.dart';
- import 'package:photos/ui/components/captioned_text_widget.dart';
- import 'package:photos/ui/components/expandable_menu_item_widget.dart';
- import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
- import 'package:photos/ui/settings/common_settings.dart';
- import "package:photos/utils/dialog_util.dart";
- import "package:photos/utils/local_settings.dart";
- import 'package:photos/utils/toast_util.dart';
- class FaceDebugSectionWidget extends StatefulWidget {
- const FaceDebugSectionWidget({Key? key}) : super(key: key);
- @override
- State<FaceDebugSectionWidget> createState() => _FaceDebugSectionWidgetState();
- }
- class _FaceDebugSectionWidgetState extends State<FaceDebugSectionWidget> {
- Timer? _timer;
- @override
- void initState() {
- super.initState();
- _timer = Timer.periodic(const Duration(seconds: 5), (timer) {
- setState(() {
- // Your state update logic here
- });
- });
- }
- @override
- void dispose() {
- _timer?.cancel();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return ExpandableMenuItemWidget(
- title: "Face Beta",
- selectionOptionsWidget: _getSectionOptions(context),
- leadingIcon: Icons.bug_report_outlined,
- );
- }
- Widget _getSectionOptions(BuildContext context) {
- final Logger _logger = Logger("FaceDebugSectionWidget");
- return Column(
- children: [
- MenuItemWidget(
- captionedTextWidget: FutureBuilder<Map<int, int>>(
- future: FaceMLDataDB.instance.getIndexedFileIds(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return CaptionedTextWidget(
- title: LocalSettings.instance.isFaceIndexingEnabled
- ? "Disable indexing (${snapshot.data!.length})"
- : "Enable indexing (${snapshot.data!.length})",
- );
- }
- return const SizedBox.shrink();
- },
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- try {
- final isEnabled =
- await LocalSettings.instance.toggleFaceIndexing();
- if (isEnabled) {
- FaceMlService.instance.indexAllImages().ignore();
- } else {
- FaceMlService.instance.pauseIndexing();
- }
- if (mounted) {
- setState(() {});
- }
- } catch (e, s) {
- _logger.warning('indexing failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- ),
- MenuItemWidget(
- captionedTextWidget: FutureBuilder<Map<int, int>>(
- future: FaceMLDataDB.instance.getIndexedFileIds(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return CaptionedTextWidget(
- title: LocalSettings.instance.isFaceIndexingEnabled
- ? "Disable indexing (no fetch) (${snapshot.data!.length})"
- : "Enable indexing (${snapshot.data!.length})",
- );
- }
- return const SizedBox.shrink();
- },
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- try {
- final isEnabled =
- await LocalSettings.instance.toggleFaceIndexing();
- if (isEnabled) {
- FaceMlService.instance
- .indexAllImages(withFetching: false)
- .ignore();
- } else {
- FaceMlService.instance.pauseIndexing();
- }
- if (mounted) {
- setState(() {});
- }
- } catch (e, s) {
- _logger.warning('indexing failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- ),
- MenuItemWidget(
- captionedTextWidget: FutureBuilder<int>(
- future: FaceMLDataDB.instance.getTotalFaceCount(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return CaptionedTextWidget(
- title: "${snapshot.data!} high quality faces",
- );
- }
- return const SizedBox.shrink();
- },
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- final faces75 = await FaceMLDataDB.instance
- .getTotalFaceCount(minFaceScore: 0.75);
- final faces78 = await FaceMLDataDB.instance
- .getTotalFaceCount(minFaceScore: kMinHighQualityFaceScore);
- final blurryFaceCount =
- await FaceMLDataDB.instance.getBlurryFaceCount(15);
- showShortToast(context, "$blurryFaceCount blurry faces");
- },
- ),
- // MenuItemWidget(
- // captionedTextWidget: const CaptionedTextWidget(
- // title: "Analyze file ID 25728869",
- // ),
- // pressedColor: getEnteColorScheme(context).fillFaint,
- // trailingIcon: Icons.chevron_right_outlined,
- // trailingIconIsMuted: true,
- // onTap: () async {
- // try {
- // final enteFile = await SearchService.instance.getAllFiles().then(
- // (value) => value.firstWhere(
- // (element) => element.uploadedFileID == 25728869,
- // ),
- // );
- // _logger.info(
- // 'File with ID ${enteFile.uploadedFileID} has name ${enteFile.displayName}',
- // );
- // FaceMlService.instance.isImageIndexRunning = true;
- // final result = await FaceMlService.instance
- // .analyzeImageInSingleIsolate(enteFile);
- // if (result != null) {
- // final resultJson = result.toJsonString();
- // _logger.info('result: $resultJson');
- // }
- // FaceMlService.instance.isImageIndexRunning = false;
- // } catch (e, s) {
- // _logger.severe('indexing failed ', e, s);
- // await showGenericErrorDialog(context: context, error: e);
- // } finally {
- // FaceMlService.instance.isImageIndexRunning = false;
- // }
- // },
- // ),
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Run Clustering",
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- try {
- await PersonService.instance.storeRemoteFeedback();
- await FaceMlService.instance
- .clusterAllImages(clusterInBuckets: true);
- Bus.instance.fire(PeopleChangedEvent());
- showShortToast(context, "Done");
- } catch (e, s) {
- _logger.warning('clustering failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- ),
- sectionOptionSpacing,
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Reset feedback",
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- await FaceMLDataDB.instance.dropFeedbackTables();
- Bus.instance.fire(PeopleChangedEvent());
- showShortToast(context, "Done");
- },
- ),
- sectionOptionSpacing,
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Reset feedback & clusters",
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- alwaysShowSuccessState: true,
- onTap: () async {
- await showChoiceDialog(
- context,
- title: "Are you sure?",
- body:
- "You will need to again cluster all the faces. You can drop feedback if you want to return to original cluster labels",
- firstButtonLabel: "Yes, confirm",
- firstButtonOnTap: () async {
- try {
- await FaceMLDataDB.instance.resetClusterIDs();
- await FaceMLDataDB.instance.dropClustersAndPersonTable();
- Bus.instance.fire(PeopleChangedEvent());
- showShortToast(context, "Done");
- } catch (e, s) {
- _logger.warning('reset feedback failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- );
- },
- ),
- sectionOptionSpacing,
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Drop People to clusterMapping",
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- await showChoiceDialog(
- context,
- title: "Are you sure?",
- body:
- "This won't delete the people, but will remove the mapping of people to clusters",
- firstButtonLabel: "Yes, confirm",
- firstButtonOnTap: () async {
- try {
- final List<PersonEntity> persons =
- await PersonService.instance.getPersons();
- for (final PersonEntity p in persons) {
- await PersonService.instance.deletePerson(p.remoteID);
- }
- Bus.instance.fire(PeopleChangedEvent());
- showShortToast(context, "Done");
- } catch (e, s) {
- _logger.warning('peopleToPersonMapping remove failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- );
- },
- ),
- sectionOptionSpacing,
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Drop embeddings & feedback",
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- await showChoiceDialog(
- context,
- title: "Are you sure?",
- body:
- "You will need to again re-index all the faces. You can drop feedback if you want to label again",
- firstButtonLabel: "Yes, confirm",
- firstButtonOnTap: () async {
- try {
- await FaceMLDataDB.instance
- .dropClustersAndPersonTable(faces: true);
- Bus.instance.fire(PeopleChangedEvent());
- showShortToast(context, "Done");
- } catch (e, s) {
- _logger.warning('drop feedback failed ', e, s);
- await showGenericErrorDialog(context: context, error: e);
- }
- },
- );
- },
- ),
- if (kDebugMode) sectionOptionSpacing,
- if (kDebugMode)
- MenuItemWidget(
- captionedTextWidget: FutureBuilder<Map<int, int>>(
- future: FaceMLDataDB.instance.getIndexedFileIds(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return CaptionedTextWidget(
- title: "Read embeddings for ${snapshot.data!.length} files",
- );
- }
- return const CaptionedTextWidget(
- title: "Loading...",
- );
- },
- ),
- pressedColor: getEnteColorScheme(context).fillFaint,
- trailingIcon: Icons.chevron_right_outlined,
- trailingIconIsMuted: true,
- onTap: () async {
- final EnteWatch watch = EnteWatch("read_embeddings")..start();
- final result = await FaceMLDataDB.instance.getFaceEmbeddingMap();
- watch.logAndReset('read embeddings ${result.length} ');
- showShortToast(
- context,
- "Read ${result.length} face embeddings in ${watch.elapsed.inSeconds} secs",
- );
- },
- ),
- ],
- );
- }
- }
|