Spaces:
Build error
Build error
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)}"} | |
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) | |