VishnuEcoClim commited on
Commit
4e10fb1
·
1 Parent(s): 9777758

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +16 -5
utils.py CHANGED
@@ -35,11 +35,22 @@ base_model = tf.keras.applications.ResNet50V2(include_top=False, input_shape=inp
35
  base_model.trainable = True
36
 
37
  def preprocess(img_path):
38
- img = Image.open(img_path)
39
- img = img.resize((256, 256))
40
- img_array = np.array(img)
41
- img_array = np.expand_dims(img_array, axis=0)
42
- return img_array
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  def model_arc():
45
  model = tf.keras.Sequential([
 
35
  base_model.trainable = True
36
 
37
  def preprocess(img_path):
38
+ try:
39
+ img = Image.open(img_path)
40
+ img = img.resize((256, 256))
41
+ img = img.convert('RGB') # Ensure the image is in RGB format
42
+
43
+ # Convert the image to a TensorFlow tensor
44
+ img_tensor = tf.keras.preprocessing.image.img_to_array(img)
45
+ img_tensor = tf.expand_dims(img_tensor, axis=0) # Create a batch-like shape
46
+
47
+ # Apply rescaling (aligning with the training rescaling)
48
+ img_tensor = tf.keras.applications.resnet.preprocess_input(img_tensor)
49
+
50
+ return img_tensor
51
+ except Exception as e:
52
+ print(f"Error preprocessing image: {e}")
53
+ return None
54
 
55
  def model_arc():
56
  model = tf.keras.Sequential([