LovnishVerma commited on
Commit
d726cc3
1 Parent(s): 890a9f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -47,8 +47,9 @@ def crop_imgs(set_name, add_pixels_value=0):
47
  return np.array(set_new)
48
 
49
  # Function to preprocess the image
50
- def preprocess_image(img_path):
51
- img = image.load_img(img_path, target_size=(200, 200))
 
52
  img_array = image.img_to_array(img)
53
  img_array = np.expand_dims(img_array, axis=0)
54
  img_array /= 255.0 # Normalize the image
@@ -87,18 +88,24 @@ def main():
87
  st.write("")
88
  st.write("Classifying...")
89
 
90
- # Read the contents of the uploaded file
91
  file_contents = uploaded_file.read()
 
 
92
 
93
  # Save the uploaded file
94
- filename = secure_filename(uploaded_file.name)
95
- file_path = os.path.join(UPLOAD_FOLDER, filename)
96
-
97
- with open(file_path, "wb") as f:
98
- f.write(file_contents)
99
-
100
- # Make prediction
101
- result = predict_braintumor(file_path)
 
 
 
 
102
 
103
  # Display prediction
104
  st.subheader("Prediction:")
 
47
  return np.array(set_new)
48
 
49
  # Function to preprocess the image
50
+ def preprocess_image(img_array):
51
+ img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
52
+ img = image.array_to_img(img_array, target_size=(200, 200))
53
  img_array = image.img_to_array(img)
54
  img_array = np.expand_dims(img_array, axis=0)
55
  img_array /= 255.0 # Normalize the image
 
88
  st.write("")
89
  st.write("Classifying...")
90
 
91
+ # Read the contents of the uploaded file or the input image
92
  file_contents = uploaded_file.read()
93
+ uploaded_array = np.asarray(bytearray(file_contents), dtype=np.uint8)
94
+ uploaded_img = cv2.imdecode(uploaded_array, -1)
95
 
96
  # Save the uploaded file
97
+ if isinstance(file_contents, bytes):
98
+ filename = secure_filename(uploaded_file.name)
99
+ file_path = os.path.join(UPLOAD_FOLDER, filename)
100
+
101
+ with open(file_path, "wb") as f:
102
+ f.write(file_contents)
103
+
104
+ # Make prediction
105
+ result = predict_braintumor(file_path)
106
+ else:
107
+ # Make prediction for Gradio (direct input of image)
108
+ result = predict_braintumor(uploaded_img)
109
 
110
  # Display prediction
111
  st.subheader("Prediction:")