-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
620 lines (502 loc) · 20.5 KB
/
app.py
File metadata and controls
620 lines (502 loc) · 20.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
from flask import Flask, render_template, Response, request, redirect, url_for, flash, jsonify, session
import cv2
import os
import numpy as np
import mediapipe as mp
from keras_facenet import FaceNet
import pickle
from datetime import datetime, timedelta
import time
import ssl
from dotenv import load_dotenv
load_dotenv()
import threading
from queue import Queue
from twilio.rest import Client
from email.message import EmailMessage
import smtplib
from playsound import playsound
from werkzeug.security import generate_password_hash, check_password_hash
import secrets
import base64
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, ForeignKey, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from io import BytesIO
from PIL import Image
# Initialize queue for background processing
task_queue = Queue()
# Flask application setup
app = Flask(__name__)
app.secret_key = secrets.token_hex(24)
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=2)
# SQLite Database Configuration
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_PATH, "database.db")
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Create the SQLAlchemy engine
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
Base = declarative_base()
# Create session factory
session_factory = sessionmaker(bind=engine)
db_session = scoped_session(session_factory)
# Division access code for registration
DIVISION_ACCESS_CODE = os.getenv("DIVISION_ACCESS_CODE", "POLICE2024")
# Email Configuration
EMAIL_SENDER = os.getenv("EMAIL_ADDRESS")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
EMAIL_RECEIVER = os.getenv("RECIPIENT_EMAIL")
# Twilio Configuration
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_PHONE_NUMBER = os.getenv("TWILIO_FROM_NUMBER")
RECIPIENT_PHONE_NUMBER = os.getenv("TWILIO_TO_NUMBER")
# Directories
DETECTED_FACES_FOLDER = os.path.join(BASE_PATH, "detected_faces")
TIME_DATA_FOLDER = os.path.join(BASE_PATH, "time_data")
EMBEDDINGS_FILE = os.path.join(BASE_PATH, "embeddings.pkl")
UPLOAD_FOLDER = os.path.join(BASE_PATH, "uploads")
DATASET_FOLDER = os.path.join(BASE_PATH, "dataset")
os.makedirs(DETECTED_FACES_FOLDER, exist_ok=True)
os.makedirs(TIME_DATA_FOLDER, exist_ok=True)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(DATASET_FOLDER, exist_ok=True)
# SQLAlchemy Models
class Officer(Base):
__tablename__ = 'officers'
id = Column(Integer, primary_key=True)
badge_number = Column(String(10), unique=True, nullable=False)
full_name = Column(String(100), nullable=False)
rank = Column(String(50), nullable=False)
email = Column(String(100), unique=True, nullable=False)
password_hash = Column(String(255), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
class Suspect(Base):
__tablename__ = 'suspects'
id = Column(Integer, primary_key=True)
suspect_name = Column(String(100), nullable=False)
image_path = Column(String(255), nullable=False)
detected_time = Column(DateTime, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
# Load Face Recognition Model
embedder = FaceNet()
mp_face_detection = mp.solutions.face_detection
face_detector = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5)
# Load known faces embeddings
try:
with open(EMBEDDINGS_FILE, "rb") as f:
known_faces = pickle.load(f)
except (FileNotFoundError, EOFError):
known_faces = {}
# Initialize database
def init_db():
try:
Base.metadata.create_all(engine)
print("Database initialized successfully")
except Exception as e:
print(f"Error initializing database: {e}")
# Face recognition functions
def extract_face(img):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = face_detector.process(img_rgb)
faces = []
if results.detections:
for detection in results.detections:
bboxC = detection.location_data.relative_bounding_box
h, w, _ = img.shape
x, y, width, height = (int(bboxC.xmin * w), int(bboxC.ymin * h),
int(bboxC.width * w), int(bboxC.height * h))
face = img_rgb[y:y + height, x:x + width]
if face.shape[0] > 0 and face.shape[1] > 0:
faces.append((face, (x, y, width, height)))
return faces
def recognize_face(face_embedding):
face_embedding = face_embedding / np.linalg.norm(face_embedding)
min_dist = float("inf")
name = "Unknown"
for person, embeddings in known_faces.items():
for saved_embedding in embeddings:
dist = np.linalg.norm(face_embedding - saved_embedding)
if dist < 0.7 and dist < min_dist:
min_dist = dist
name = person
return name
# Alert mechanisms
def make_alert_call(name, timestamp):
"""Make a phone call alert when a suspect is detected"""
try:
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
# Create a TwiML response with text-to-speech
twiml = f"""
<Response>
<Say>Alert! Suspect {name} has been detected at {timestamp}. Please check your email for more details.</Say>
<Pause length="1"/>
<Say>Repeating: Suspect {name} has been detected.</Say>
</Response>
"""
# Make the call
call = client.calls.create(
twiml=twiml,
to=RECIPIENT_PHONE_NUMBER,
from_=TWILIO_PHONE_NUMBER
)
print(f"Phone alert initiated for suspect: {name}, Call SID: {call.sid}")
return True
except Exception as e:
print(f"Error making phone call: {e}")
return False
def send_email_alert(name, timestamp, face_path):
if not EMAIL_SENDER or not EMAIL_PASSWORD or not EMAIL_RECEIVER:
print("Email configuration not set up")
return
subject = f"Suspect Detected: {name}"
body = f"A suspect has been detected!!!!.\n\nName: {name}\nTime: {timestamp}"
msg = EmailMessage()
msg['From'] = EMAIL_SENDER
msg['To'] = EMAIL_RECEIVER
msg['Subject'] = subject
msg.set_content(body)
# Attach the detected face image
try:
with open(face_path, 'rb') as img:
img_data = img.read()
img_name = os.path.basename(face_path)
msg.add_attachment(img_data, maintype='image', subtype='jpeg', filename=img_name)
except Exception as e:
print(f"Error attaching image: {e}")
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(EMAIL_SENDER, EMAIL_PASSWORD)
smtp.sendmail(EMAIL_SENDER, EMAIL_RECEIVER, msg.as_string())
print(f"Email alert sent for suspect: {name}")
except Exception as e:
print(f"Email sending failed: {e}")
def process_alerts():
"""Thread to process suspect alerts asynchronously"""
while True:
task = task_queue.get()
if task is None:
break # Stop the thread when None is added to the queue
name, timestamp, face_path = task
try:
# Save suspect in SQLite database
session = db_session()
suspect = Suspect(
suspect_name=name,
image_path=face_path,
detected_time=datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
)
session.add(suspect)
session.commit()
session.close()
# Send Email (if configured)
if EMAIL_SENDER and EMAIL_PASSWORD and EMAIL_RECEIVER:
send_email_alert(name, timestamp, face_path)
# Make phone call alert (if configured)
if TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN and TWILIO_PHONE_NUMBER:
make_alert_call(name, timestamp)
except Exception as e:
print(f"Error processing alert: {e}")
task_queue.task_done()
# Start the background thread
alert_thread = threading.Thread(target=process_alerts, daemon=True)
alert_thread.start()
# Video processing functions
def extract_faces_from_video(name, video_path, num_images=500):
if not os.path.exists(video_path):
return "Error: Video file does not exist."
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return "Error: Unable to open video file."
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
step = max(1, total_frames // num_images)
output_dir = os.path.join(DATASET_FOLDER, name)
os.makedirs(output_dir, exist_ok=True)
frame_count = 0
saved_images = 0
new_embeddings = []
while saved_images < num_images:
ret, frame = cap.read()
if not ret:
break
if frame_count % step != 0:
frame_count += 1
continue
frame_count += 1
faces = extract_face(frame)
for face, _ in faces:
face_resized = cv2.resize(face, (160, 160))
embedding = embedder.embeddings([face_resized])[0]
new_embeddings.append(embedding)
image_path = os.path.join(output_dir, f"face_{saved_images}.jpg")
cv2.imwrite(image_path, cv2.cvtColor(face_resized, cv2.COLOR_RGB2BGR))
saved_images += 1
cap.release()
# Save embeddings to embeddings.pkl
if new_embeddings:
if os.path.exists(EMBEDDINGS_FILE):
with open(EMBEDDINGS_FILE, "rb") as f:
current_known_faces = pickle.load(f)
else:
current_known_faces = {}
if name in current_known_faces:
current_known_faces[name].extend(new_embeddings)
else:
current_known_faces[name] = new_embeddings
with open(EMBEDDINGS_FILE, "wb") as f:
pickle.dump(current_known_faces, f)
# Update global known_faces
global known_faces
known_faces = current_known_faces
return f"Extracted {len(new_embeddings)} face embeddings for {name} and saved in {EMBEDDINGS_FILE}."
return "No faces detected in the video."
alerts = []
last_detection_time = {}
# NEW: Process frame from client-side camera
def process_frame(frame_data):
"""Process a single frame from client-side camera"""
global alerts, last_detection_time
try:
# Decode base64 image
image_data = base64.b64decode(frame_data.split(',')[1])
image = Image.open(BytesIO(image_data))
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# Reload known faces dynamically
if os.path.exists(EMBEDDINGS_FILE):
with open(EMBEDDINGS_FILE, "rb") as f:
current_known_faces = pickle.load(f)
global known_faces
known_faces = current_known_faces
detections = []
faces = extract_face(frame)
for face, (x, y, width, height) in faces:
face_resized = cv2.resize(face, (160, 160))
face_embedding = embedder.embeddings([face_resized])[0]
name = recognize_face(face_embedding)
detections.append({
'name': name,
'bbox': {'x': x, 'y': y, 'width': width, 'height': height}
})
current_time = time.time()
if name != "Unknown" and (name not in last_detection_time or current_time - last_detection_time[name] >= 10):
last_detection_time[name] = current_time
person_folder = os.path.join(DETECTED_FACES_FOLDER, name)
os.makedirs(person_folder, exist_ok=True)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
face_count = len(os.listdir(person_folder))
face_filename = f"img_{face_count + 1}.jpg"
face_path = os.path.join(person_folder, face_filename)
cv2.imwrite(face_path, cv2.cvtColor(face, cv2.COLOR_RGB2BGR))
time_data_path = os.path.join(TIME_DATA_FOLDER, f"{name}.txt")
with open(time_data_path, "a") as f:
f.write(f"{timestamp}\n")
alerts.append({"name": name, "time": timestamp})
# Add task to the queue instead of blocking frame processing
task_queue.put((name, timestamp, face_path))
return detections
except Exception as e:
print(f"Error processing frame: {e}")
return []
# REMOVED: The old generate_frames function that used cv2.VideoCapture(0)
# Routes for authentication
@app.route('/')
def login():
if 'badge_number' in session:
return redirect(url_for('landing'))
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login_post():
badge_number = request.form.get('badge_number')
password = request.form.get('password')
if not badge_number or not password:
flash('Please provide both badge number and password', 'error')
return redirect(url_for('login'))
try:
db = db_session()
officer = db.query(Officer).filter_by(badge_number=badge_number).first()
if officer and check_password_hash(officer.password_hash, password):
session.permanent = True
session['badge_number'] = badge_number
session['full_name'] = officer.full_name
session['rank'] = officer.rank
flash(f'Welcome, {officer.rank} {officer.full_name}!', 'success')
return redirect(url_for('landing'))
else:
flash('Invalid credentials. Please try again.', 'error')
except Exception as e:
flash(f'Error during login: {str(e)}', 'error')
finally:
db.close()
return redirect(url_for('login'))
@app.route('/register')
def register():
return render_template('registration.html')
@app.route('/register', methods=['POST'])
def register_post():
badge_number = request.form.get('badge_number')
full_name = request.form.get('full_name')
rank = request.form.get('rank')
email = request.form.get('email')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
division_code = request.form.get('division_code')
# Validate input
if not all([badge_number, full_name, rank, email, password, confirm_password, division_code]):
flash('All fields are required', 'error')
return redirect(url_for('register'))
if password != confirm_password:
flash('Passwords do not match', 'error')
return redirect(url_for('register'))
if division_code != DIVISION_ACCESS_CODE:
flash('Invalid division access code', 'error')
return redirect(url_for('register'))
try:
db = db_session()
# Check if badge number or email already exists
existing_user = db.query(Officer).filter(
(Officer.badge_number == badge_number) | (Officer.email == email)
).first()
if existing_user:
flash('Badge number or email already registered', 'error')
return redirect(url_for('register'))
# Create new officer
password_hash = generate_password_hash(password)
new_officer = Officer(
badge_number=badge_number,
full_name=full_name,
rank=rank,
email=email,
password_hash=password_hash
)
db.add(new_officer)
db.commit()
flash('Registration successful! You can now log in', 'success')
return redirect(url_for('login'))
except Exception as e:
flash(f'Error during registration: {str(e)}', 'error')
return redirect(url_for('register'))
finally:
db.close()
@app.route('/logout')
def logout():
session.clear()
flash('You have been logged out', 'info')
return redirect(url_for('login'))
# Routes for application features
@app.route('/landing')
def landing():
if 'badge_number' not in session:
return redirect(url_for('login'))
return render_template('landing.html')
@app.route('/get_alerts')
def get_alerts():
global alerts
return jsonify(alerts)
@app.route('/suspects')
def get_suspects():
if 'badge_number' not in session:
return redirect(url_for('login'))
try:
db = db_session()
suspects = db.query(Suspect).order_by(Suspect.detected_time.desc()).all()
# Add base64 image data for each suspect
suspects_with_images = []
for suspect in suspects:
suspect_dict = {
'id': suspect.id,
'suspect_name': suspect.suspect_name,
'image_path': suspect.image_path,
'detected_time': suspect.detected_time,
'created_at': suspect.created_at
}
try:
with open(suspect.image_path, 'rb') as img_file:
img_data = img_file.read()
suspect_dict['image_b64'] = base64.b64encode(img_data).decode('utf-8')
except Exception as e:
print(f"Error reading image file: {e}")
suspect_dict['image_b64'] = ''
suspects_with_images.append(suspect_dict)
return render_template('suspects.html', suspects=suspects_with_images)
except Exception as e:
flash(f'Error retrieving suspects: {str(e)}', 'error')
return redirect(url_for('landing'))
finally:
db.close()
@app.route('/live-mon')
def live_mon():
if 'badge_number' not in session:
return redirect(url_for('login'))
global alerts
alerts = [] # Clear previous alerts
return render_template("livemon.html")
# NEW: Route to process frames from client-side camera
@app.route('/process_frame', methods=['POST'])
def process_frame_route():
if 'badge_number' not in session:
return jsonify({'error': 'Not authenticated'}), 401
try:
data = request.get_json()
frame_data = data.get('frame')
if not frame_data:
return jsonify({'error': 'No frame data provided'}), 400
detections = process_frame(frame_data)
return jsonify({'detections': detections})
except Exception as e:
print(f"Error in process_frame_route: {e}")
return jsonify({'error': 'Frame processing failed'}), 500
# REMOVED: /video_feed route since we're not using server-side camera
@app.route('/delete_face', methods=['GET', 'POST'])
def delete_face():
if 'badge_number' not in session:
return redirect(url_for('login'))
embeddings_path = EMBEDDINGS_FILE
# Load existing data
try:
with open(embeddings_path, 'rb') as f:
embeddings = pickle.load(f)
except Exception as e:
print("⚠️ Error loading embeddings:", e)
embeddings = {}
if request.method == 'POST':
name_to_delete = request.form['name']
if name_to_delete in embeddings:
del embeddings[name_to_delete]
with open(embeddings_path, 'wb') as f:
pickle.dump(embeddings, f)
# Update global known_faces
global known_faces
known_faces = embeddings
message = f"✅ Deleted: {name_to_delete}"
else:
message = f"❌ Name '{name_to_delete}' not found."
return render_template("delete_face.html", names=list(embeddings.keys()), message=message)
return render_template("delete_face.html", names=list(embeddings.keys()), message=None)
@app.route('/contact')
def contact():
if 'badge_number' not in session:
return redirect(url_for('login'))
return render_template('contact.html')
@app.route('/new-crim', methods=['GET', 'POST'])
def newcrim():
if 'badge_number' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
name = request.form.get("name")
file = request.files["video"]
if file and name:
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
result = extract_faces_from_video(name, filepath)
flash(result)
return redirect(url_for('newcrim'))
return render_template('newcrim.html')
# Cleanup function for the application
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if __name__ == "__main__":
init_db() # Initialize database tables on startup
app.run(host="0.0.0.0", debug=True)