Skip to content

Commit

Permalink
Merge pull request #1 from Sookeyy-12/main
Browse files Browse the repository at this point in the history
updated best
  • Loading branch information
Sookeyy-12 authored Jan 11, 2025
2 parents be10216 + 15db8ce commit 27c89d5
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dlib
venv
Faces/*
windows
42 changes: 42 additions & 0 deletions AntiSpoofing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from ultralytics import YOLO
import cv2
import cvzone
import math
import time

confidence = 0.9

cap = cv2.VideoCapture(0) # for webcam
cap.set(3, 640)
cap.set(4, 480)
# cap = cv2.VideoCapture("test.mp4") # for video file

model = YOLO("models/best.pt")
model.overrides['verbose'] = False

classNames = ["fake", "real"]

while True:
success, img = cap.read()
results = model(img, stream=True)
for r in results:
boxes = r.boxes
for box in boxes:
# Confidence
conf = math.ceil((box.conf[0] * 100))/100
# class name
if conf > confidence:
cls = int(box.cls[0])
if classNames[cls] == "real":
print("Real")
else:
print("Spoof")

cv2.imshow("Image", img)
key = cv2.waitKey(1) & 0xFF

# Exit if 'q' key is pressed
if key == ord("q"):
break

cv2.destroyAllWindows()
45 changes: 45 additions & 0 deletions FaceRecognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import cv2
import os
import pickle
import face_recognition
import numpy as np
import cvzone

cap = cv2.VideoCapture(0)
cap.set(3, 640) # Set width
cap.set(4, 480) # Set height

# Load the Encoding File
print("Loading encodings...")
encodings = open("GeneratedEncodings.p", "rb")
encodingListKnownWithIDs = pickle.load(encodings)
encodings.close()
encodeListKnown, studentIDs = encodingListKnownWithIDs
print("Encodings loaded.")

while True:
success, img = cap.read()

imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

faceCurrentFrame = face_recognition.face_locations(imgS)
encodeCurrentFrame = face_recognition.face_encodings(imgS,faceCurrentFrame)

for encodeFace, faceLocation in zip(encodeCurrentFrame, faceCurrentFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDistance = face_recognition.face_distance(encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDistance)
if matches[matchIndex]:
print("Known Face Detected")
print(studentIDs[matchIndex])


cv2.imshow("Face Attendance", img)

# Wait for a key press to break the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
Binary file added GeneratedEncodings.p
Binary file not shown.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# DoorAutoCompiled
# Should be the Correct One

## How to Run:

Note: if you windows, you will first need to build `dlib` using wheel.

1. create a virtual environment
```bash
python -m venv venv
```

2. install requirements
```bash
pip install -r reqs.txt
```
This 100% works on windows, but might need calibration in linux.

3. run the app
```bash
python main.py
```
34 changes: 34 additions & 0 deletions encodeGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cv2
import face_recognition
import pickle
import os

### Importing the Faces
folderPath = "Faces"
pathList = os.listdir(folderPath)
imgList = []
studentIDs = []
for path in pathList:
imgList.append(cv2.imread(os.path.join(folderPath, path)))
studentIDs.append(os.path.splitext(path)[0])

### Encodings Generator ###
def findEncodings(imagesList):
encodeList = []
for img in imagesList:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)

return encodeList

print("Encoding Started...")
encodeListKnown = findEncodings(imgList)
encodingListKnownWithIDs = (encodeListKnown, studentIDs)
print("Encoding Complete.")

file = open("GeneratedEncodings.p", "wb")
pickle.dump(encodingListKnownWithIDs, file)
file.close()

print("Encodings Saved.")
70 changes: 70 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import cv2
import pickle
import face_recognition
import numpy as np
import math
from ultralytics import YOLO

# Initialize video capture
cap = cv2.VideoCapture(0)
cap.set(3, 640) # Set width
cap.set(4, 480) # Set height

# Load the Encoding File
print("Loading encodings...")
encodings = open("GeneratedEncodings.p", "rb")
encodingListKnownWithIDs = pickle.load(encodings)
encodings.close()
encodeListKnown, studentIDs = encodingListKnownWithIDs
print("Encodings loaded.")

# Load the anti-spoofing model
model = YOLO("models/best.pt")
model.overrides['verbose'] = False
classNames = ["fake", "real"]
confidence = 0.9

while True:
success, img = cap.read()

# Anti-spoofing check
results = model(img, stream=True)
is_real = False
for r in results:
boxes = r.boxes
for box in boxes:
conf = math.ceil((box.conf[0] * 100))/100
if conf > confidence:
cls = int(box.cls[0])
if classNames[cls] == "real":
is_real = True
break
if is_real:
break

if is_real:
# Face recognition
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

faceCurrentFrame = face_recognition.face_locations(imgS)
encodeCurrentFrame = face_recognition.face_encodings(imgS, faceCurrentFrame)

for encodeFace, faceLocation in zip(encodeCurrentFrame, faceCurrentFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDistance = face_recognition.face_distance(encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDistance)
if matches[matchIndex]:
print("Known Face Detected")
print(studentIDs[matchIndex])
else:
print("Spoofing Attempt Detected")

cv2.imshow("Face Attendance", img)

# Wait for a key press to break the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
Binary file added models/best.pt
Binary file not shown.
Binary file added reqs.txt
Binary file not shown.
Binary file added requirements-win.txt
Binary file not shown.
Binary file added requirements.txt
Binary file not shown.

0 comments on commit 27c89d5

Please sign in to comment.