Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,41 +14,45 @@ from sklearn.metrics import classification_report, confusion_matrix
|
|
| 14 |
import torchvision
|
| 15 |
|
| 16 |
|
| 17 |
-
#
|
| 18 |
zip_path = "Fruits_dataset.zip"
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
if not os.path.exists(dataset_folder):
|
| 23 |
print("Extracting dataset from uploaded zip...")
|
| 24 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 25 |
-
zip_ref.extractall(
|
| 26 |
else:
|
| 27 |
print("Dataset already extracted.")
|
| 28 |
|
| 29 |
-
# Normalize folder structure
|
| 30 |
-
for folder in
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
os.
|
| 35 |
-
os.
|
|
|
|
| 36 |
|
| 37 |
# Confirm class folders
|
| 38 |
-
print("Available classes:", os.listdir(
|
| 39 |
|
| 40 |
-
# Preview
|
| 41 |
print("\nImage sizes:")
|
| 42 |
for cls in os.listdir(dataset_folder):
|
| 43 |
cls_folder = os.path.join(dataset_folder, cls)
|
| 44 |
if os.path.isdir(cls_folder):
|
| 45 |
for img_file in os.listdir(cls_folder):
|
| 46 |
img_path = os.path.join(cls_folder, img_file)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
# Preview image dimensions
|
|
|
|
| 14 |
import torchvision
|
| 15 |
|
| 16 |
|
| 17 |
+
# Paths
|
| 18 |
zip_path = "Fruits_dataset.zip"
|
| 19 |
+
dataset_root = "./Fruits_dataset"
|
| 20 |
+
dataset_folder = os.path.join(dataset_root, "Data")
|
| 21 |
|
| 22 |
+
# Extract dataset if not already extracted
|
| 23 |
if not os.path.exists(dataset_folder):
|
| 24 |
print("Extracting dataset from uploaded zip...")
|
| 25 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 26 |
+
zip_ref.extractall(dataset_root)
|
| 27 |
else:
|
| 28 |
print("Dataset already extracted.")
|
| 29 |
|
| 30 |
+
# Normalize folder structure: make all class folder names lowercase
|
| 31 |
+
for folder in os.listdir(dataset_folder):
|
| 32 |
+
folder_path = os.path.join(dataset_folder, folder)
|
| 33 |
+
if os.path.isdir(folder_path):
|
| 34 |
+
new_name = folder.lower()
|
| 35 |
+
new_path = os.path.join(dataset_folder, new_name)
|
| 36 |
+
if folder != new_name and not os.path.exists(new_path):
|
| 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 |
# Preview image dimensions
|