courte commited on
Commit
b69eaf0
·
1 Parent(s): b9bf296
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -16,6 +16,9 @@ CAR_BRANDS = sorted(os.listdir(DATASET_PATH))
16
  # Ensure the CAR_BRANDS list contains only valid directories
17
  CAR_BRANDS = [brand for brand in CAR_BRANDS if os.path.isdir(os.path.join(DATASET_PATH, brand))]
18
 
 
 
 
19
  # Define the preprocessing function
20
  def preprocess_image(image):
21
  """
@@ -24,6 +27,10 @@ def preprocess_image(image):
24
  if isinstance(image, np.ndarray): # If Gradio gives a NumPy array, convert it to PIL Image
25
  image = Image.fromarray(image)
26
 
 
 
 
 
27
  image = image.convert("RGB") # Ensure it's in RGB mode
28
  image = image.resize((299, 299)) # Resize to match model input size
29
  image = np.array(image) / 255.0 # Normalize pixel values
@@ -35,25 +42,38 @@ def predict(image):
35
  """
36
  Predict the car brand and return the predicted brand and sample images.
37
  """
38
- # Preprocess the image
39
- processed_image = preprocess_image(image)
40
-
41
- # Make a prediction
42
- predictions = model.predict(processed_image)
43
- predicted_class_index = np.argmax(predictions, axis=1)[0]
44
- predicted_brand = CAR_BRANDS[predicted_class_index]
45
-
46
- # Get sample images from the predicted brand folder
47
- brand_folder = os.path.join(DATASET_PATH, predicted_brand)
48
- sample_images = []
49
- if os.path.exists(brand_folder):
50
- for filename in os.listdir(brand_folder)[:5]: # Limit to 5 sample images
51
- img_path = os.path.join(brand_folder, filename)
52
- if os.path.isfile(img_path):
53
- sample_images.append(Image.open(img_path).resize((200, 200))) # Resize for consistency
54
-
55
- # Return the predicted brand and sample images as separate outputs
56
- return predicted_brand, sample_images or ["No images found for this brand."]
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Create the Gradio interface
59
  iface = gr.Interface(
 
16
  # Ensure the CAR_BRANDS list contains only valid directories
17
  CAR_BRANDS = [brand for brand in CAR_BRANDS if os.path.isdir(os.path.join(DATASET_PATH, brand))]
18
 
19
+ # Print the list of car brands for debugging
20
+ print("Car brands:", CAR_BRANDS)
21
+
22
  # Define the preprocessing function
23
  def preprocess_image(image):
24
  """
 
27
  if isinstance(image, np.ndarray): # If Gradio gives a NumPy array, convert it to PIL Image
28
  image = Image.fromarray(image)
29
 
30
+ # Check if the image is valid
31
+ if image is None or image.size == 0:
32
+ raise ValueError("Invalid or blank image uploaded.")
33
+
34
  image = image.convert("RGB") # Ensure it's in RGB mode
35
  image = image.resize((299, 299)) # Resize to match model input size
36
  image = np.array(image) / 255.0 # Normalize pixel values
 
42
  """
43
  Predict the car brand and return the predicted brand and sample images.
44
  """
45
+ try:
46
+ # Preprocess the image
47
+ processed_image = preprocess_image(image)
48
+
49
+ # Make a prediction
50
+ predictions = model.predict(processed_image)
51
+ predicted_class_index = np.argmax(predictions, axis=1)[0]
52
+ predicted_brand = CAR_BRANDS[predicted_class_index]
53
+
54
+ # Debugging: Print the predicted brand
55
+ print(f"Predicted brand: {predicted_brand}")
56
+
57
+ # Get sample images from the predicted brand folder
58
+ brand_folder = os.path.join(DATASET_PATH, predicted_brand)
59
+ print(f"Predicted brand folder: {brand_folder}")
60
+
61
+ sample_images = []
62
+ if os.path.exists(brand_folder):
63
+ print(f"Found {len(os.listdir(brand_folder))} files in the folder.")
64
+ for filename in os.listdir(brand_folder)[:5]: # Limit to 5 sample images
65
+ img_path = os.path.join(brand_folder, filename)
66
+ if os.path.isfile(img_path):
67
+ sample_images.append(Image.open(img_path).resize((200, 200))) # Resize for consistency
68
+ else:
69
+ print("Brand folder does not exist.")
70
+
71
+ # Return the predicted brand and sample images as separate outputs
72
+ return predicted_brand, sample_images or ["No images found for this brand."]
73
+
74
+ except Exception as e:
75
+ print(f"Error during prediction: {e}")
76
+ return "Error", ["An error occurred during prediction."]
77
 
78
  # Create the Gradio interface
79
  iface = gr.Interface(