From c59268465eed62cce60318677f74a73cf1f1bbe6 Mon Sep 17 00:00:00 2001 From: Joe Heffer Date: Fri, 14 Jun 2024 16:49:51 +0100 Subject: [PATCH] Save labels metadata to JSON file --- backend/btviewer/blueprints/label/views.py | 5 +++-- backend/btviewer/blueprints/photo/model.py | 14 ++++++++++++-- frontend/src/components/SaveMarkers.jsx | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/backend/btviewer/blueprints/label/views.py b/backend/btviewer/blueprints/label/views.py index 8dcfa40..c684d7a 100644 --- a/backend/btviewer/blueprints/label/views.py +++ b/backend/btviewer/blueprints/label/views.py @@ -46,14 +46,15 @@ def create(): # Load the selected image photo_path = flask.request.args['path'] - source = flask.request.args['source'] photo = Photo(photo_path) # Get the label data from the request + source = flask.request.args['source'] + version = flask.request.args['version'] labels = flask.request.json # Create the labels - photo.add_labels(labels, source=source) + photo.add_labels(labels, source=source, version=version) # Return a success response return flask.Response(status=HTTPStatus.CREATED) diff --git a/backend/btviewer/blueprints/photo/model.py b/backend/btviewer/blueprints/photo/model.py index be62d5f..08be7fe 100644 --- a/backend/btviewer/blueprints/photo/model.py +++ b/backend/btviewer/blueprints/photo/model.py @@ -81,15 +81,25 @@ def add_label(self, **kwargs): """ raise NotImplementedError - def add_labels(self, labels: list[dict], source: str): + def add_labels(self, labels: list[dict], source: str, version: str, indent: int = 2): label_file_path = self.label_directory.joinpath(f'{source}.json') # Make label directory self.label_directory.mkdir(exist_ok=True) + # Build our labels metadata file contents + document = { + "provenance": { + "source": source, + "version": version, + "mode": "manual" + }, + "tags": labels + } + # Save label file with label_file_path.open('w') as file: - json.dump(labels, file) + json.dump(document, file, indent=indent) app.logger.info("Labels saved to '%s'", file.name) @property diff --git a/frontend/src/components/SaveMarkers.jsx b/frontend/src/components/SaveMarkers.jsx index 06d6a18..bd940e0 100644 --- a/frontend/src/components/SaveMarkers.jsx +++ b/frontend/src/components/SaveMarkers.jsx @@ -10,7 +10,8 @@ function SaveMarkers(props) { // eslint-disable-next-line no-unused-vars const photo_path = '1970-01-01/set_A/device_1234/camera_1/20200101_094359.123456_000002.np'; const source = 'btviewer'; - const url = `/api/labels/create?path=${photo_path}&source=${source}`; + const version = '0.0.0' + const url = `/api/labels/create?path=${photo_path}&source=${source}&version=${version}`; fetch(url, { method: 'post',