Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,59 +14,44 @@ from sklearn.metrics import classification_report, confusion_matrix
|
|
| 14 |
import torchvision
|
| 15 |
|
| 16 |
|
| 17 |
-
|
| 18 |
zip_path = "Fruits_dataset.zip"
|
| 19 |
-
|
| 20 |
-
dataset_folder =
|
| 21 |
|
| 22 |
-
# Extract
|
| 23 |
-
if not os.path.exists(
|
| 24 |
print("Extracting dataset from uploaded zip...")
|
| 25 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 26 |
-
zip_ref.extractall(
|
| 27 |
else:
|
| 28 |
print("Dataset already extracted.")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
for
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
os.rename(folder_path, new_path)
|
| 38 |
-
|
| 39 |
-
# Confirm class folders
|
| 40 |
-
print("Available classes:", os.listdir(dataset_folder))
|
| 41 |
-
|
| 42 |
-
# Preview image sizes in each class
|
| 43 |
-
print("\nImage sizes:")
|
| 44 |
-
for cls in os.listdir(dataset_folder):
|
| 45 |
-
cls_folder = os.path.join(dataset_folder, cls)
|
| 46 |
-
if os.path.isdir(cls_folder):
|
| 47 |
-
for img_file in os.listdir(cls_folder):
|
| 48 |
-
img_path = os.path.join(cls_folder, img_file)
|
| 49 |
-
if os.path.isfile(img_path):
|
| 50 |
-
try:
|
| 51 |
-
img = Image.open(img_path)
|
| 52 |
-
print(f"{img_path}: {img.size}")
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"Failed to read image: {img_path} ({e})")
|
| 55 |
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
image_shapes = []
|
| 60 |
for cls in os.listdir(dataset_folder):
|
| 61 |
-
|
| 62 |
-
if os.path.isdir(
|
| 63 |
-
for img_file in os.listdir(
|
| 64 |
-
img_path = os.path.join(
|
| 65 |
try:
|
| 66 |
img = Image.open(img_path)
|
| 67 |
-
|
| 68 |
-
except:
|
| 69 |
-
|
| 70 |
|
| 71 |
# -------------------------------
|
| 72 |
# 2. Load Data
|
|
|
|
| 14 |
import torchvision
|
| 15 |
|
| 16 |
|
| 17 |
+
|
| 18 |
zip_path = "Fruits_dataset.zip"
|
| 19 |
+
extract_root = "./unzipped"
|
| 20 |
+
dataset_folder = None
|
| 21 |
|
| 22 |
+
# Extract ZIP
|
| 23 |
+
if not os.path.exists(extract_root):
|
| 24 |
print("Extracting dataset from uploaded zip...")
|
| 25 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 26 |
+
zip_ref.extractall(extract_root)
|
| 27 |
else:
|
| 28 |
print("Dataset already extracted.")
|
| 29 |
|
| 30 |
+
# Try to auto-detect the actual dataset folder
|
| 31 |
+
for root, dirs, files in os.walk(extract_root):
|
| 32 |
+
if any(os.path.isdir(os.path.join(root, d)) for d in dirs):
|
| 33 |
+
# Pick folder that has class folders inside
|
| 34 |
+
if all(os.path.isdir(os.path.join(root, d)) for d in dirs):
|
| 35 |
+
dataset_folder = root
|
| 36 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
if dataset_folder is None:
|
| 39 |
+
raise RuntimeError("Could not find dataset folder with class directories.")
|
| 40 |
|
| 41 |
+
print(f"Detected dataset folder: {dataset_folder}")
|
| 42 |
+
print("Classes:", os.listdir(dataset_folder))
|
| 43 |
|
| 44 |
+
# Show image sizes
|
|
|
|
| 45 |
for cls in os.listdir(dataset_folder):
|
| 46 |
+
cls_path = os.path.join(dataset_folder, cls)
|
| 47 |
+
if os.path.isdir(cls_path):
|
| 48 |
+
for img_file in os.listdir(cls_path):
|
| 49 |
+
img_path = os.path.join(cls_path, img_file)
|
| 50 |
try:
|
| 51 |
img = Image.open(img_path)
|
| 52 |
+
print(f"{img_path}: {img.size}")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Failed to open {img_path}: {e}")
|
| 55 |
|
| 56 |
# -------------------------------
|
| 57 |
# 2. Load Data
|