Spaces:
Runtime error
Runtime error
Commit ·
5ce98c9
1
Parent(s): 719e71f
Update app to load images from Hugging Face Hub instead of local files
Browse files- app.py +37 -19
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from PIL import Image
|
|
| 11 |
import colorsys
|
| 12 |
from pathlib import Path
|
| 13 |
import matplotlib.pyplot as plt
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
# --- Utility Functions ---
|
|
@@ -18,43 +19,60 @@ import matplotlib.pyplot as plt
|
|
| 18 |
|
| 19 |
@st.cache_resource
|
| 20 |
def load_image(image_name):
|
| 21 |
-
"""Load an image from
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
img = cv.imread(str(img_path))
|
| 25 |
if img is not None:
|
| 26 |
return cv.cvtColor(img, cv.COLOR_BGR2RGB)
|
|
|
|
|
|
|
| 27 |
return None
|
| 28 |
|
| 29 |
|
| 30 |
def get_available_images(exclude_colorblind_tests=False):
|
| 31 |
-
"""Get list of available images
|
| 32 |
|
| 33 |
Args:
|
| 34 |
exclude_colorblind_tests: If True, exclude numbered test images (1-, 2-, etc.)
|
| 35 |
"""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if exclude_colorblind_tests:
|
| 42 |
# Filter out colorblind test images (those starting with a digit)
|
| 43 |
-
|
| 44 |
|
| 45 |
-
return
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
def get_colorblind_test_images():
|
| 50 |
-
"""Get only colorblind test images (numbered)."""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
def generate_gradient_image(size=256):
|
|
|
|
| 11 |
import colorsys
|
| 12 |
from pathlib import Path
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
+
from huggingface_hub import hf_hub_download, list_repo_files
|
| 15 |
|
| 16 |
|
| 17 |
# --- Utility Functions ---
|
|
|
|
| 19 |
|
| 20 |
@st.cache_resource
|
| 21 |
def load_image(image_name):
|
| 22 |
+
"""Load an image from Hugging Face Hub."""
|
| 23 |
+
try:
|
| 24 |
+
# Download image from HF Hub dataset
|
| 25 |
+
img_path = hf_hub_download(
|
| 26 |
+
repo_id="amithjkamath/exampleimages",
|
| 27 |
+
filename=image_name,
|
| 28 |
+
repo_type="dataset"
|
| 29 |
+
)
|
| 30 |
img = cv.imread(str(img_path))
|
| 31 |
if img is not None:
|
| 32 |
return cv.cvtColor(img, cv.COLOR_BGR2RGB)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.warning(f"Could not load image {image_name}: {e}")
|
| 35 |
return None
|
| 36 |
|
| 37 |
|
| 38 |
def get_available_images(exclude_colorblind_tests=False):
|
| 39 |
+
"""Get list of available images from Hugging Face Hub.
|
| 40 |
|
| 41 |
Args:
|
| 42 |
exclude_colorblind_tests: If True, exclude numbered test images (1-, 2-, etc.)
|
| 43 |
"""
|
| 44 |
+
try:
|
| 45 |
+
files = list_repo_files(
|
| 46 |
+
repo_id="amithjkamath/exampleimages",
|
| 47 |
+
repo_type="dataset"
|
| 48 |
+
)
|
| 49 |
+
# Filter for image files
|
| 50 |
+
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 51 |
+
image_files = sorted(image_files)
|
| 52 |
|
| 53 |
if exclude_colorblind_tests:
|
| 54 |
# Filter out colorblind test images (those starting with a digit)
|
| 55 |
+
image_files = [f for f in image_files if not f[0].isdigit()]
|
| 56 |
|
| 57 |
+
return image_files
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.warning(f"Could not fetch images from Hugging Face Hub: {e}")
|
| 60 |
+
return []
|
| 61 |
|
| 62 |
|
| 63 |
def get_colorblind_test_images():
|
| 64 |
+
"""Get only colorblind test images (numbered) from Hugging Face Hub."""
|
| 65 |
+
try:
|
| 66 |
+
files = list_repo_files(
|
| 67 |
+
repo_id="amithjkamath/exampleimages",
|
| 68 |
+
repo_type="dataset"
|
| 69 |
+
)
|
| 70 |
+
# Filter for image files that start with a digit
|
| 71 |
+
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg')) and f[0].isdigit()]
|
| 72 |
+
return sorted(image_files)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.warning(f"Could not fetch colorblind test images: {e}")
|
| 75 |
+
return []
|
| 76 |
|
| 77 |
|
| 78 |
def generate_gradient_image(size=256):
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ numpy==1.26.4
|
|
| 3 |
opencv-python==4.10.0.84
|
| 4 |
pillow==10.4.0
|
| 5 |
matplotlib==3.9.2
|
|
|
|
|
|
| 3 |
opencv-python==4.10.0.84
|
| 4 |
pillow==10.4.0
|
| 5 |
matplotlib==3.9.2
|
| 6 |
+
huggingface_hub==0.24.7
|