Spaces:
Sleeping
Sleeping
File size: 5,319 Bytes
d07a202 ded7889 d07a202 662a3d1 5772bc5 d07a202 a6e6b8e 3e0aa9d a6e6b8e d07a202 5772bc5 d07a202 a6e6b8e d07a202 a6e6b8e d07a202 a6e6b8e d07a202 a6e6b8e d07a202 b2e404b d07a202 5772bc5 d07a202 5772bc5 d07a202 662a3d1 5772bc5 662a3d1 d07a202 662a3d1 5772bc5 6ea6cca d07a202 5772bc5 662a3d1 5772bc5 d07a202 5772bc5 662a3d1 5772bc5 d07a202 3e0aa9d 5772bc5 3e0aa9d d07a202 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
from flask import Flask, flash, request, redirect, render_template
import os
import cv2
import imutils
import numpy as np
from tensorflow.keras.models import load_model
from werkzeug.utils import secure_filename
import tempfile
from pymongo import MongoClient
from datetime import datetime
# Load the Brain Tumor CNN Model
braintumor_model = load_model('models/braintumor.h5')
# Configuring Flask application
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # Disable caching for images
app.secret_key = "nielitchandigarhpunjabpolice" # Secret key for session management
# Allowed image file extensions
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
# Connect to MongoDB Atlas
client = MongoClient("mongodb+srv://test:test@cluster0.sxci1.mongodb.net/?retryWrites=true&w=majority")
db = client['brain_tumor_detection'] # Database name
collection = db['predictions'] # Collection name
def allowed_file(filename):
"""Check if the file is a valid image format (png, jpg, jpeg)."""
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def preprocess_imgs(set_name, img_size):
"""
Preprocess images by resizing them to the target size (224x224 for VGG16)
and applying appropriate resizing techniques.
"""
set_new = []
for img in set_name:
img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC) # Resize image
set_new.append(img)
return np.array(set_new)
def crop_imgs(set_name, add_pixels_value=0):
"""
Crop the region of interest (ROI) in the image for brain tumor detection.
"""
set_new = []
for img in set_name:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
ADD_PIXELS = add_pixels_value
new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS,
extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()
set_new.append(new_img)
return np.array(set_new)
@app.route('/')
def brain_tumor():
"""Render the HTML form for the user to upload an image."""
return render_template('braintumor.html')
@app.route('/resultbt', methods=['POST'])
def resultbt():
"""Process the uploaded image and save prediction results to MongoDB."""
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
file = request.files['file']
if file and allowed_file(file.filename):
temp_file = tempfile.NamedTemporaryFile(delete=False)
filename = secure_filename(file.filename)
file.save(temp_file.name)
flash('Image successfully uploaded and displayed below')
# Process the image
img = cv2.imread(temp_file.name)
img = crop_imgs([img])
img = img.reshape(img.shape[1:])
img = preprocess_imgs([img], (224, 224))
# Make prediction
pred = braintumor_model.predict(img)
prediction = 'Tumor Detected' if pred[0][0] >= 0.5 else 'No Tumor Detected'
confidence_score = float(pred[0][0])
# Prepare data for MongoDB
result = {
"firstname": firstname,
"lastname": lastname,
"email": email,
"phone": phone,
"gender": gender,
"age": age,
"image_name": filename,
"prediction": prediction,
"confidence_score": confidence_score,
"timestamp": datetime.utcnow()
}
# Insert data into MongoDB
collection.insert_one(result)
# Return the result to the user
return render_template('resultbt.html', filename=filename, fn=firstname, ln=lastname, age=age, r=prediction, gender=gender)
else:
flash('Allowed image types are - png, jpg, jpeg')
return redirect(request.url)
@app.route('/dbresults')
def dbresults():
"""Fetch all results from MongoDB and render them in a template."""
# Fetch all documents from the MongoDB collection
all_results = collection.find() # Returns a cursor
# Convert cursor to a list of dictionaries
results_list = []
for result in all_results:
result['_id'] = str(result['_id']) # Convert ObjectId to string for JSON serialization
results_list.append(result)
# Pass the results to the HTML template
return render_template('dbresults.html', results=results_list)
if __name__ == '__main__':
app.run(debug=True)
|