Skip to content

Commit

Permalink
Save button is now inactive until there are available labels in curre…
Browse files Browse the repository at this point in the history
…nt image
  • Loading branch information
RaiBP committed Jul 17, 2023
1 parent 0c675e1 commit f539f7f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 86 deletions.
171 changes: 86 additions & 85 deletions app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions app/frontend/my-app2/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -263,6 +264,7 @@ async function downloadMasksAndLabels() {
})
}
setPolygons(transformedPolygons)
setIsClassified(false)

}

Expand All @@ -281,6 +283,7 @@ function classifyCallback(labels) {
});

setPolygons(transformedPolygons);
setIsClassified(true)
}

function setImageCallback(response_json) {
Expand Down Expand Up @@ -309,6 +312,10 @@ function classifyCallback(labels) {
}
transformedPolygons.push(currentPolygon)
})
setIsClassified(true)
}
else {
setIsClassified(false)
}
setPolygons(transformedPolygons)
}
Expand Down Expand Up @@ -603,6 +610,7 @@ function classifyCallback(labels) {
onClassify={classify}
onSave={saveMasksAndLabels}
onDownload={download}
isClassified={isClassified}
/>
</MenuContainer>
<StageContainer>
Expand Down
15 changes: 15 additions & 0 deletions app/frontend/my-app2/src/components/Menu/Menu.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion app/frontend/my-app2/src/components/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function Menu({
onClassify,
onSave,
onDownload,
isClassified
}) {
return (
<div className="menu-container">
Expand All @@ -92,7 +93,15 @@ function Menu({
<SegmentationMethodsSelector onChange={onSegmentationMethodChange} />
<DatasetSelector onChange={onDatasetChange} />
<Button className="menu-button" onClick={onClassify}>Classify</Button>
<Button className="menu-button" onClick={onSave}>Save Masks and Labels</Button>
{isClassified ? (
<Button className="menu-button" onClick={onSave}>
Save Masks and Labels
</Button>
) : (
<Button className="menu-button-disabled" disabled>
Save Masks and Labels
</Button>
)}
<Button className="menu-button" onClick={onDownload}>Download Masks and Labels</Button>
</div>
)
Expand Down

0 comments on commit f539f7f

Please sign in to comment.