LovnishVerma commited on
Commit
6ea6cca
·
verified ·
1 Parent(s): 4ad43ea

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -18
main.py CHANGED
@@ -70,34 +70,40 @@ def brain_tumor():
70
  @app.route('/resultbt', methods=['POST'])
71
  def resultbt():
72
  if request.method == 'POST':
73
- firstname = request.form['firstname']
74
- lastname = request.form['lastname']
75
- email = request.form['email']
76
- phone = request.form['phone']
77
- gender = request.form['gender']
78
- age = request.form['age']
79
  file = request.files['file']
80
-
81
  if file and allowed_file(file.filename):
82
  filename = secure_filename(file.filename)
83
- file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
 
 
 
 
 
 
84
  flash('Image successfully uploaded and displayed below')
85
 
86
- img = cv2.imread('static/uploads/' + filename)
87
- img = crop_imgs([img])
88
- img = img.reshape(img.shape[1:])
89
- img = preprocess_imgs([img], (224, 224))
 
90
 
91
- pred = braintumor_model.predict(img)
92
- if pred < 0.5:
93
- pred = 0
94
- else:
95
- pred = 1
96
 
97
- return render_template('resultbt.html', filename=filename, fn=firstname, ln=lastname, age=age, r=pred, gender=gender)
 
 
 
 
 
 
98
  else:
99
  flash('Allowed image types are - png, jpg, jpeg')
100
  return redirect(request.url)
101
 
 
102
  if __name__ == '__main__':
103
  app.run(debug=True)
 
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)