LovnishVerma commited on
Commit
d07a202
·
verified ·
1 Parent(s): 96e6dbe

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +97 -38
main.py CHANGED
@@ -1,44 +1,103 @@
1
- from flask import Flask, request, jsonify, render_template
2
- from flask_cors import CORS
 
 
3
  import numpy as np
4
- import pandas as pd
5
- from sklearn.linear_model import LogisticRegression
 
 
 
 
 
 
 
6
 
7
  app = Flask(__name__)
8
- app.static_folder = 'static'
9
- app.static_url_path = '/static'
10
-
11
- app.secret_key = "flask-nielit-2023"
12
-
13
- CORS(app)
14
-
15
- @app.route('/')
16
- def iris():
17
- return render_template("index.html")
18
-
19
- @app.route('/irisf', methods=["POST"])
20
- def page():
21
- swidth=eval(request.form.get("swidth"))
22
- sheight=eval(request.form.get("sheight"))
23
- pwidth=eval(request.form.get("pwidth"))
24
- pheight=eval(request.form.get("pheight"))
25
-
26
- url="https://raw.githubusercontent.com/lovnishverma/datasets/main/iris.csv"
27
-
28
- data=pd.read_csv(url, header=None)
29
- flower=data.values
30
-
31
- #Split
32
- x=flower[:,:4]
33
- y=flower[:,-1]
34
-
35
- model=LogisticRegression()
36
- model.fit(x,y)
37
-
38
- arr=model.predict([[swidth,sheight,pwidth,pheight]])
39
-
40
- return render_template("index.html", data=str(arr[0]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
 
 
42
 
43
  if __name__ == '__main__':
44
- app.run()
 
1
+ from flask import Flask, flash, request, redirect, render_template
2
+ import os
3
+ import cv2
4
+ 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')
11
+
12
+ # Configuring Flask
13
+ UPLOAD_FOLDER = 'static/uploads'
14
+ ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
15
 
16
  app = Flask(__name__)
17
+ app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
18
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
19
+ app.secret_key = "secret key"
20
+
21
+ def allowed_file(filename):
22
+ """Check if the file is a valid image format"""
23
+ return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
24
+
25
+ def preprocess_imgs(set_name, img_size):
26
+ """
27
+ Preprocess the image by resizing and applying VGG16 preprocessing
28
+ """
29
+ set_new = []
30
+ for img in set_name:
31
+ img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC)
32
+ set_new.append(img)
33
+ return np.array(set_new)
34
+
35
+ def crop_imgs(set_name, add_pixels_value=0):
36
+ """
37
+ Crop the region of interest (ROI) in the image (brain tumor detection)
38
+ """
39
+ set_new = []
40
+ for img in set_name:
41
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
42
+ gray = cv2.GaussianBlur(gray, (5, 5), 0)
43
+
44
+ # Threshold the image and find contours to crop the image
45
+ thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
46
+ thresh = cv2.erode(thresh, None, iterations=2)
47
+ thresh = cv2.dilate(thresh, None, iterations=2)
48
+
49
+ cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
50
+ cnts = imutils.grab_contours(cnts)
51
+ c = max(cnts, key=cv2.contourArea)
52
+
53
+ # Find the extreme points and crop the image
54
+ extLeft = tuple(c[c[:, :, 0].argmin()][0])
55
+ extRight = tuple(c[c[:, :, 0].argmax()][0])
56
+ extTop = tuple(c[c[:, :, 1].argmin()][0])
57
+ extBot = tuple(c[c[:, :, 1].argmax()][0])
58
+
59
+ ADD_PIXELS = add_pixels_value
60
+ new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS,
61
+ extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()
62
+ set_new.append(new_img)
63
+
64
+ return np.array(set_new)
65
+
66
+ @app.route('/braintumor')
67
+ def brain_tumor():
68
+ return render_template('braintumor.html')
69
+
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)