Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,53 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
-
import numpy as np
|
| 3 |
-
import os
|
| 4 |
-
import cv2
|
| 5 |
-
import tensorflow
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model_path = os.path.join(os.path.dirname(__file__), "model.h5")
|
| 9 |
-
model = tensorflow.keras.models.load_model(model_path, compile=False, safe_mode=False)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
app = Flask(__name__)
|
| 14 |
-
|
| 15 |
-
UPLOAD_FOLDER = 'static/uploads'
|
| 16 |
-
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', "webp"}
|
| 17 |
-
|
| 18 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 19 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 20 |
-
|
| 21 |
-
def allowed_file(filename):
|
| 22 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 23 |
-
|
| 24 |
-
@app.route('/')
|
| 25 |
-
def form():
|
| 26 |
-
return render_template('index.html')
|
| 27 |
-
@app.route('/predict', methods=['POST'])
|
| 28 |
-
def predict():
|
| 29 |
-
if 'file' not in request.files:
|
| 30 |
-
return "No file uploaded", 400
|
| 31 |
-
|
| 32 |
-
file = request.files['file']
|
| 33 |
-
|
| 34 |
-
if file.filename == '' or not allowed_file(file.filename):
|
| 35 |
-
return "Invalid file type", 400
|
| 36 |
-
|
| 37 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
| 38 |
-
file.save(file_path)
|
| 39 |
-
|
| 40 |
-
image = cv2.imread(file_path)
|
| 41 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 42 |
-
image = cv2.resize(image, (256, 256))
|
| 43 |
-
image = image.astype('float32') / 255.0
|
| 44 |
-
image = np.expand_dims(image, axis=0)
|
| 45 |
-
|
| 46 |
-
result = float(model.predict(image))
|
| 47 |
-
prediction = "No Mask" if result > 0.5 else "With Mask"
|
| 48 |
-
|
| 49 |
-
return render_template('predict.html', result=prediction)
|
| 50 |
-
|
| 51 |
-
if __name__ == "__main__":
|
| 52 |
-
port = int(os.environ.get("PORT",
|
| 53 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import tensorflow
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
model_path = os.path.join(os.path.dirname(__file__), "model.h5")
|
| 9 |
+
model = tensorflow.keras.models.load_model(model_path, compile=False, safe_mode=False)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
|
| 15 |
+
UPLOAD_FOLDER = 'static/uploads'
|
| 16 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', "webp"}
|
| 17 |
+
|
| 18 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 19 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
def allowed_file(filename):
|
| 22 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 23 |
+
|
| 24 |
+
@app.route('/')
|
| 25 |
+
def form():
|
| 26 |
+
return render_template('index.html')
|
| 27 |
+
@app.route('/predict', methods=['POST'])
|
| 28 |
+
def predict():
|
| 29 |
+
if 'file' not in request.files:
|
| 30 |
+
return "No file uploaded", 400
|
| 31 |
+
|
| 32 |
+
file = request.files['file']
|
| 33 |
+
|
| 34 |
+
if file.filename == '' or not allowed_file(file.filename):
|
| 35 |
+
return "Invalid file type", 400
|
| 36 |
+
|
| 37 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
| 38 |
+
file.save(file_path)
|
| 39 |
+
|
| 40 |
+
image = cv2.imread(file_path)
|
| 41 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 42 |
+
image = cv2.resize(image, (256, 256))
|
| 43 |
+
image = image.astype('float32') / 255.0
|
| 44 |
+
image = np.expand_dims(image, axis=0)
|
| 45 |
+
|
| 46 |
+
result = float(model.predict(image))
|
| 47 |
+
prediction = "No Mask" if result > 0.5 else "With Mask"
|
| 48 |
+
|
| 49 |
+
return render_template('predict.html', result=prediction)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
port = int(os.environ.get("PORT", 7860))
|
| 53 |
+
app.run(host="0.0.0.0", port=port)
|