CoolFace
Apppublic

pranit144/Institute_Inspection_image_processing

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
roufgh2299 linesDownload Raw Back to root
1from flask import Flask, render_template, request, flash
2import os
3from werkzeug.utils import secure_filename
4from keras.models import load_model
5from keras.layers import DepthwiseConv2D
6from PIL import Image, ImageOps
7import numpy as np
8
9app = Flask(__name__)
10app.secret_key = 'secret-key'
11
12UPLOAD_FOLDER = os.path.abspath('uploads/')
13app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
14os.makedirs(UPLOAD_FOLDER, exist_ok=True)
15FACILITIES = [
16    {
17        "name": "Audi",
18        "type": "image_processing",
19        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\audi model\keras_model.h5",
20        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\audi model\labels.txt",
21        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Audi.pdf"
22    },
23    {
24        "name": "Canteen",
25        "type": "image_processing",
26        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\canteen model\keras_model.h5",
27        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\canteen model\labels.txt",
28        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Canteen.pdf"
29    },
30    {
31        "name": "Classroom",
32        "type": "image_processing",
33        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\classroom model\keras_model.h5",
34        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\classroom model\labels.txt",
35        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Classroom.pdf"
36    },
37    {
38        "name": "Computer Lab",
39        "type": "image_processing",
40        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\computer lab model\keras_model.h5",
41        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\computer lab model\labels.txt",
42        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Computer Lab.pdf"
43    },
44    {
45        "name": "Conference Hall",
46        "type": "image_processing",
47        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\conference hall model\keras_model.h5",
48        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\conference hall model\labels.txt",
49        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Conference Hall.pdf"
50    },
51    {
52        "name": "Drawing Halls",
53        "type": "image_processing",
54        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\drawing halls model\keras_model.h5",
55        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\drawing halls model\labels.txt",
56        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Drawing Halls.pdf"
57    },
58    {
59        "name": "Faculty Cabin",
60        "type": "image_processing",
61        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\faculty cabin model\keras_model.h5",
62        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\faculty cabin model\labels.txt",
63        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Faculty Cabin.pdf"
64    },
65    {
66        "name": "Fire Extinguishers",
67        "type": "image_processing",
68        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\fire extinguishers model\keras_model.h5",
69        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\fire extinguishers model\labels.txt",
70        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Fire Extinguishers.pdf"
71    },
72    {
73        "name": "Generator",
74        "type": "image_processing",
75        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\generator model\keras_model.h5",
76        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\generator model\labels.txt",
77        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Generator.pdf"
78    },
79    {
80        "name": "Grounds",
81        "type": "image_processing",
82        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\grounds model\keras_model.h5",
83        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\grounds model\labels.txt",
84        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Grounds.pdf"
85    },
86    {
87        "name": "Library",
88        "type": "image_processing",
89        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\library model\keras_model.h5",
90        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\library model\labels.txt",
91        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Library.pdf"
92    },
93    {
94        "name": "Medical Room",
95        "type": "image_processing",
96        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\medical room model\keras_model.h5",
97        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\medical room model\labels.txt",
98        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Medical Room.pdf"
99    },
100    {
101        "name": "Parking",
102        "type": "image_processing",
103        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\parking model\keras_model.h5",
104        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\parking model\labels.txt",
105        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Parking.pdf"
106    },
107    {
108        "name": "Pothole Management",
109        "type": "image_processing",
110        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\pothole management model\keras_model.h5",
111        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\pothole management model\labels.txt",
112        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Pothole Management.pdf"
113    },
114    {
115        "name": "Restroom",
116        "type": "image_processing",
117        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\restroom model\keras_model.h5",
118        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\restroom model\labels.txt",
119        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Restroom.pdf"
120    },
121    {
122        "name": "Seminar Hall",
123        "type": "image_processing",
124        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\seminar hall model\keras_model.h5",
125        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\seminar hall model\labels.txt",
126        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Seminar Hall.pdf"
127    },
128    {
129        "name": "Server Room",
130        "type": "image_processing",
131        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\server room model\keras_model.h5",
132        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\server room model\labels.txt",
133        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Server Room.pdf"
134    },
135    {
136        "name": "Sports Equipment",
137        "type": "image_processing",
138        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\sports equipment model\keras_model.h5",
139        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\sports equipment model\labels.txt",
140        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Sports Equipment.pdf"
141    },
142    {
143        "name": "TPO (Training and Placement Office)",
144        "type": "image_processing",
145        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\tpo model\keras_model.h5",
146        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\tpo model\labels.txt",
147        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\TPO (Training and Placement Office).pdf"
148    },
149    {
150        "name": "Workshop",
151        "type": "image_processing",
152        "model": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\workshop model\keras_model.h5",
153        "labels": r"C:\Users\Pranit\PycharmProjects\infrastructure\MODELS\workshop model\labels.txt",
154        "pdf": r"C:\Users\Pranit\PycharmProjects\infrastructure\pdfs\Workshop.pdf"
155    }
156]
157
158def calculate_required_facilities(num_students, num_divisions, num_courses, course_duration):
159    # 1. Classrooms Required
160    classrooms_required = num_divisions * course_duration * 0.5
161
162    # 2. Total Labs Required
163    base_labs = 2 * num_courses * course_duration
164    if num_students > 180 * num_courses:
165        extra_students_per_course = (num_students - (180 * num_courses)) // 50
166        base_labs += extra_students_per_course
167    additional_labs = (num_students - 600) // 150 if num_students > 600 else 0
168    total_labs = base_labs + additional_labs
169
170    # 3. Workshops Required
171    workshops_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
172
173    # 4. CAD Centres Required
174    cad_centres_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
175
176    # 5. Computer Centres Required
177    computer_centres_required = 1 if num_students <= 600 else 1 + (num_students - 600) // 600
178
179    # 6. Seminar Halls Required
180    seminar_halls_required = 1
181
182    # 7. Library Required
183    library_required = 1
184
185    # 8. Language Labs Required
186    language_labs_required = 1
187
188    # 9. PCs/Laptops Required
189    pcs_laptops_required = max(20, num_students // 10)
190
191    # 10. Additional Facilities (Single Requirement)
192    auditorium_required = 1
193    faculty_cabins_required = 1
194    medical_room_required = 1
195    sports_facilities_required = 1
196    canteen_required = 1
197    generator_required = 1
198    bicycle_stand_required = 1
199    bus_transport_required = 1
200
201    # 11. Other Derived Formulas
202    dispensers_required = 1
203    fire_extinguishers_required = 1
204    grounds_required = 1
205    parking_areas_required = 1
206    road_condition_check = 1
207
208    # Returning the required facilities as a dictionary
209    return {
210        "classrooms_required": int(classrooms_required),
211        "labs_required": int(total_labs),
212        "workshops_required": int(workshops_required),
213        "cad_centres_required": int(cad_centres_required),
214        "computer_centres_required": int(computer_centres_required),
215        "seminar_halls_required": int(seminar_halls_required),
216        "library_required": int(library_required),
217        "language_labs_required": int(language_labs_required),
218        "pcs_laptops_required": int(pcs_laptops_required),
219        "auditorium_required": int(auditorium_required),
220        "faculty_cabins_required": int(faculty_cabins_required),
221        "medical_room_required": int(medical_room_required),
222        "sports_facilities_required": int(sports_facilities_required),
223        "canteen_required": int(canteen_required),
224        "generator_required": int(generator_required),
225        "bicycle_stand_required": int(bicycle_stand_required),
226        "bus_transport_required": int(bus_transport_required),
227        "dispensers_required": int(dispensers_required),
228        "fire_extinguishers_required": int(fire_extinguishers_required),
229        "grounds_required": int(grounds_required),
230        "parking_areas_required": int(parking_areas_required),
231        "road_condition_check": int(road_condition_check),
232    }
233
234
235def process_image_with_model(model_path, labels_path, image_path):
236    try:
237        model = load_model(model_path, compile=False)
238        with open(labels_path, "r") as file:
239            class_names = file.readlines()
240        image = Image.open(image_path).convert("RGB")
241        image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
242        image_array = np.asarray(image).astype(np.float32) / 127.5 - 1
243        data = np.expand_dims(image_array, axis=0)
244        prediction = model.predict(data)
245        index = np.argmax(prediction)
246        class_name = class_names[index].strip()
247        confidence_score = prediction[0][index]
248        return {"class": class_name, "confidence": round(confidence_score * 100, 2)}
249    except Exception as e:
250        return {"error": f"Failed to process image. Reason: {str(e)}"}
251
252@app.route('/', methods=['GET', 'POST'])
253def home():
254    required_facilities = {}
255
256    if request.method == 'POST' and 'calculate_facilities' in request.form:
257        # Fetch inputs for calculation
258        num_students = int(request.form['num_students'])
259        num_divisions = int(request.form['num_divisions'])
260        num_courses = int(request.form['num_courses'])
261        course_duration = int(request.form['course_duration'])
262
263        # Calculate facilities
264        required_facilities = calculate_required_facilities(
265            num_students, num_divisions, num_courses, course_duration
266        )
267
268    # Handle verification for each facility
269    elif request.method == 'POST' and 'facility' in request.form:
270        selected_facility = request.form.get('facility')
271        facility = next((f for f in FACILITIES if f['name'] == selected_facility), None)
272
273        if facility:
274            files = request.files.getlist("files")
275            if files:
276                save_dir = os.path.join(app.config['UPLOAD_FOLDER'], selected_facility)
277                os.makedirs(save_dir, exist_ok=True)
278                for file in files:
279                    if file.filename == "":
280                        flash("No file selected for uploading.", "warning")
281                        continue
282                    filename = secure_filename(file.filename)
283                    file_path = os.path.join(save_dir, filename)
284                    file.save(file_path)
285                    result = process_image_with_model(facility["model"], facility["labels"], file_path)
286                    if 'error' in result:
287                        flash(result['error'], "danger")
288                    else:
289                        flash(f"{result['class']} detected with {result['confidence']}% confidence.", "success")
290
291    return render_template(
292        'index.html',
293        facilities=FACILITIES,
294        required_facilities=required_facilities
295    )
296
297if __name__ == '__main__':
298    app.run(debug=True)
299