Pengi5659 commited on
Commit
d73bd07
·
verified ·
1 Parent(s): 5ca707a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -1,24 +1,50 @@
1
-
2
  import tensorflow as tf
3
  import os
4
- # Avoid OOM errors by setting GPU Memory Consumption Growth
5
- gpus = tf.config.experimental.list_physical_devices('GPU')
6
- for gpu in gpus:
7
- tf.config.experimental.set_memory_growth(gpu, True)
8
- tf.config.list_physical_devices('GPU')
9
  import cv2
10
  import imghdr
11
- data_dir = 'data'
12
- image_exts = ['jpeg','jpg', 'bmp', 'png']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  for image_class in os.listdir(data_dir):
14
- for image in os.listdir(os.path.join(data_dir, image_class)):
15
- image_path = os.path.join(data_dir, image_class, image)
 
16
  try:
17
  img = cv2.imread(image_path)
 
 
 
 
 
18
  tip = imghdr.what(image_path)
19
- if tip not in image_exts:
20
- print('Image not in ext list {}'.format(image_path))
21
  os.remove(image_path)
 
 
 
 
 
 
 
 
 
 
22
  except Exception as e:
23
- print('Issue with image {}'.format(image_path))
24
- # os.remove(image_path)
 
 
 
1
  import tensorflow as tf
2
  import os
 
 
 
 
 
3
  import cv2
4
  import imghdr
5
+ from PIL import Image
6
+
7
+ # Configure GPU (if available)
8
+ gpus = tf.config.list_physical_devices('GPU')
9
+ if gpus:
10
+ for gpu in gpus:
11
+ tf.config.experimental.set_memory_growth(gpu, True)
12
+
13
+ print("Available GPUs:", tf.config.list_physical_devices('GPU'))
14
+
15
+ # Allowed image extensions
16
+ image_exts = ['jpeg', 'jpg', 'bmp', 'png']
17
+
18
+ # Root data directory
19
+ data_dir = 'data'
20
+
21
+ # Walk through directory and clean up images
22
  for image_class in os.listdir(data_dir):
23
+ class_path = os.path.join(data_dir, image_class)
24
+ for image in os.listdir(class_path):
25
+ image_path = os.path.join(class_path, image)
26
  try:
27
  img = cv2.imread(image_path)
28
+ if img is None:
29
+ print('Unreadable image (OpenCV):', image_path)
30
+ os.remove(image_path)
31
+ continue
32
+
33
  tip = imghdr.what(image_path)
34
+ if tip not in image_exts:
35
+ print('Unsupported image type:', image_path)
36
  os.remove(image_path)
37
+ continue
38
+
39
+ # Additional corruption check using PIL
40
+ try:
41
+ with Image.open(image_path) as im:
42
+ im.verify()
43
+ except Exception as pil_err:
44
+ print('Corrupted image:', image_path)
45
+ os.remove(image_path)
46
+
47
  except Exception as e:
48
+ print('Error processing image:', image_path)
49
+ print(str(e))
50
+ os.remove(image_path)