add face library: face recognition, student refs, manual assign
Сервис face-id (insightface/ArcFace) ищет лица на фото групп и записей, сравнивает их с эталонными фото учеников и предлагает привязки по процентам косинусного сходства. - face-id/: FastAPI-сервис /detect, /compare, /health, ленивая загрузка модели - таблицы photo_faces + student_face_refs, face_scanned_at в фото групп и записей - настройки: face_id_enabled, face_scan_on_upload, face_id_threshold - пересчёт привязок по всем фото rematchFaces(): считает по сохранённым эмбеддингам, сервис face-id не нужен; автозапуск после добавления эталона плюс кнопка в настройках - просмотрщик фото: рамки с процентом сходства, подсказка-кандидат, ручная привязка по всем группам - бэкап/restore, sweep сиротских файлов, удаление лиц вместе с фото
This commit is contained in:
@@ -6,6 +6,8 @@ const S_PAGE_SIZE = 25;
|
||||
let sSearchTimer = null;
|
||||
let aSearchTimer = null;
|
||||
let attachGroupLoaded = false;
|
||||
let faceRefs = [];
|
||||
let facePhotos = [];
|
||||
|
||||
async function loadGroupOptions(sel, selected) {
|
||||
if (!groupOptionsLoaded) {
|
||||
@@ -245,6 +247,8 @@ function openProfileData(id) {
|
||||
profileDataId = id;
|
||||
loadGroupOptions();
|
||||
loadStudentPhotos(id);
|
||||
loadFaceRefs(id);
|
||||
loadFacePhotos(id);
|
||||
fetch(`${API}/api/students/${id}/profile`, { headers: hdr() })
|
||||
.then(r => (r.ok ? r.json() : Promise.reject(new Error('load'))))
|
||||
.then(data => fillProfileForm(data.profile, data.name, data.photo_path))
|
||||
@@ -255,6 +259,113 @@ function openProfileData(id) {
|
||||
function closeProfileData() {
|
||||
document.getElementById('profileDataModal').classList.remove('open');
|
||||
profileDataId = null;
|
||||
faceRefs = [];
|
||||
facePhotos = [];
|
||||
}
|
||||
|
||||
async function loadFaceRefs(id) {
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${id}/face-refs`, { headers: hdr() });
|
||||
if (!res.ok) {
|
||||
renderFaceRefs();
|
||||
return;
|
||||
}
|
||||
const data = await res.json();
|
||||
faceRefs = data.refs || [];
|
||||
renderFaceRefs();
|
||||
} catch {
|
||||
faceRefs = [];
|
||||
renderFaceRefs();
|
||||
}
|
||||
}
|
||||
|
||||
function renderFaceRefs() {
|
||||
const wrap = document.getElementById('faceRefsList');
|
||||
if (!wrap) return;
|
||||
if (!faceRefs.length) {
|
||||
wrap.innerHTML = '<span class="face-empty">Эталонов пока нет — распознавание по этому ученику работать не будет</span>';
|
||||
return;
|
||||
}
|
||||
wrap.innerHTML = faceRefs.map(r => `
|
||||
<div class="face-ref">
|
||||
<img src="${esc(thumbSrc(r.photo_path))}" alt="" loading="lazy">
|
||||
<button type="button" class="pf-del" data-face-ref-del="${r.id}" title="Удалить эталон">✕</button>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function addFaceRef(file) {
|
||||
if (!profileDataId || !file) return;
|
||||
if (!file.type.startsWith('image/')) {
|
||||
showToast('Можно загружать только изображения');
|
||||
return;
|
||||
}
|
||||
showToast('Ищем лицо на фото...');
|
||||
const fd = new FormData();
|
||||
fd.append('photo', file);
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${profileDataId}/face-refs`, { method: 'POST', headers: hdr(), body: fd });
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
showToast(data.error || 'Не удалось добавить эталон');
|
||||
return;
|
||||
}
|
||||
faceRefs.push(data);
|
||||
renderFaceRefs();
|
||||
showToast('Эталон добавлен');
|
||||
} catch {
|
||||
showToast('Не удалось добавить эталон');
|
||||
}
|
||||
}
|
||||
|
||||
async function delFaceRef(refId) {
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${profileDataId}/face-refs/${refId}`, { method: 'DELETE', headers: hdr() });
|
||||
if (!res.ok) {
|
||||
showToast('Не удалось удалить эталон');
|
||||
return;
|
||||
}
|
||||
faceRefs = faceRefs.filter(r => r.id !== refId);
|
||||
renderFaceRefs();
|
||||
} catch {
|
||||
showToast('Не удалось удалить эталон');
|
||||
}
|
||||
}
|
||||
|
||||
async function loadFacePhotos(id) {
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${id}/face-photos`, { headers: hdr() });
|
||||
if (!res.ok) {
|
||||
renderFacePhotos();
|
||||
return;
|
||||
}
|
||||
const data = await res.json();
|
||||
facePhotos = data.photos || [];
|
||||
renderFacePhotos();
|
||||
} catch {
|
||||
facePhotos = [];
|
||||
renderFacePhotos();
|
||||
}
|
||||
}
|
||||
|
||||
function facePhotoHref(p) {
|
||||
if (p.source_type === 'entry') return `journal.html#entry-${p.source_id}`;
|
||||
return 'photos.html';
|
||||
}
|
||||
|
||||
function renderFacePhotos() {
|
||||
const wrap = document.getElementById('facePhotosList');
|
||||
if (!wrap) return;
|
||||
if (!facePhotos.length) {
|
||||
wrap.innerHTML = '<span class="face-empty">Пока нет фото, где распознан этот ученик</span>';
|
||||
return;
|
||||
}
|
||||
wrap.innerHTML = facePhotos.map((p, i) => `
|
||||
<div class="face-shot">
|
||||
<img src="${esc(thumbSrc(p.path))}" data-face-shot="${i}" alt="" loading="lazy">
|
||||
<a class="face-src" href="${facePhotoHref(p)}">${p.source_type === 'entry' ? 'Запись' : 'Группа'}</a>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function loadStudentPhotos(id) {
|
||||
@@ -640,6 +751,29 @@ document.addEventListener('paste', e => {
|
||||
});
|
||||
document.getElementById('profileDataModal').addEventListener('click', e => { if (e.target === e.currentTarget) closeProfileData(); });
|
||||
|
||||
document.getElementById('faceRefAddBtn').addEventListener('click', () => document.getElementById('faceRefFile').click());
|
||||
document.getElementById('faceRefFile').addEventListener('change', e => {
|
||||
const f = e.target.files[0];
|
||||
e.target.value = '';
|
||||
addFaceRef(f);
|
||||
});
|
||||
document.getElementById('faceRefsList').addEventListener('click', e => {
|
||||
const btn = e.target.closest('[data-face-ref-del]');
|
||||
if (!btn) return;
|
||||
delFaceRef(parseInt(btn.dataset.faceRefDel, 10));
|
||||
});
|
||||
document.getElementById('facePhotosList').addEventListener('click', e => {
|
||||
const img = e.target.closest('[data-face-shot]');
|
||||
if (!img) return;
|
||||
const p = facePhotos[parseInt(img.dataset.faceShot, 10)];
|
||||
if (!p) return;
|
||||
const m = document.getElementById('imgModal');
|
||||
const s = document.getElementById('imgModalSrc');
|
||||
if (!m || !s) return;
|
||||
s.src = `${API}${p.path}`;
|
||||
m.classList.add('open');
|
||||
});
|
||||
|
||||
(async () => {
|
||||
if (await checkAuth()) {
|
||||
buildSidebar(document.body.dataset.page);
|
||||
|
||||
Reference in New Issue
Block a user