EzekielMW commited on
Commit
e687465
·
verified ·
1 Parent(s): 6998125

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -1,17 +1,4 @@
1
- import os
2
- import zipfile
3
-
4
- zip_file = "Fruits_datasets.zip"
5
-
6
- # Extract the zip file
7
- with zipfile.ZipFile(zip_file, 'r') as zip_ref:
8
- zip_ref.extractall(".")
9
- print("Extracted files:")
10
- for name in zip_ref.namelist():
11
- print(name)
12
-
13
 
14
- """
15
  import os
16
  import zipfile
17
  import torch
@@ -29,11 +16,15 @@ import torchvision
29
 
30
 
31
 
 
 
 
 
32
  zip_path = "Fruits_dataset.zip"
33
  extract_root = "./unzipped"
34
  dataset_folder = None
35
 
36
- # Extract ZIP
37
  if not os.path.exists(extract_root):
38
  print("Extracting dataset from uploaded zip...")
39
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
@@ -41,21 +32,24 @@ if not os.path.exists(extract_root):
41
  else:
42
  print("Dataset already extracted.")
43
 
44
- # Try to auto-detect the actual dataset folder
45
  for root, dirs, files in os.walk(extract_root):
46
- if any(os.path.isdir(os.path.join(root, d)) for d in dirs):
47
- # Pick folder that has class folders inside
48
- if all(os.path.isdir(os.path.join(root, d)) for d in dirs):
 
 
49
  dataset_folder = root
50
  break
51
 
 
52
  if dataset_folder is None:
53
- raise RuntimeError("Could not find dataset folder with class directories.")
54
 
55
- print(f"Detected dataset folder: {dataset_folder}")
56
- print("Classes:", os.listdir(dataset_folder))
57
 
58
- # Show image sizes
59
  for cls in os.listdir(dataset_folder):
60
  cls_path = os.path.join(dataset_folder, cls)
61
  if os.path.isdir(cls_path):
@@ -65,7 +59,7 @@ for cls in os.listdir(dataset_folder):
65
  img = Image.open(img_path)
66
  print(f"{img_path}: {img.size}")
67
  except Exception as e:
68
- print(f"Failed to open {img_path}: {e}")
69
 
70
  # -------------------------------
71
  # 2. Load Data
@@ -211,6 +205,6 @@ with desc_block:
211
  image_input = gr.Image(type="pil", label="Upload Fruit Image", image_mode="RGB", height=256, width=256)
212
  pred_output = gr.Textbox(label="Prediction Result")
213
  predict_button = gr.Button("Classify Image")
214
- predict_button.click(fn=predict_image, inputs=image_input, outputs=pred_output)"""
215
 
216
  desc_block.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
2
  import os
3
  import zipfile
4
  import torch
 
16
 
17
 
18
 
19
+ import os
20
+ import zipfile
21
+ from PIL import Image
22
+
23
  zip_path = "Fruits_dataset.zip"
24
  extract_root = "./unzipped"
25
  dataset_folder = None
26
 
27
+ # Step 1: Extract ZIP
28
  if not os.path.exists(extract_root):
29
  print("Extracting dataset from uploaded zip...")
30
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
 
32
  else:
33
  print("Dataset already extracted.")
34
 
35
+ # Step 2: Auto-detect the dataset folder (looking for class folders like Apples, Pears, etc.)
36
  for root, dirs, files in os.walk(extract_root):
37
+ # Check if this folder contains class folders (e.g., Apples/)
38
+ if all(os.path.isdir(os.path.join(root, d)) for d in dirs) and len(dirs) > 0:
39
+ # Ensure it has image files inside class folders
40
+ subdir = os.path.join(root, dirs[0])
41
+ if any(f.lower().endswith(('.png', '.jpg', '.jpeg')) for f in os.listdir(subdir)):
42
  dataset_folder = root
43
  break
44
 
45
+ # Step 3: Handle case when nothing found
46
  if dataset_folder is None:
47
+ raise RuntimeError("Could not find dataset folder with class image directories.")
48
 
49
+ print(f"Detected dataset folder: {dataset_folder}")
50
+ print("Classes:", os.listdir(dataset_folder))
51
 
52
+ # Step 4: Show image dimensions per class
53
  for cls in os.listdir(dataset_folder):
54
  cls_path = os.path.join(dataset_folder, cls)
55
  if os.path.isdir(cls_path):
 
59
  img = Image.open(img_path)
60
  print(f"{img_path}: {img.size}")
61
  except Exception as e:
62
+ print(f"⚠️ Failed to open {img_path}: {e}")
63
 
64
  # -------------------------------
65
  # 2. Load Data
 
205
  image_input = gr.Image(type="pil", label="Upload Fruit Image", image_mode="RGB", height=256, width=256)
206
  pred_output = gr.Textbox(label="Prediction Result")
207
  predict_button = gr.Button("Classify Image")
208
+ predict_button.click(fn=predict_image, inputs=image_input, outputs=pred_output)
209
 
210
  desc_block.launch()