m7mdal7aj commited on
Commit
0d3c647
1 Parent(s): 08b4416

fixed bug in perform_object_detection function

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -95,27 +95,34 @@ def perform_object_detection(image, model_name, threshold):
95
  Returns:
96
  PIL.Image, str: The image with drawn bounding boxes and a string of detected objects.
97
  """
98
- # Initialize the ObjectDetector
99
- detector = ObjectDetector()
100
- # Load the specified model
101
- detector.load_model(model_name)
102
- # Perform object detection
103
- processed_image, detected_objects = detector.detect_objects(image, threshold)
104
- return processed_image, detected_objects
 
 
 
105
 
106
  # Check if the 'Detect Objects' button was clicked
107
  if detect_button:
108
  if image is not None:
109
  # Open the uploaded image
110
- image = Image.open(image)
111
- # Display the original image
112
- st.image(image, use_column_width=True, caption="Original Image")
113
-
 
 
 
 
114
  # Perform object detection
115
  processed_image, detected_objects = perform_object_detection(image, detect_model, threshold)
116
 
117
- # Display the image with detected objects
118
- if isinstance(processed_image, Image.Image):
119
  st.image(processed_image, use_column_width=True, caption="Image with Detected Objects")
120
  else:
121
  st.error("Failed to process image for object detection.")
@@ -124,4 +131,3 @@ if detect_button:
124
  st.write(detected_objects)
125
  else:
126
  st.write("Please upload an image for object detection.")
127
-
 
95
  Returns:
96
  PIL.Image, str: The image with drawn bounding boxes and a string of detected objects.
97
  """
98
+
99
+ try:
100
+ # Perform object detection and draw bounding boxes
101
+ processed_image, detected_objects = detect_and_draw_objects(image, model_name, threshold)
102
+ return processed_image, detected_objects
103
+ except Exception as e:
104
+ # Print the error for debugging
105
+ print(f"Error in object detection: {e}")
106
+ return None, str(e)
107
+
108
 
109
  # Check if the 'Detect Objects' button was clicked
110
  if detect_button:
111
  if image is not None:
112
  # Open the uploaded image
113
+ try:
114
+ image = Image.open(image)
115
+ # Display the original image
116
+ st.image(image, use_column_width=True, caption="Original Image")
117
+ except Exception as e:
118
+ st.error(f"Error loading image: {e}")
119
+ return
120
+
121
  # Perform object detection
122
  processed_image, detected_objects = perform_object_detection(image, detect_model, threshold)
123
 
124
+ if processed_image:
125
+ # Display the image with detected objects
126
  st.image(processed_image, use_column_width=True, caption="Image with Detected Objects")
127
  else:
128
  st.error("Failed to process image for object detection.")
 
131
  st.write(detected_objects)
132
  else:
133
  st.write("Please upload an image for object detection.")