iamironman4279 commited on
Commit
a193f32
1 Parent(s): be6a817

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -0
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for
2
+
3
+ from keras.models import load_model
4
+
5
+ from keras.preprocessing import image
6
+
7
+ import numpy as np
8
+
9
+ import cv2
10
+
11
+ import io
12
+
13
+ import base64
14
+
15
+ from pymongo import MongoClient
16
+
17
+
18
+
19
+ app = Flask(__name__)
20
+
21
+
22
+
23
+ # Load the trained model
24
+
25
+ model = load_model('weights.hdf5')
26
+
27
+ model.compile(loss='binary_crossentropy',
28
+
29
+ optimizer='rmsprop',
30
+
31
+ metrics=['accuracy'])
32
+
33
+
34
+
35
+ # MongoDB connection
36
+
37
+ client = MongoClient('mongodb://localhost:27017/')
38
+
39
+ db = client['userfeedback']
40
+
41
+ feedback_collection = db['feedback']
42
+
43
+
44
+
45
+ @app.route('/', methods=['GET', 'POST'])
46
+
47
+ def index():
48
+
49
+ if request.method == 'POST':
50
+
51
+ return redirect(url_for('upload'))
52
+
53
+ return render_template('index.html')
54
+
55
+
56
+
57
+ @app.route('/upload', methods=['GET', 'POST'])
58
+
59
+ def upload():
60
+
61
+ if request.method == 'POST':
62
+
63
+ # Get the uploaded image file
64
+
65
+ img_file = request.files['file']
66
+
67
+ if img_file:
68
+
69
+ # Read the image file
70
+
71
+ img_bytes = img_file.stream.read()
72
+
73
+ # Convert bytes to numpy array
74
+
75
+ img_np = np.frombuffer(img_bytes, np.uint8)
76
+
77
+ # Decode numpy array to image
78
+
79
+ img = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
80
+
81
+ # Resize the image to match the input shape expected by the model
82
+
83
+ img_resized = cv2.resize(img, (150, 150))
84
+
85
+ # Expand the dimensions to match the input shape expected by the model
86
+
87
+ x = np.expand_dims(img_resized, axis=0)
88
+
89
+ # Normalize the image data
90
+
91
+ x = x / 255.0
92
+
93
+ # Predict probabilities for each class
94
+
95
+ probabilities = model.predict(x)
96
+
97
+ # Find the index of the class with the highest probability
98
+
99
+ predicted_class_index = np.argmax(probabilities)
100
+
101
+ # Determine the class label
102
+
103
+ if predicted_class_index == 1:
104
+
105
+ prediction = "Cancer"
106
+
107
+ # Swap red and violet colors
108
+
109
+ img[:,:,0], img[:,:,2] = img[:,:,2], img[:,:,0].copy()
110
+
111
+ else:
112
+
113
+ prediction = "Normal"
114
+
115
+ img[:,:,0], img[:,:,2] = img[:,:,2], img[:,:,0].copy()
116
+
117
+
118
+
119
+ # Convert the swapped image to base64 format for HTML rendering
120
+
121
+ _, img_encoded = cv2.imencode('.png', img)
122
+
123
+ swapped_img_base64 = base64.b64encode(img_encoded).decode()
124
+
125
+ # Render the result template with the prediction and swapped image
126
+
127
+ return render_template('result.html', prediction=prediction, swapped_img_base64=swapped_img_base64)
128
+
129
+ return render_template('upload.html')
130
+
131
+
132
+
133
+ @app.route('/submit', methods=['POST'])
134
+
135
+ def submit_feedback():
136
+
137
+ if request.method == 'POST':
138
+
139
+ feedback = request.form.get('feedback')
140
+
141
+ if feedback:
142
+
143
+ feedback_collection.insert_one({'feedback': feedback})
144
+
145
+ # Show alert for successful submission
146
+
147
+ return '''
148
+
149
+ <script>
150
+
151
+ alert('Thank you for submitting your feedback!');
152
+
153
+ window.location.href = '/';
154
+
155
+ </script>
156
+
157
+ '''
158
+
159
+ return redirect(url_for('index'))
160
+
161
+
162
+
163
+ if __name__ == '__main__':
164
+
165
+ app.run(debug=True)