From f539f7fa143dff56378d14657c73f17c94bf5c0f Mon Sep 17 00:00:00 2001 From: RaiBP Date: Mon, 17 Jul 2023 14:16:32 +0200 Subject: [PATCH] Save button is now inactive until there are available labels in current image --- app/backend/backend.py | 171 +++++++++--------- app/frontend/my-app2/src/App.js | 8 + .../my-app2/src/components/Menu/Menu.css | 15 ++ .../my-app2/src/components/Menu/Menu.js | 11 +- 4 files changed, 119 insertions(+), 86 deletions(-) diff --git a/app/backend/backend.py b/app/backend/backend.py index 85ad057..98483e7 100644 --- a/app/backend/backend.py +++ b/app/backend/backend.py @@ -157,112 +157,113 @@ def get_saved_masks_and_labels(self, image_id, dataset_id): return masks, labels def save_masks(self, masks, labels): - # Save the masks and labels in the h5 file - with h5py.File(self.user_dataset, 'a') as f: - # Check if the dataset_id group exists, if not, create it - if self.dataset_id not in f: - self._create_user_dataset() - - dataset_group = f[self.dataset_id] - - image_ids_dataset = dataset_group['image_ids'] - - # Check if the image_id already exists in the file - if self.image_id in image_ids_dataset: - - start_occur = np.where(image_ids_dataset[:] == self.image_id)[0][0] - end_occur = np.where(image_ids_dataset[:] == self.image_id)[0][-1] - len_occur = (end_occur - start_occur) + 1 - len_masks = len(masks) - init_len = dataset_group['masks'].shape[0] - new_masks = len_occur - len_masks - - if len_masks == len_occur: - for mask, label in zip(masks, labels): - # Append the mask, label, and image_id to the corresponding datasets + if len(masks) != 0 and len(labels) != 0: + # Save the masks and labels in the h5 file + with h5py.File(self.user_dataset, 'a') as f: + # Check if the dataset_id group exists, if not, create it + if self.dataset_id not in f: + self._create_user_dataset() + + dataset_group = f[self.dataset_id] + + image_ids_dataset = dataset_group['image_ids'] + + # Check if the image_id already exists in the file + if self.image_id in image_ids_dataset: + + start_occur = np.where(image_ids_dataset[:] == self.image_id)[0][0] + end_occur = np.where(image_ids_dataset[:] == self.image_id)[0][-1] + len_occur = (end_occur - start_occur) + 1 + len_masks = len(masks) + init_len = dataset_group['masks'].shape[0] + new_masks = len_occur - len_masks + + if len_masks == len_occur: + for mask, label in zip(masks, labels): + # Append the mask, label, and image_id to the corresponding datasets + mask_dataset = dataset_group['masks'] + mask_dataset[start_occur] = mask + + label_dataset = dataset_group['labels'] + label_dataset[start_occur] = label + + start_occur += 1 + + elif len_masks > len_occur: + # Resize the arrays mask_dataset = dataset_group['masks'] - mask_dataset[start_occur] = mask + mask_dataset.resize(init_len + (len_masks-len_occur), axis=0) label_dataset = dataset_group['labels'] - label_dataset[start_occur] = label + label_dataset.resize(init_len + (len_masks-len_occur), axis=0) - start_occur += 1 + image_id_dataset = dataset_group['image_ids'] + image_id_dataset.resize(init_len + (len_masks-len_occur), axis=0) - elif len_masks > len_occur: - # Resize the arrays - mask_dataset = dataset_group['masks'] - mask_dataset.resize(init_len + (len_masks-len_occur), axis=0) + # Shift all the data after the Image-ID in question to the right + for k in range(init_len-(end_occur+1)): + mask_dataset[-1-k] = mask_dataset[init_len-1-k] + label_dataset[-1-k] = label_dataset[init_len-1-k] + image_id_dataset[-1-k] = image_id_dataset[init_len-1-k] - label_dataset = dataset_group['labels'] - label_dataset.resize(init_len + (len_masks-len_occur), axis=0) + for mask, label in zip(masks, labels): + # Append the mask, label, and image_id to the corresponding datasets + mask_dataset[start_occur] = mask - image_id_dataset = dataset_group['image_ids'] - image_id_dataset.resize(init_len + (len_masks-len_occur), axis=0) + label_dataset[start_occur] = label - # Shift all the data after the Image-ID in question to the right - for k in range(init_len-(end_occur+1)): - mask_dataset[-1-k] = mask_dataset[init_len-1-k] - label_dataset[-1-k] = label_dataset[init_len-1-k] - image_id_dataset[-1-k] = image_id_dataset[init_len-1-k] + image_id_dataset[start_occur] = self.image_id - for mask, label in zip(masks, labels): - # Append the mask, label, and image_id to the corresponding datasets - mask_dataset[start_occur] = mask + start_occur += 1 + # len_masks < len_ocurr + else: + mask_dataset = dataset_group['masks'] + label_dataset = dataset_group['labels'] + image_id_dataset = dataset_group['image_ids'] - label_dataset[start_occur] = label + # Shift all the data after the Image-ID in question to the left + for k in range(init_len-(end_occur+1)): + mask_dataset[end_occur+(len_masks-len_occur)+1+k] = mask_dataset[end_occur+1+k] + label_dataset[end_occur+(len_masks-len_occur)+1+k] = label_dataset[end_occur+1+k] + image_id_dataset[end_occur+(len_masks-len_occur)+1+k] = image_id_dataset[end_occur+1+k] - image_id_dataset[start_occur] = self.image_id + for mask, label in zip(masks, labels): + # Append the mask, label, and image_id to the corresponding datasets + mask_dataset = dataset_group['masks'] + mask_dataset[start_occur] = mask - start_occur += 1 - # len_masks < len_ocurr - else: - mask_dataset = dataset_group['masks'] - label_dataset = dataset_group['labels'] - image_id_dataset = dataset_group['image_ids'] + label_dataset = dataset_group['labels'] + label_dataset[start_occur] = label - # Shift all the data after the Image-ID in question to the left - for k in range(init_len-(end_occur+1)): - mask_dataset[end_occur+(len_masks-len_occur)+1+k] = mask_dataset[end_occur+1+k] - label_dataset[end_occur+(len_masks-len_occur)+1+k] = label_dataset[end_occur+1+k] - image_id_dataset[end_occur+(len_masks-len_occur)+1+k] = image_id_dataset[end_occur+1+k] + image_id_dataset = dataset_group['image_ids'] + image_id_dataset[start_occur] = self.image_id + start_occur += 1 + + # Resize the arrays + mask_dataset.resize(init_len + (len_masks-len_occur), axis=0) + label_dataset.resize(init_len + (len_masks-len_occur), axis=0) + image_id_dataset.resize(init_len + (len_masks-len_occur), axis=0) + self.cell_counter += new_masks + else: + # Save the masks, labels, and image_ids as separate datasets for mask, label in zip(masks, labels): # Append the mask, label, and image_id to the corresponding datasets mask_dataset = dataset_group['masks'] - mask_dataset[start_occur] = mask + mask_dataset.resize(mask_dataset.shape[0] + 1, axis=0) + mask_dataset[-1] = mask label_dataset = dataset_group['labels'] - label_dataset[start_occur] = label + label_dataset.resize(label_dataset.shape[0] + 1, axis=0) + label_dataset[-1] = label image_id_dataset = dataset_group['image_ids'] - image_id_dataset[start_occur] = self.image_id - - start_occur += 1 - - # Resize the arrays - mask_dataset.resize(init_len + (len_masks-len_occur), axis=0) - label_dataset.resize(init_len + (len_masks-len_occur), axis=0) - image_id_dataset.resize(init_len + (len_masks-len_occur), axis=0) - self.cell_counter += new_masks - else: - # Save the masks, labels, and image_ids as separate datasets - for mask, label in zip(masks, labels): - # Append the mask, label, and image_id to the corresponding datasets - mask_dataset = dataset_group['masks'] - mask_dataset.resize(mask_dataset.shape[0] + 1, axis=0) - mask_dataset[-1] = mask - - label_dataset = dataset_group['labels'] - label_dataset.resize(label_dataset.shape[0] + 1, axis=0) - label_dataset[-1] = label - - image_id_dataset = dataset_group['image_ids'] - image_id_dataset.resize(image_id_dataset.shape[0] + 1, axis=0) - image_id_dataset[-1] = self.image_id - - self.cell_counter += len(masks) - # Increment the image_counter - self.image_counter += 1 + image_id_dataset.resize(image_id_dataset.shape[0] + 1, axis=0) + image_id_dataset[-1] = self.image_id + + self.cell_counter += len(masks) + # Increment the image_counter + self.image_counter += 1 def get_masks_from_polygons(self, polygons): diff --git a/app/frontend/my-app2/src/App.js b/app/frontend/my-app2/src/App.js index bd62a68..50917bf 100644 --- a/app/frontend/my-app2/src/App.js +++ b/app/frontend/my-app2/src/App.js @@ -137,6 +137,7 @@ const AnnotationArea = () => { // Preview line management const [previewLine, setPreviewLine] = useState(null) const isDrawing = React.useRef(false) + const [isClassified, setIsClassified] = useState(false) // Context Menu for Polygon-editing const [contextMenu, setContextMenu] = useState({ visible: false, x: 0, y: 0, polygonID: -1 }); @@ -263,6 +264,7 @@ async function downloadMasksAndLabels() { }) } setPolygons(transformedPolygons) + setIsClassified(false) } @@ -281,6 +283,7 @@ function classifyCallback(labels) { }); setPolygons(transformedPolygons); + setIsClassified(true) } function setImageCallback(response_json) { @@ -309,6 +312,10 @@ function classifyCallback(labels) { } transformedPolygons.push(currentPolygon) }) + setIsClassified(true) + } + else { + setIsClassified(false) } setPolygons(transformedPolygons) } @@ -603,6 +610,7 @@ function classifyCallback(labels) { onClassify={classify} onSave={saveMasksAndLabels} onDownload={download} + isClassified={isClassified} /> diff --git a/app/frontend/my-app2/src/components/Menu/Menu.css b/app/frontend/my-app2/src/components/Menu/Menu.css index 0e58931..bdddf1b 100644 --- a/app/frontend/my-app2/src/components/Menu/Menu.css +++ b/app/frontend/my-app2/src/components/Menu/Menu.css @@ -28,6 +28,21 @@ background-color: #606268; } + +.menu-button-disabled { + margin-bottom: 10px; + width: 250%; + padding: 10px 10px; + font-size: 14px; + border-radius: 5px; + border: none; + color: #fff; + transition: background-color 0.3s ease; + opacity: 0.6; /* Lower the opacity to visually indicate disabled state */ + cursor: not-allowed; /* Change the cursor to indicate unclickable */ + background-color: #9e9e9e; /* Change the background color for disabled state */ +} + .selector-container { width: 250%; margin-bottom: 20px; diff --git a/app/frontend/my-app2/src/components/Menu/Menu.js b/app/frontend/my-app2/src/components/Menu/Menu.js index 6491e36..feef8e4 100644 --- a/app/frontend/my-app2/src/components/Menu/Menu.js +++ b/app/frontend/my-app2/src/components/Menu/Menu.js @@ -71,6 +71,7 @@ function Menu({ onClassify, onSave, onDownload, + isClassified }) { return (
@@ -92,7 +93,15 @@ function Menu({ - +{isClassified ? ( + +) : ( + +)}
)