This commit is contained in:
Joseph 2025-02-03 16:20:00 +07:00
parent 38780c56a8
commit 9532845bc3
1 changed files with 23 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import requests
import face_recognition
import numpy as np
import pickle
import time
# Khởi tạo danh sách rỗng để lưu trữ thông tin người dùng
user_data = []
history = []
@ -17,6 +18,7 @@ screen_height = 1100
WINDOW_QR_CODE = "QR Code"
WINDOW_TRACKING = "Tracking"
WINDOW_HISTORY = "History"
# URL_API = "http://localhost:8000/api/v1"
URL_API = "https://ms.prology.net/api/v1"
data = [0]
# Hàm thông báo bằng giọng nói
@ -262,7 +264,10 @@ def encodeImgs(save_path="../DetectFace/encodings.pkl"):
encodeListKnow = encodeImgs()
print("Load data success")
# Khởi tạo camera
def main():
recognized_faces = {}
name_history = {}
cap = cv.VideoCapture(0)
face_cascade = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')
cv.namedWindow(WINDOW_TRACKING, cv.WINDOW_NORMAL)
@ -278,22 +283,37 @@ def main():
faceCurFrame = face_recognition.face_locations(frameS, model='hog')
encodeCurFrame = face_recognition.face_encodings(frameS)
frame = process_qr_code(frame)
current_time = time.time()
for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodeListKnow, encodeFace)
faceDis = face_recognition.face_distance(encodeListKnow, encodeFace)
# print(faceDis)
matchIndex = np.argmin(faceDis)
if faceDis[matchIndex] < 0.3:
if faceDis[matchIndex] < 0.35:
name = classNames[matchIndex].upper()
# If the face is recognized, track the timestamp
if name not in recognized_faces:
recognized_faces[name] = current_time # Store first detection time
else:
elapsed_time = current_time - recognized_faces[name]
if (name not in name_history) or (current_time - name_history[name] >= 60):
if elapsed_time >= 2.5: # If face is seen for 2s, execute script
process_face_detect(f"{name}\n{"Staff"}\n\n", frame)
name_history[name] = time.time()
del recognized_faces[name]
else:
display_text(frame, f"Checking: "+str(round((elapsed_time/2.5)*100,2))+"%", (700, 55), 1, (0, 255, 255), 2)
else:
display_text(frame, f"Checked. Try after {round(60-(current_time - name_history[name]),0)}s", (600, 55), 1, (0, 255, 255), 2)
else:
name = "Unknow"
recognized_faces = {}
display_text(frame, f"Face not found - use QRcode", (20, 55), 0.7, (6, 6, 255), 2)
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1*2, x2*2, y2*2, x1*2
cv.rectangle(frame, (x1,y1), (x2,y2), (0,255,0), 2)
cv.putText(frame, name + f"({(1 - round(faceDis[matchIndex], 2))*100}%) - CHECKED", (20, 25), cv.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 2)
cv.putText(frame, name + f"({(1 - round(faceDis[matchIndex], 2))*100}%)", (x1, y1), cv.FONT_HERSHEY_COMPLEX, 0.8, (0,255,0), 2)