2021-08-13 20:04:59 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-22 19:33:41 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-08-13 20:04:59 +02:00
|
|
|
"time"
|
|
|
|
|
2021-10-02 14:24:44 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-08-22 16:14:34 +02:00
|
|
|
"github.com/manifoldco/promptui"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-08-13 20:04:59 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2021-08-13 20:04:59 +02:00
|
|
|
)
|
|
|
|
|
2021-10-06 08:31:35 +02:00
|
|
|
// FacesCommand registers the facial recognition subcommands.
|
2021-08-13 20:04:59 +02:00
|
|
|
var FacesCommand = cli.Command{
|
|
|
|
Name: "faces",
|
2021-10-06 08:31:35 +02:00
|
|
|
Usage: "Facial recognition subcommands",
|
2021-08-13 20:04:59 +02:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "stats",
|
2021-08-15 14:14:27 +02:00
|
|
|
Usage: "Shows stats on face samples",
|
2021-08-13 20:04:59 +02:00
|
|
|
Action: facesStatsAction,
|
|
|
|
},
|
2021-08-28 15:26:26 +02:00
|
|
|
{
|
|
|
|
Name: "audit",
|
2021-09-22 19:33:41 +02:00
|
|
|
Usage: "Scans the index for issues",
|
2021-08-28 15:26:26 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "fix, f",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "fix discovered issues",
|
2021-08-28 15:26:26 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: facesAuditAction,
|
|
|
|
},
|
2021-08-15 14:14:27 +02:00
|
|
|
{
|
2021-09-22 19:33:41 +02:00
|
|
|
Name: "reset",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Removes people and faces after confirmation",
|
2021-09-22 19:33:41 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "remove all people and faces",
|
2021-09-22 19:33:41 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-15 14:14:27 +02:00
|
|
|
Action: facesResetAction,
|
|
|
|
},
|
2021-08-22 21:06:44 +02:00
|
|
|
{
|
2021-09-22 19:33:41 +02:00
|
|
|
Name: "index",
|
|
|
|
Usage: "Searches originals for faces",
|
|
|
|
ArgsUsage: "[path]",
|
|
|
|
Action: facesIndexAction,
|
2021-08-22 21:06:44 +02:00
|
|
|
},
|
2021-08-13 20:04:59 +02:00
|
|
|
{
|
2021-09-24 13:13:59 +02:00
|
|
|
Name: "update",
|
2021-09-22 19:33:41 +02:00
|
|
|
Usage: "Performs face clustering and matching",
|
2021-08-16 00:29:36 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "update all faces",
|
2021-08-16 00:29:36 +02:00
|
|
|
},
|
|
|
|
},
|
2021-09-24 13:13:59 +02:00
|
|
|
Action: facesUpdateAction,
|
2021-09-22 19:33:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "optimize",
|
|
|
|
Usage: "Optimizes face clusters",
|
|
|
|
Action: facesOptimizeAction,
|
2021-08-13 20:04:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// facesStatsAction shows stats on face embeddings.
|
|
|
|
func facesStatsAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
w := service.Faces()
|
|
|
|
|
2021-08-28 15:26:26 +02:00
|
|
|
if err := w.Stats(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2021-08-28 15:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// facesAuditAction shows stats on face embeddings.
|
|
|
|
func facesAuditAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
w := service.Faces()
|
|
|
|
|
|
|
|
if err := w.Audit(ctx.Bool("fix")); err != nil {
|
2021-08-13 20:04:59 +02:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2021-08-13 20:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 14:14:27 +02:00
|
|
|
// facesResetAction resets face clusters and matches.
|
|
|
|
func facesResetAction(ctx *cli.Context) error {
|
2021-09-22 19:33:41 +02:00
|
|
|
if ctx.Bool("force") {
|
|
|
|
return facesResetAllAction(ctx)
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
actionPrompt := promptui.Prompt{
|
|
|
|
Label: "Remove automatically recognized faces, matches, and dangling subjects?",
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := actionPrompt.Run(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 14:14:27 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
w := service.Faces()
|
|
|
|
|
|
|
|
if err := w.Reset(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2021-08-15 14:14:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
// facesResetAllAction removes all people, faces, and face markers.
|
|
|
|
func facesResetAllAction(ctx *cli.Context) error {
|
|
|
|
actionPrompt := promptui.Prompt{
|
2021-10-05 22:33:29 +02:00
|
|
|
Label: "Permanently remove all people and faces?",
|
2021-09-22 19:33:41 +02:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := actionPrompt.Run(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:06:44 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
if err := query.RemovePeopleAndFaces(); err != nil {
|
2021-08-22 21:06:44 +02:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2021-08-22 21:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
// facesIndexAction searches originals for faces.
|
|
|
|
func facesIndexAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
// Use first argument to limit scope if set.
|
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath == "" {
|
|
|
|
log.Infof("finding faces in %s", txt.Quote(conf.OriginalsPath()))
|
|
|
|
} else {
|
|
|
|
log.Infof("finding faces in %s", txt.Quote(filepath.Join(conf.OriginalsPath(), subPath)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.ReadOnly() {
|
2021-11-18 03:02:10 +01:00
|
|
|
log.Infof("config: read-only mode enabled")
|
2021-09-22 19:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var indexed fs.Done
|
|
|
|
|
|
|
|
if w := service.Index(); w != nil {
|
|
|
|
opt := photoprism.IndexOptions{
|
|
|
|
Path: subPath,
|
|
|
|
Rescan: true,
|
|
|
|
Convert: conf.Settings().Index.Convert && conf.SidecarWritable(),
|
|
|
|
Stack: true,
|
|
|
|
FacesOnly: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
indexed = w.Start(opt)
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if w := service.Purge(); w != nil {
|
2021-09-22 19:33:41 +02:00
|
|
|
opt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
|
|
|
Ignore: indexed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if files, photos, err := w.Start(opt); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else if len(files) > 0 || len(photos) > 0 {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Infof("purge: removed %s and %s", english.Plural(len(files), "file", "files"), english.Plural(len(photos), "photo", "photos"))
|
2021-09-22 19:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("indexed %s in %s", english.Plural(len(indexed), "file", "files"), elapsed)
|
2021-09-22 19:33:41 +02:00
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
// facesUpdateAction performs face clustering and matching.
|
|
|
|
func facesUpdateAction(ctx *cli.Context) error {
|
2021-08-13 20:04:59 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
opt := photoprism.FacesOptions{
|
|
|
|
Force: ctx.Bool("force"),
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.Faces()
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
if err := w.Start(opt); err != nil {
|
2021-08-13 20:04:59 +02:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2021-08-13 20:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-22 19:33:41 +02:00
|
|
|
|
|
|
|
// facesOptimizeAction optimizes existing face clusters.
|
|
|
|
func facesOptimizeAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
w := service.Faces()
|
|
|
|
|
|
|
|
if res, err := w.Optimize(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-05 19:06:05 +02:00
|
|
|
log.Infof("merged %s in %s", english.Plural(res.Merged, "face cluster", "face clusters"), elapsed)
|
2021-09-22 19:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|