feat: bulk import photos into face gallery

This commit is contained in:
tian 2026-05-08 09:58:22 +08:00
parent c65da1299e
commit 981731e2ce
2 changed files with 45 additions and 0 deletions

View File

@ -663,6 +663,7 @@ func (u *UI) Routes() (chi.Router, error) {
r.Get("/diagnostics", u.pageDiagnostics) r.Get("/diagnostics", u.pageDiagnostics)
r.Get("/alarms", u.pageAlarms) r.Get("/alarms", u.pageAlarms)
r.Get("/face-gallery", u.pageFaceGallery) r.Get("/face-gallery", u.pageFaceGallery)
r.Post("/face-gallery/import", u.actionFaceGalleryImport)
r.Post("/face-gallery/add", u.actionFaceGalleryAdd) r.Post("/face-gallery/add", u.actionFaceGalleryAdd)
r.Post("/face-gallery/delete", u.actionFaceGalleryDelete) r.Post("/face-gallery/delete", u.actionFaceGalleryDelete)
r.Post("/face-gallery/rename", u.actionFaceGalleryRename) r.Post("/face-gallery/rename", u.actionFaceGalleryRename)
@ -4021,3 +4022,43 @@ func (u *UI) actionFaceGalleryRename(w http.ResponseWriter, r *http.Request) {
repo.RenamePerson(id, name) repo.RenamePerson(id, name)
http.Redirect(w, r, "/ui/face-gallery", http.StatusFound) http.Redirect(w, r, "/ui/face-gallery", http.StatusFound)
} }
func (u *UI) actionFaceGalleryImport(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(100 << 20); err != nil {
http.Error(w, "form too large", http.StatusBadRequest)
return
}
datasetDir := filepath.Join("dataset")
count := 0
for _, files := range r.MultipartForm.File {
for _, hdr := range files {
// webkitdirectory sends files with relative paths like "person_name/photo.jpg"
parts := strings.SplitN(filepath.ToSlash(hdr.Filename), "/", 2)
if len(parts) < 2 {
continue
}
personName := strings.TrimSpace(parts[0])
fileName := filepath.Base(parts[1])
if personName == "" || fileName == "" {
continue
}
personDir := filepath.Join(datasetDir, personName)
os.MkdirAll(personDir, 0o755)
file, err := hdr.Open()
if err != nil {
continue
}
dst := filepath.Join(personDir, fileName)
f, err := os.Create(dst)
if err != nil {
file.Close()
continue
}
io.Copy(f, file)
f.Close()
file.Close()
count++
}
}
http.Redirect(w, r, fmt.Sprintf("/ui/face-gallery?msg=已导入 %d 张照片", count), http.StatusFound)
}

View File

@ -6,6 +6,10 @@
<div class="form-hint">新增人员需上传照片,系统自动提取人脸特征。修改后需点击"重新生成"。</div> <div class="form-hint">新增人员需上传照片,系统自动提取人脸特征。修改后需点击"重新生成"。</div>
</div> </div>
<div class="actions compact"> <div class="actions compact">
<form method="post" action="/ui/face-gallery/import" enctype="multipart/form-data" style="display:flex;gap:8px;align-items:center">
<input type="file" name="photos" webkitdirectory multiple required style="width:200px">
<button class="btn secondary" type="submit">批量导入</button>
</form>
<form method="post" action="/ui/face-gallery/add" enctype="multipart/form-data" style="display:flex;gap:8px;align-items:center"> <form method="post" action="/ui/face-gallery/add" enctype="multipart/form-data" style="display:flex;gap:8px;align-items:center">
<input type="text" name="name" placeholder="姓名" required style="width:100px"> <input type="text" name="name" placeholder="姓名" required style="width:100px">
<input type="file" name="photo" accept="image/*" required style="width:160px"> <input type="file" name="photo" accept="image/*" required style="width:160px">