LovnishVerma commited on
Commit
662a3d1
·
verified ·
1 Parent(s): 6ea6cca

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -23
main.py CHANGED
@@ -5,6 +5,7 @@ import imutils
5
  import numpy as np
6
  from tensorflow.keras.models import load_model
7
  from werkzeug.utils import secure_filename
 
8
 
9
  # Load the Brain Tumor CNN Model
10
  braintumor_model = load_model('models/braintumor.h5')
@@ -70,40 +71,40 @@ def brain_tumor():
70
  @app.route('/resultbt', methods=['POST'])
71
  def resultbt():
72
  if request.method == 'POST':
 
 
 
 
 
 
 
73
  file = request.files['file']
 
74
  if file and allowed_file(file.filename):
 
 
75
  filename = secure_filename(file.filename)
76
- filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
77
-
78
- # Ensure upload folder exists
79
- if not os.path.exists(app.config['UPLOAD_FOLDER']):
80
- os.makedirs(app.config['UPLOAD_FOLDER'])
81
 
82
- file.save(filepath)
83
  flash('Image successfully uploaded and displayed below')
84
 
85
- # Load and preprocess image
86
- try:
87
- img = cv2.imread(filepath)
88
- if img is None:
89
- raise ValueError("Image file is invalid or cannot be read.")
90
 
91
- img = crop_imgs([img])
92
- img = img.reshape(img.shape[1:])
93
- img = preprocess_imgs([img], (224, 224))
94
- img = img.reshape(1, 224, 224, 3)
 
 
95
 
96
- pred = braintumor_model.predict(img)
97
- pred = 1 if pred >= 0.5 else 0
98
-
99
- return render_template('resultbt.html', filename=filename, r=pred)
100
- except Exception as e:
101
- flash(f"Error processing image: {str(e)}")
102
- return redirect(request.url)
103
  else:
104
  flash('Allowed image types are - png, jpg, jpeg')
105
  return redirect(request.url)
106
 
107
-
108
  if __name__ == '__main__':
109
  app.run(debug=True)
 
5
  import numpy as np
6
  from tensorflow.keras.models import load_model
7
  from werkzeug.utils import secure_filename
8
+ import tempfile
9
 
10
  # Load the Brain Tumor CNN Model
11
  braintumor_model = load_model('models/braintumor.h5')
 
71
  @app.route('/resultbt', methods=['POST'])
72
  def resultbt():
73
  if request.method == 'POST':
74
+ # Get user input from the form
75
+ firstname = request.form['firstname']
76
+ lastname = request.form['lastname']
77
+ email = request.form['email']
78
+ phone = request.form['phone']
79
+ gender = request.form['gender']
80
+ age = request.form['age']
81
  file = request.files['file']
82
+
83
  if file and allowed_file(file.filename):
84
+ # Use tempfile to create a temporary file path
85
+ temp_file = tempfile.NamedTemporaryFile(delete=False)
86
  filename = secure_filename(file.filename)
87
+ file.save(temp_file.name) # Save file to the temp location
 
 
 
 
88
 
 
89
  flash('Image successfully uploaded and displayed below')
90
 
91
+ # Read and process the image
92
+ img = cv2.imread(temp_file.name)
93
+ img = crop_imgs([img])
94
+ img = img.reshape(img.shape[1:])
95
+ img = preprocess_imgs([img], (224, 224))
96
 
97
+ # Model prediction
98
+ pred = braintumor_model.predict(img)
99
+ if pred < 0.5:
100
+ pred = 0
101
+ else:
102
+ pred = 1
103
 
104
+ return render_template('resultbt.html', filename=filename, fn=firstname, ln=lastname, age=age, r=pred, gender=gender)
 
 
 
 
 
 
105
  else:
106
  flash('Allowed image types are - png, jpg, jpeg')
107
  return redirect(request.url)
108
 
 
109
  if __name__ == '__main__':
110
  app.run(debug=True)