-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (71 loc) · 2.5 KB
/
app.py
File metadata and controls
81 lines (71 loc) · 2.5 KB
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
from flask import Flask, render_template, request, jsonify, session
import os
import numpy as np
from werkzeug.utils import secure_filename
import pickle
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing import image
from sklearn.cluster import KMeans
def preprocess(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = tf.keras.applications.vgg16.preprocess_input(img_data)
features = base_model.predict(img_data)
return features.flatten()
def predict():
try:
with open("model/kmeans_model.pkl", "rb") as file:
kmeans = pickle.load(file)
cluster = kmeans.predict([preprocess(session["curfile"])])
print("Class :",cluster[0])
return cluster[0]
except Exception as e:
return str(e)
app = Flask(__name__)
app.secret_key = "sgdgwjkdbksjdgbkajbf"
UPLOAD_FOLDER = 'static/uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload',methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
session["curfile"] = filepath
return jsonify({"img_url": filepath})
@app.route('/detect', methods=['POST'])
def detect():
pc = predict()
if (pc == 0):
output = "High Light Polution detected"
elif(pc == 1):
output = "High to Moderate Light Polution detected"
elif(pc == 2):
output = "Moderate to Low Light Polution detected"
else:
return jsonify({"error": pc})
return jsonify({'message': output})
if __name__ == '__main__':
print("Starting......")
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
print("GPU memory growth enabled.")
except RuntimeError as e:
print(e)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
print("Loaded VGG16....")
app.run(debug=True)
print("Shutting down.")