im exporting all anotaion with image and i can import them again like project now i want export labels too like project to load them again with all annotatoins this is my code "
<v-file-input label="Upload Images" accept="image/*" outlined @change="onImageUpload" multiple
prepend-icon="mdi-upload" hint="Select multiple images for annotation" persistent-hint>
code
Assistant
User
can i export labels in an other json file?
Assistant
Yes, you can export labels in a separate JSON file. To achieve this, you can create a new method specifically for exporting just the labels. This method will convert the labels array into a JSON format and prompt the user to download it as a separate file. Below is how you can implement this:
Create a New Method for Exporting Labels:
Here's how you can define the method in your Vue component:
User
and import them again
Assistant
To import labels from a separate JSON file, you'll need to add a method that reads and processes the JSON file specifically for labels. Here's how to set it up:
Create a Method for Importing Labels:
Add a new method called importLabelsFromJson to handle the import of labels:
<!-- List of Images with better spacing and highlighting -->
<!-- Style modified for scrollable list -->
<v-list class="mt-5" tile style="max-height: 650px; overflow-y: auto;">
<v-subheader class="font-weight-medium">Images</v-subheader>
<v-list-item v-for="(img, index) in images" :key="index" @click="selectImage(index)"
:class="{ 'active': selectedImageIndex === index, 'v-list-item--active': selectedImageIndex === index }"
style="cursor: pointer;" :aria-label="'Select image ' + (index + 1)">
<v-list-item-content>
<v-list-item-title>Image {{ index + 1 }} : {{ img.rectangles.length }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-col>
<!-- Main Content for Canvas and Tools -->
<v-col cols="9">
<v-row>
<v-col cols="8">
<!-- Export Button positioned logically -->
<v-btn color="primary" elevation="2" dark @click="exportToJson" class="mb-3"
aria-label="Export annotated data with images">
Export Annotations
</v-btn>
<!-- Canvas for Drawing -->
<v-card elevation="1">
<v-card-text class="pa-4">
<canvas ref="canvas" :width="canvasWidth" :height="canvasHeight" @wheel.prevent="onMouseWheel"
@mousedown="onMouseDown" @mouseup="onMouseUp" @mousemove="onMouseMove"
style="border: 1px solid #ddd;"></canvas>
</v-card-text>
</v-card>
<!-- Labels with descriptive badge -->
<v-chip-group v-if="labels.length" mandatory class="mt-3">
<v-chip v-for="(label, index) in labels" :key="label.name" @click="selectLabel(label)"
:color="selectedLabel === label.name ? label.color : ''" class="ma-2" style="border-radius: 12px;">
<v-badge :color="label.color" :content="index + 1" inline></v-badge>
<v-label class="ml-2">{{ label.name }}</v-label>
</v-chip>
</v-chip-group>
</v-col>
<!-- Rectangle List and Import -->
<v-col cols="4">
<!-- Import JSON with feedback -->
<v-file-input label="Import Annotated Data" accept=".json" outlined @change="importJsonData"
prepend-icon="mdi-folder-open" hint="Import existing annotations in JSON format"
persistent-hint></v-file-input>
<!-- Rectangle List with emphasis on active selection -->
<v-list v-if="rectangles.length" class="mt-5" tile>
<v-subheader class="font-weight-medium">Rectangles</v-subheader>
<v-list-item v-for="(rect, index) in rectangles" :key="index"
@click="toggleRectangleSelection(index, $event)"
:class="{ 'active': selectedRectangles.includes(rect), 'v-list-item--active': selectedRectangles.includes(rect) }"
style="cursor: pointer;">
<template v-slot:prepend>
<v-icon :style="{ color: rect.color }">mdi-circle</v-icon>
</template>
<template v-slot:append>
<v-btn icon @click.stop="removeRectangle(index)" color="red" variant="tonal" density="comfortable"
aria-label="Remove rectangle">
<v-icon>mdi-delete</v-icon>
</v-btn>
</template>
<v-list-item-content>
<v-list-item-title>{{ rect.label }}</v-list-item-title>
<v-list-item-subtitle>
[x: {{ rect.x.toFixed(3) }}, y: {{ rect.y.toFixed(3) }}, w: {{ rect.width.toFixed(3) }}, h:
{{ rect.height.toFixed(3) }}]
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</v-list>
</v-col>
</v-row>
<!-- Export to ZIP Button -->
<v-btn color="success" elevation="2" dark @click="exportToZip" class="my-3"
aria-label="Export annotations and images as ZIP">
Export Annotations & ZIP
</v-btn>
</v-col>
</v-row>
</v-container>
Your Vue.js component is designed for annotating images with rectangles and managing these annotations efficiently. It seems that you want to export both the annotations and labels for easier project re-import. The export functionality for annotations to JSON and ZIP format is already implemented in your code. To also include labels in your dataset export, you can follow these steps:
Modify the Export Functionality:
Update your exportToJson function to include the labels in the data you export. This will make sure that your exported JSON file contains all label information alongside the image annotations.
Handling the Import Functionality:
Adjust your importJsonData function to handle labels when importing data. You need to ensure that the label information is correctly loaded and associated with the imported images.
Here is how you can modify the relevant parts of the code:
importJsonData(event) {
const file = event.target.files[0];
if (!file) {
alert('No file selected');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = JSON.parse(e.target.result);
if (!data.images || !Array.isArray(data.images) || !data.images.every(item =>
item.imageName && item.imageSrc && Array.isArray(item.rectangles)
)) {
throw new Error('Invalid data structure');
}
// Load labels if available
if (data.labels && Array.isArray(data.labels)) {
this.labels = data.labels;
}
this.images = data.images.map(item => ({
name: item.imageName,
src: item.imageSrc,
rectangles: item.rectangles.map(rect => ({
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
label: rect.label,
labelid: rect.labelid,
color: this.getLabelColorById(rect.labelid)
}))
}));
if (this.images.length > 0) {
this.selectImage(0);
}
alert('Data imported successfully');
} catch (error) {
console.error('Error parsing JSON:', error);
alert('Failed to import data. Please check the console for more details.');
}
};
reader.readAsText(file);
},
Explanation
Export Function: The exportToJson function now includes the labels array. This ensures the exported JSON contains all the necessary information about labels, which can be used when re-importing the project.
Import Function: Modified to check the presence of a labels array in the JSON and uses it if available, ensuring the proper labels are loaded along with image annotations.
By following these changes, you ensure that your project's annotations and labels can be both exported and imported, maintaining the integrity of your project setup during transfers.
exportLabelsToJson
javascript
methods: {
// Existing methods ...
exportLabelsToJson() {
const labelData = {
labels: this.labels // Directly taking the labels array
};
const jsonData = JSON.stringify(labelData, null, 2);
const blob = new Blob([jsonData], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'labels.json'; // Name of the JSON file for labels
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
},
// Other methods ...
}
Add a Button in the Template for Exporting Labels:
Add a button in your template that calls exportLabelsToJson when clicked:
Separate Method: The exportLabelsToJson function exports only the labels array. This function creates a JSON blob from the labels data, similar to how you handle the existing export functionality.
Download Prompt: The method uses a temporary anchor element to trigger a download in the browser, similar to the approach in the exportToJson method.
Integration in UI: A new button is added to the template to allow users to trigger the label export. You can style and position this button according to your design requirements.
By separating the label export into its own function, you gain flexibility in managing and saving your labels independently from the full annotation dataset.
javascript
methods: {
// Existing methods ...
importLabelsFromJson(event) {
const file = event.target.files[0];
if (!file) {
alert('No file selected');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = JSON.parse(e.target.result);
if (!data.labels || !Array.isArray(data.labels)) {
throw new Error('Invalid label data structure');
}
this.labels = data.labels;
alert('Labels imported successfully');
} catch (error) {
console.error('Error parsing JSON:', error);
alert('Failed to import labels. Please check the console for more details.');
}
};
reader.readAsText(file);
},
// Other methods ...
}
Add a File Input in the Template for Importing Labels:
Include a file input element in your template to trigger the label import:
Import Method: importLabelsFromJson will read the selected JSON file and parse it to extract the labels. It expects the JSON structure to have a top-level labels array.
Error Handling: The method includes error handling to catch and notify you if the JSON structure is invalid.
Integration in UI: A file input is added to the UI to upload and import the labels JSON file. This is similar to the way you already handle annotation imports.
These additions allow you to maintain the flexibility of both exporting and importing labels separately, thereby managing them independently of the image annotations. This feature might be particularly useful if you're frequently updating or managing label sets across different projects.