Skip to content

Commit

Permalink
optimize face cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
JoinTyang committed Nov 22, 2024
1 parent 7101d21 commit a138501
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
14 changes: 14 additions & 0 deletions frontend/src/components/common/notice-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const MSG_TYPE_DELETED_FILES = 'deleted_files';
const MSG_TYPE_SAML_SSO_FAILED = 'saml_sso_failed';
const MSG_TYPE_REPO_SHARE_PERM_CHANGE = 'repo_share_perm_change';
const MSG_TYPE_REPO_SHARE_PERM_DELETE = 'repo_share_perm_delete';
const MSG_TYPE_FACE_CLUSTER = 'face_cluster';

dayjs.extend(relativeTime);

Expand Down Expand Up @@ -354,6 +355,19 @@ class NoticeItem extends React.Component {
return { avatar_url: null, notice };
}

if (noticeType === MSG_TYPE_FACE_CLUSTER) {
let repo_id = detail.repo_id;
let repo_name = detail.repo_name;

const repoURL = `${siteRoot}library/${repo_id}/${encodeURIComponent(repo_name)}/`;
const repoLink = `<a href=${repoURL} target="_blank">${Utils.HTMLescape(repo_name)}</a>`;

let notice = gettext('Your library {libraryName} has recently clustered face successfully.');
notice = notice.replace('{libraryName}', repoLink);

return { avatar_url: null, notice };
}

if (noticeType === MSG_TYPE_SAML_SSO_FAILED) {
const { error_msg } = detail;
let notice = gettext(error_msg);
Expand Down
19 changes: 6 additions & 13 deletions seahub/api2/endpoints/metadata_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from seahub.repo_metadata.models import RepoMetadata, RepoMetadataViews
from seahub.views import check_folder_permission
from seahub.repo_metadata.utils import add_init_metadata_task, gen_unique_id, init_metadata, \
get_unmodifiable_columns, can_read_metadata, init_faces, add_init_face_recognition_task, \
get_unmodifiable_columns, can_read_metadata, init_faces, \
extract_file_details, get_someone_similar_faces, remove_faces_table, FACES_SAVE_PATH
from seahub.repo_metadata.metadata_server_api import MetadataServerAPI, list_metadata_view_records
from seahub.utils.timeutils import datetime_to_isoformat_timestr
Expand Down Expand Up @@ -1132,22 +1132,12 @@ def post(self, request, repo_id):
seafile_api.mkdir_with_parents(repo_id, '/', FACES_SAVE_PATH, request.user.username)

try:
RepoMetadata.objects.enable_face_recognition(repo_id)
RepoMetadata.objects.enable_face_recognition(repo_id, request.user.username)
except Exception as e:
logger.exception(e)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')

params = {
'repo_id': repo_id,
}

try:
task_id = add_init_face_recognition_task(params=params)
except Exception as e:
logger.error(e)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')

return Response({'task_id': task_id})
return Response({'success': True})

def delete(self, request, repo_id):
# recource check
Expand Down Expand Up @@ -1182,6 +1172,9 @@ def delete(self, request, repo_id):

try:
record.face_recognition_enabled = False
record.last_face_cluster_time = None
record.face_creator = None
record.face_commit = None
record.save()
except Exception as e:
logger.error(e)
Expand Down
4 changes: 4 additions & 0 deletions seahub/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Meta:
MSG_TYPE_REPO_MINOTOR = 'repo_monitor'
MSG_TYPE_DELETED_FILES = 'deleted_files'
MSG_TYPE_SAML_SSO_FAILED = 'saml_sso_failed'
MSG_TYPE_FACE_CLUSTER = 'face_cluster'

USER_NOTIFICATION_COUNT_CACHE_PREFIX = 'USER_NOTIFICATION_COUNT_'

Expand Down Expand Up @@ -475,6 +476,9 @@ def is_deleted_files_msg(self):
def is_saml_sso_error_msg(self):
return self.msg_type == MSG_TYPE_SAML_SSO_FAILED

def is_face_cluster_msg(self):
return self.msg_type == MSG_TYPE_FACE_CLUSTER

def user_message_detail_to_dict(self):
"""Parse user message detail, returns dict contains ``message`` and
``msg_from``.
Expand Down
6 changes: 6 additions & 0 deletions seahub/notifications/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ def update_notice_detail(request, notices):
notice.detail = d
except Exception as e:
logger.error(e)
elif notice.is_face_cluster_msg():
try:
d = json.loads(notice.detail)
notice.detail = d
except Exception as e:
logger.error(e)

return notices

Expand Down
6 changes: 5 additions & 1 deletion seahub/repo_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ def enable_metadata(self, repo_id):
metadata.save()
return metadata

def enable_face_recognition(self, repo_id):
def enable_face_recognition(self, repo_id, face_creator):
metadata = self.filter(repo_id=repo_id).first()
face_recognition_enabled = True
metadata.face_recognition_enabled = face_recognition_enabled
metadata.face_creator = face_creator
metadata.save()
return metadata

Expand All @@ -61,6 +62,9 @@ class RepoMetadata(models.Model):
face_recognition_enabled = models.BooleanField(db_index=True)
from_commit = models.CharField(max_length=40)
to_commit = models.CharField(max_length=40)
last_face_cluster_time = models.DateTimeField(db_index=True, blank=True, null=True)
face_creator = models.CharField(max_length=255)
face_commit = models.CharField(max_length=40)

objects = RepoMetadataManager()

Expand Down
9 changes: 0 additions & 9 deletions seahub/repo_metadata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ def add_init_metadata_task(params):
return json.loads(resp.content)['task_id']


def add_init_face_recognition_task(params):
payload = {'exp': int(time.time()) + 300, }
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
headers = {"Authorization": "Token %s" % token}
url = urljoin(SEAFEVENTS_SERVER_URL, '/add-init-face-recognition-task')
resp = requests.get(url, params=params, headers=headers)
return json.loads(resp.content)['task_id']


def get_someone_similar_faces(faces, metadata_server_api):
from seafevents.repo_metadata.utils import METADATA_TABLE, FACES_TABLE
sql = f'SELECT `{METADATA_TABLE.columns.id.name}`, `{METADATA_TABLE.columns.parent_dir.name}`, `{METADATA_TABLE.columns.file_name.name}` FROM `{METADATA_TABLE.name}` WHERE `{METADATA_TABLE.columns.id.name}` IN ('
Expand Down

0 comments on commit a138501

Please sign in to comment.