feat(visualizer): implement image selection and display from grid

This commit is contained in:
2026-02-07 11:08:13 +08:00
parent aa6baa87fe
commit d6bb233651
3 changed files with 147 additions and 34 deletions

View File

@@ -1,3 +1,4 @@
import io
from typing import Any, Dict, List, Optional, Union, cast
import torch
@@ -9,6 +10,21 @@ from tqdm.auto import tqdm
from transformers import AutoImageProcessor, AutoModel
def pil_image_to_bytes(image: Image.Image, format: str = "PNG") -> bytes:
"""Convert a PIL Image to bytes in the specified format.
Args:
image: PIL Image to convert.
format: Image format (e.g., 'PNG', 'JPEG').
Returns:
bytes: The encoded image bytes.
"""
buffer = io.BytesIO()
image.save(buffer, format=format)
return buffer.getvalue()
class FeatureRetrieval:
"""Singleton feature retrieval manager for image feature extraction."""
@@ -98,7 +114,7 @@ class FeatureRetrieval:
"id": i + j,
"label": batch_labels[j],
"vector": cls_tokens[j].numpy(),
"binary": batch_imgs[j].tobytes(),
"binary": pil_image_to_bytes(batch_imgs[j]),
}
for j in range(actual_batch_size)
]