Skip to content

Commit 8397b91

Browse files
First commit
0 parents  commit 8397b91

File tree

2,814 files changed

+991686
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,814 files changed

+991686
-0
lines changed

app.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from flask import Flask, Response, render_template, jsonify, request
2+
import cv2
3+
import numpy as np
4+
5+
app = Flask(__name__)
6+
7+
8+
recognizer = cv2.face.LBPHFaceRecognizer_create()
9+
recognizer.read('trainer.yml')
10+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
11+
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
12+
13+
14+
cap = cv2.VideoCapture(0)
15+
16+
17+
color_recognized = (0, 255, 0)
18+
color_unrecognized = (0, 0, 255)
19+
font = cv2.FONT_HERSHEY_SIMPLEX
20+
confidence_threshold = 100
21+
22+
def preprocess_face(face):
23+
"""Preprocess the face image to improve recognition."""
24+
face = cv2.equalizeHist(face)
25+
face = cv2.GaussianBlur(face, (3, 3), 0)
26+
face = cv2.resize(face, (200, 200))
27+
return face
28+
29+
def align_face(face):
30+
"""Align the face image to improve recognition."""
31+
return face
32+
33+
def gen_frames():
34+
"""Generate frames from the webcam."""
35+
while True:
36+
ret, frame = cap.read()
37+
if not ret:
38+
break
39+
40+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
41+
42+
43+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
44+
45+
for (x, y, w, h) in faces:
46+
face = gray[y:y+h, x:x+w]
47+
preprocessed_face = preprocess_face(face)
48+
aligned_face = align_face(preprocessed_face)
49+
label, confidence = recognizer.predict(aligned_face)
50+
51+
52+
eyes = eye_cascade.detectMultiScale(face)
53+
54+
55+
if confidence < confidence_threshold and len(eyes) >= 2:
56+
color = color_recognized
57+
text = 'Welcome Sir'
58+
else:
59+
color = color_unrecognized
60+
text = 'Cannot enter'
61+
62+
63+
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
64+
cv2.putText(frame, text, (x, y-10), font, 0.9, color, 2, cv2.LINE_AA)
65+
66+
67+
ret, buffer = cv2.imencode('.jpg', frame)
68+
frame = buffer.tobytes()
69+
yield (b'--frame\r\n'
70+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
71+
72+
@app.route('/')
73+
def index():
74+
return render_template('index.html')
75+
76+
@app.route('/video_feed')
77+
def video_feed():
78+
return Response(gen_frames(),
79+
mimetype='multipart/x-mixed-replace; boundary=frame')
80+
81+
@app.route('/status', methods=['GET'])
82+
def status():
83+
84+
return jsonify({
85+
'status': 'Authentication in progress',
86+
'progress': '50%'
87+
})
88+
89+
@app.route('/start_auth', methods=['POST'])
90+
def start_auth():
91+
92+
return jsonify({'status': 'Authentication started'})
93+
94+
@app.route('/stop_auth', methods=['POST'])
95+
def stop_auth():
96+
97+
return jsonify({'status': 'Authentication stopped'})
98+
99+
if __name__ == '__main__':
100+
try:
101+
app.run(debug=True)
102+
finally:
103+
cap.release()
104+
cv2.destroyAllWindows()
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
89.3 KB
Loading
89.3 KB
Loading

face_data.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import cv2
2+
import os
3+
4+
5+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
6+
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
7+
8+
9+
output_dir = 'dataset'
10+
if not os.path.exists(output_dir):
11+
os.makedirs(output_dir)
12+
13+
14+
cap = cv2.VideoCapture(0)
15+
16+
print("Collecting images. Press 'q' to quit.")
17+
18+
count = 0
19+
user_id = input("Enter user ID: ")
20+
21+
while True:
22+
ret, frame = cap.read()
23+
if not ret:
24+
break
25+
26+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
27+
28+
29+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
30+
31+
for (x, y, w, h) in faces:
32+
face = gray[y:y+h, x:x+w]
33+
color_face = frame[y:y+h, x:x+w]
34+
35+
36+
eyes = eye_cascade.detectMultiScale(face)
37+
38+
39+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
40+
41+
42+
for (ex, ey, ew, eh) in eyes:
43+
cv2.rectangle(color_face, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
44+
45+
46+
if len(eyes) >= 2:
47+
face_filename = os.path.join(output_dir, f'user.{user_id}.{count}.jpg')
48+
cv2.imwrite(face_filename, face)
49+
print(f"Image {count} saved.")
50+
count += 1
51+
52+
53+
cv2.imshow('Collecting Images', frame)
54+
55+
56+
if cv2.waitKey(1) & 0xFF == ord('q') or count >= 10:
57+
break
58+
59+
cap.release()
60+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)