EzekielMW commited on
Commit
23919f5
·
verified ·
1 Parent(s): 0947142

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -14,59 +14,44 @@ from sklearn.metrics import classification_report, confusion_matrix
14
  import torchvision
15
 
16
 
17
- # Paths
18
  zip_path = "Fruits_dataset.zip"
19
- dataset_root = "./Fruits_dataset"
20
- dataset_folder = "./Fruits_dataset/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
59
- image_shapes = []
60
  for cls in os.listdir(dataset_folder):
61
- cls_folder = os.path.join(dataset_folder, cls)
62
- if os.path.isdir(cls_folder):
63
- for img_file in os.listdir(cls_folder):
64
- img_path = os.path.join(cls_folder, img_file)
65
  try:
66
  img = Image.open(img_path)
67
- image_shapes.append(img.size)
68
- except:
69
- continue
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