Spaces:
Build error
Build error
File size: 14,199 Bytes
07629a7 | 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | from flask import Flask, render_template, request, flash
import os
from werkzeug.utils import secure_filename
from keras.models import load_model
from keras.layers import DepthwiseConv2D
from PIL import Image, ImageOps
import numpy as np
app = Flask(__name__)
app.secret_key = 'secret-key'
UPLOAD_FOLDER = os.path.abspath('uploads/')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
FACILITIES = [
{
"name": "Audi",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\audi model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\audi model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Audi.pdf"
},
{
"name": "Canteen",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\canteen model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\canteen model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Canteen.pdf"
},
{
"name": "Classroom",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\classroom model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\classroom model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Classroom.pdf"
},
{
"name": "Computer Lab",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\computer lab model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\computer lab model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Computer Lab.pdf"
},
{
"name": "Conference Hall",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\conference hall model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\conference hall model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Conference Hall.pdf"
},
{
"name": "Drawing Halls",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\drawing halls model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\drawing halls model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Drawing Halls.pdf"
},
{
"name": "Faculty Cabin",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\faculty cabin model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\faculty cabin model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Faculty Cabin.pdf"
},
{
"name": "Fire Extinguishers",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\fire extinguishers model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\fire extinguishers model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Fire Extinguishers.pdf"
},
{
"name": "Generator",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\generator model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\generator model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Generator.pdf"
},
{
"name": "Grounds",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\grounds model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\grounds model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Grounds.pdf"
},
{
"name": "Library",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\library model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\library model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Library.pdf"
},
{
"name": "Medical Room",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\medical room model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\medical room model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Medical Room.pdf"
},
{
"name": "Parking",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\parking model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\parking model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Parking.pdf"
},
{
"name": "Pothole Management",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\pothole management model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\pothole management model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Pothole Management.pdf"
},
{
"name": "Restroom",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\restroom model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\restroom model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Restroom.pdf"
},
{
"name": "Seminar Hall",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\seminar hall model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\seminar hall model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Seminar Hall.pdf"
},
{
"name": "Server Room",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\server room model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\server room model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Server Room.pdf"
},
{
"name": "Sports Equipment",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\sports equipment model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\sports equipment model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Sports Equipment.pdf"
},
{
"name": "TPO (Training and Placement Office)",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\tpo model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\tpo model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\TPO (Training and Placement Office).pdf"
},
{
"name": "Workshop",
"type": "image_processing",
"model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\workshop model\keras_model.h5",
"labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\workshop model\labels.txt",
"pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Workshop.pdf"
}
]
def calculate_required_facilities(num_students, num_divisions, num_courses, course_duration):
# 1. Classrooms Required
classrooms_required = num_divisions * course_duration * 0.5
# 2. Total Labs Required
base_labs = 2 * num_courses * course_duration
if num_students > 180 * num_courses:
extra_students_per_course = (num_students - (180 * num_courses)) // 50
base_labs += extra_students_per_course
additional_labs = (num_students - 600) // 150 if num_students > 600 else 0
total_labs = base_labs + additional_labs
# 3. Workshops Required
workshops_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
# 4. CAD Centres Required
cad_centres_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
# 5. Computer Centres Required
computer_centres_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
# 6. Seminar Halls Required
seminar_halls_required = 1
# 7. Library Required
library_required = 1
# 8. Language Labs Required
language_labs_required = 1
# 9. PCs/Laptops Required
pcs_laptops_required = max(20, num_students // 10)
# 10. Additional Facilities (Single Requirement)
auditorium_required = 1
faculty_cabins_required = 1
medical_room_required = 1
sports_facilities_required = 1
canteen_required = 1
generator_required = 1
bicycle_stand_required = 1
bus_transport_required = 1
# 11. Other Derived Formulas
dispensers_required = 1
fire_extinguishers_required = 1
grounds_required = 1
parking_areas_required = 1
road_condition_check = 1
# Returning the required facilities as a dictionary
return {
"classrooms_required": int(classrooms_required),
"labs_required": int(total_labs),
"workshops_required": int(workshops_required),
"cad_centres_required": int(cad_centres_required),
"computer_centres_required": int(computer_centres_required),
"seminar_halls_required": int(seminar_halls_required),
"library_required": int(library_required),
"language_labs_required": int(language_labs_required),
"pcs_laptops_required": int(pcs_laptops_required),
"auditorium_required": int(auditorium_required),
"faculty_cabins_required": int(faculty_cabins_required),
"medical_room_required": int(medical_room_required),
"sports_facilities_required": int(sports_facilities_required),
"canteen_required": int(canteen_required),
"generator_required": int(generator_required),
"bicycle_stand_required": int(bicycle_stand_required),
"bus_transport_required": int(bus_transport_required),
"dispensers_required": int(dispensers_required),
"fire_extinguishers_required": int(fire_extinguishers_required),
"grounds_required": int(grounds_required),
"parking_areas_required": int(parking_areas_required),
"road_condition_check": int(road_condition_check),
}
def process_image_with_model(model_path, labels_path, image_path):
try:
model = load_model(model_path, compile=False)
with open(labels_path, "r") as file:
class_names = file.readlines()
image = Image.open(image_path).convert("RGB")
image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
image_array = np.asarray(image).astype(np.float32) / 127.5 - 1
data = np.expand_dims(image_array, axis=0)
prediction = model.predict(data)
index = np.argmax(prediction)
class_name = class_names[index].strip()
confidence_score = prediction[0][index]
return {"class": class_name, "confidence": round(confidence_score * 100, 2)}
except Exception as e:
return {"error": f"Failed to process image. Reason: {str(e)}"}
@app.route('/', methods=['GET', 'POST'])
def home():
required_facilities = {}
if request.method == 'POST' and 'calculate_facilities' in request.form:
# Fetch inputs for calculation
num_students = int(request.form['num_students'])
num_divisions = int(request.form['num_divisions'])
num_courses = int(request.form['num_courses'])
course_duration = int(request.form['course_duration'])
# Calculate facilities
required_facilities = calculate_required_facilities(
num_students, num_divisions, num_courses, course_duration
)
# Handle verification for each facility
elif request.method == 'POST' and 'facility' in request.form:
selected_facility = request.form.get('facility')
facility = next((f for f in FACILITIES if f['name'] == selected_facility), None)
if facility:
files = request.files.getlist("files")
if files:
save_dir = os.path.join(app.config['UPLOAD_FOLDER'], selected_facility)
os.makedirs(save_dir, exist_ok=True)
for file in files:
if file.filename == "":
flash("No file selected for uploading.", "warning")
continue
filename = secure_filename(file.filename)
file_path = os.path.join(save_dir, filename)
file.save(file_path)
result = process_image_with_model(facility["model"], facility["labels"], file_path)
if 'error' in result:
flash(result['error'], "danger")
else:
flash(f"{result['class']} detected with {result['confidence']}% confidence.", "success")
return render_template(
'index.html',
facilities=FACILITIES,
required_facilities=required_facilities
)
if __name__ == '__main__':
app.run(debug=True)
|