Spaces:
Build error
Build error
Commit
·
8f3faa9
1
Parent(s):
153ee9b
added delete image
Browse files- app/data.py +12 -0
- app/main.py +9 -0
- instance-labeler/app/page.tsx +14 -0
app/data.py
CHANGED
|
@@ -93,6 +93,18 @@ class Data:
|
|
| 93 |
self.data[image]["labels"] = []
|
| 94 |
self._save_data()
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def get_all_images(self) -> list:
|
| 97 |
return list(self.data.keys())
|
| 98 |
|
|
|
|
| 93 |
self.data[image]["labels"] = []
|
| 94 |
self._save_data()
|
| 95 |
|
| 96 |
+
def delete_image(self, image: str) -> None:
|
| 97 |
+
if image in self.data:
|
| 98 |
+
if "image" in self.data[image]:
|
| 99 |
+
Path(self.data[image]["image"]).unlink(missing_ok=True)
|
| 100 |
+
if "emb" in self.data[image]:
|
| 101 |
+
Path(self.data[image]["emb"]).unlink(missing_ok=True)
|
| 102 |
+
if "masks" in self.data[image]:
|
| 103 |
+
for label_path in self.data[image]["masks"]:
|
| 104 |
+
Path(label_path).unlink(missing_ok=True)
|
| 105 |
+
del self.data[image]
|
| 106 |
+
self._save_data()
|
| 107 |
+
|
| 108 |
def get_all_images(self) -> list:
|
| 109 |
return list(self.data.keys())
|
| 110 |
|
app/main.py
CHANGED
|
@@ -273,6 +273,15 @@ async def upload_image(image: str, image_data: ImageData) -> Response:
|
|
| 273 |
return Response(content=image_id, media_type="text/plain")
|
| 274 |
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
@app.get("/v1/get_all_images")
|
| 277 |
async def get_all_images() -> Response:
|
| 278 |
return JSONResponse(content={"images": DATA.get_all_images()})
|
|
|
|
| 273 |
return Response(content=image_id, media_type="text/plain")
|
| 274 |
|
| 275 |
|
| 276 |
+
@app.delete("/autolabeler/v1/delete_image/{image}")
|
| 277 |
+
async def delete_image(image: str) -> Response:
|
| 278 |
+
if image not in DATA:
|
| 279 |
+
raise HTTPException(status_code=404, detail="Image not found")
|
| 280 |
+
|
| 281 |
+
DATA.delete_image(image)
|
| 282 |
+
return Response(content="deleted", media_type="text/plain")
|
| 283 |
+
|
| 284 |
+
|
| 285 |
@app.get("/v1/get_all_images")
|
| 286 |
async def get_all_images() -> Response:
|
| 287 |
return JSONResponse(content={"images": DATA.get_all_images()})
|
instance-labeler/app/page.tsx
CHANGED
|
@@ -22,6 +22,18 @@ export default function Home() {
|
|
| 22 |
setDisplayImage(`data:${contentType};base64,${res.data}`);
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
const handleFileUpload = async (e: ChangeEvent<HTMLInputElement>) => {
|
| 26 |
const reader = new FileReader();
|
| 27 |
reader.addEventListener("load", async () => {
|
|
@@ -66,6 +78,8 @@ export default function Home() {
|
|
| 66 |
}}>Next</button>
|
| 67 |
</div>
|
| 68 |
<br />
|
|
|
|
|
|
|
| 69 |
<input type="file" id="image" accept="image/png, image/jpeg" onChange={handleFileUpload} />
|
| 70 |
</>
|
| 71 |
)
|
|
|
|
| 22 |
setDisplayImage(`data:${contentType};base64,${res.data}`);
|
| 23 |
}
|
| 24 |
|
| 25 |
+
const handleFileDelete = async (imageName: string) => {
|
| 26 |
+
try {
|
| 27 |
+
const res = await axios.delete(`v1/delete_image/${imageName}`);
|
| 28 |
+
getAllImages();
|
| 29 |
+
setDisplayIndex(0);
|
| 30 |
+
setDisplayImage('/loading.png');
|
| 31 |
+
} catch (err) {
|
| 32 |
+
console.log(err);
|
| 33 |
+
alert('Error deleting image');
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
const handleFileUpload = async (e: ChangeEvent<HTMLInputElement>) => {
|
| 38 |
const reader = new FileReader();
|
| 39 |
reader.addEventListener("load", async () => {
|
|
|
|
| 78 |
}}>Next</button>
|
| 79 |
</div>
|
| 80 |
<br />
|
| 81 |
+
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-2"
|
| 82 |
+
onClick={() => handleFileDelete(images[displayIndex])}>Delete Image</button>
|
| 83 |
<input type="file" id="image" accept="image/png, image/jpeg" onChange={handleFileUpload} />
|
| 84 |
</>
|
| 85 |
)
|