Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files
app.py
CHANGED
|
@@ -75,47 +75,32 @@ def image_record(filename):
|
|
| 75 |
)
|
| 76 |
|
| 77 |
|
| 78 |
-
def gallery_items(db
|
| 79 |
-
"""Return
|
| 80 |
-
if db is None:
|
| 81 |
-
db = load_db()
|
| 82 |
items = []
|
| 83 |
for item in db.get("images", []):
|
| 84 |
path = Path(item.get("path", ""))
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
with Image.open(path) as im:
|
| 88 |
-
items.append((im.convert("RGB").copy(), item.get("filename", path.name)))
|
| 89 |
-
except Exception:
|
| 90 |
-
continue
|
| 91 |
return items
|
| 92 |
|
| 93 |
|
| 94 |
-
def
|
| 95 |
-
"""Get
|
| 96 |
-
if file is None:
|
| 97 |
-
return None
|
| 98 |
if isinstance(file, (str, os.PathLike)):
|
| 99 |
return Path(file)
|
| 100 |
if isinstance(file, dict):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
for attr in ("path", "name"):
|
| 109 |
-
value = getattr(file, attr, None)
|
| 110 |
-
if value:
|
| 111 |
-
candidate = Path(str(value))
|
| 112 |
-
if candidate.exists():
|
| 113 |
-
return candidate
|
| 114 |
-
return None
|
| 115 |
|
| 116 |
|
| 117 |
def upload_training_images(files):
|
| 118 |
-
"""Copy uploaded files into dataset storage and
|
| 119 |
DATA.mkdir(parents=True, exist_ok=True)
|
| 120 |
IMAGE_DIR.mkdir(parents=True, exist_ok=True)
|
| 121 |
db = load_db()
|
|
@@ -138,9 +123,9 @@ def upload_training_images(files):
|
|
| 138 |
|
| 139 |
for file in files:
|
| 140 |
try:
|
| 141 |
-
src =
|
| 142 |
-
if src
|
| 143 |
-
raise FileNotFoundError("
|
| 144 |
|
| 145 |
ext = src.suffix.lower() or ".jpg"
|
| 146 |
if ext not in {".jpg", ".jpeg", ".png", ".webp", ".bmp"}:
|
|
@@ -808,7 +793,7 @@ with gr.Blocks(title="Anything Detector", js=ANNOTATOR_JS) as demo:
|
|
| 808 |
)
|
| 809 |
upload_status = gr.Markdown()
|
| 810 |
saved_images = gr.Gallery(
|
| 811 |
-
value=gallery_items(),
|
| 812 |
label="Saved training images",
|
| 813 |
columns=5,
|
| 814 |
rows=2,
|
|
@@ -970,4 +955,5 @@ if __name__ == "__main__":
|
|
| 970 |
server_port=7860,
|
| 971 |
ssr_mode=False,
|
| 972 |
max_file_size="100mb",
|
|
|
|
| 973 |
)
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
|
| 78 |
+
def gallery_items(db):
|
| 79 |
+
"""Return Gradio Gallery items using absolute paths plus captions."""
|
|
|
|
|
|
|
| 80 |
items = []
|
| 81 |
for item in db.get("images", []):
|
| 82 |
path = Path(item.get("path", ""))
|
| 83 |
+
if path.is_file():
|
| 84 |
+
items.append((str(path), item.get("filename", path.name)))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return items
|
| 86 |
|
| 87 |
|
| 88 |
+
def normalize_uploaded_file(file):
|
| 89 |
+
"""Get a real local path from Gradio filepath/FileData variants."""
|
|
|
|
|
|
|
| 90 |
if isinstance(file, (str, os.PathLike)):
|
| 91 |
return Path(file)
|
| 92 |
if isinstance(file, dict):
|
| 93 |
+
value = file.get("path") or file.get("name") or file.get("url")
|
| 94 |
+
if value and not str(value).startswith(("http://", "https://")):
|
| 95 |
+
return Path(str(value))
|
| 96 |
+
value = getattr(file, "path", None) or getattr(file, "name", None)
|
| 97 |
+
if value:
|
| 98 |
+
return Path(str(value))
|
| 99 |
+
raise ValueError(f"Unsupported upload object: {type(file).__name__}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
def upload_training_images(files):
|
| 103 |
+
"""Copy uploaded files into persistent dataset storage and register them."""
|
| 104 |
DATA.mkdir(parents=True, exist_ok=True)
|
| 105 |
IMAGE_DIR.mkdir(parents=True, exist_ok=True)
|
| 106 |
db = load_db()
|
|
|
|
| 123 |
|
| 124 |
for file in files:
|
| 125 |
try:
|
| 126 |
+
src = normalize_uploaded_file(file)
|
| 127 |
+
if not src.exists() or not src.is_file():
|
| 128 |
+
raise FileNotFoundError(f"Uploaded file not found: {src}")
|
| 129 |
|
| 130 |
ext = src.suffix.lower() or ".jpg"
|
| 131 |
if ext not in {".jpg", ".jpeg", ".png", ".webp", ".bmp"}:
|
|
|
|
| 793 |
)
|
| 794 |
upload_status = gr.Markdown()
|
| 795 |
saved_images = gr.Gallery(
|
| 796 |
+
value=gallery_items(load_db()),
|
| 797 |
label="Saved training images",
|
| 798 |
columns=5,
|
| 799 |
rows=2,
|
|
|
|
| 955 |
server_port=7860,
|
| 956 |
ssr_mode=False,
|
| 957 |
max_file_size="100mb",
|
| 958 |
+
allowed_paths=[str(DATA), str(IMAGE_DIR)],
|
| 959 |
)
|