DanJChong's picture
Upload folder using huggingface_hub
4cb9a15 verified
# ==================== visualization/image_viewer.py ====================
"""Image loading and display functionality"""
import pandas as pd
from PIL import Image
import requests
from io import BytesIO
from typing import Optional, Tuple
class ImageViewer:
"""Handles image loading and display"""
@staticmethod
def load_image_from_url(url: str, max_size: Tuple[int, int] = (200, 200)) -> Optional[Image.Image]:
"""Load and resize image from URL"""
try:
response = requests.get(url, timeout=10)
img = Image.open(BytesIO(response.content))
img.thumbnail(max_size, Image.Resampling.LANCZOS)
return img
except Exception as e:
print(f"Error loading image {url}: {e}")
placeholder = Image.new('RGB', (200, 200), color='gray')
return placeholder
@staticmethod
def get_image_pair(data: pd.DataFrame, row_index: int) -> Tuple[Optional[Image.Image], Optional[Image.Image]]:
"""Get image pair for a specific row
Returns:
tuple: (img1, img2)
- img1: Image for image_1
- img2: Image for image_2
"""
if row_index >= len(data):
return None, None
row = data.iloc[row_index]
# Simply load the URLs - no swapping needed since CSV is now fixed
img1_url = row.get('stim_1', '')
img2_url = row.get('stim_2', '')
img1 = ImageViewer.load_image_from_url(img1_url) if img1_url else None
img2 = ImageViewer.load_image_from_url(img2_url) if img2_url else None
return img1, img2