EzekielMW commited on
Commit
a4bd9d3
·
verified ·
1 Parent(s): a618268

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -14,41 +14,45 @@ from sklearn.metrics import classification_report, confusion_matrix
14
  import torchvision
15
 
16
 
17
- # Define dataset paths
18
  zip_path = "Fruits_dataset.zip"
19
- dataset_folder = "./Fruits_dataset"
 
20
 
21
- # Check and extract dataset
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 by moving class folders
30
- for folder in ["apples", "pears", "Pears"]:
31
- src = f"./{folder}"
32
- dst = f"./Fruits_dataset/{folder.lower()}"
33
- if os.path.exists(src) and not os.path.exists(dst):
34
- os.makedirs("./Fruits_dataset", exist_ok=True)
35
- os.rename(src, dst)
 
36
 
37
  # Confirm class folders
38
- print("Available classes:", os.listdir("./Fruits_dataset"))
39
 
40
- # Preview all image sizes
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
- try:
48
- img = Image.open(img_path)
49
- print(f"{img_path}: {img.size}")
50
- except:
51
- print(f"Failed to read image: {img_path}")
 
 
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