Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.0 kB
2
Indexable
Never
import cv2
import sqlite3
import numpy as np
import time

def one():
    conn = sqlite3.connect('test.db')
    c = conn.cursor()
    start_time = time.time()



    c.execute('''CREATE TABLE IF NOT EXISTS faces (id INTEGER PRIMARY KEY, face BLOB)''')


    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')


    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()
    

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    
        for i, (x, y, w, h) in enumerate(faces):
            face_roi = frame[y:y+h, x:x+w]
            _, buffer = cv2.imencode('.jpg', face_roi)
            face_data = buffer.tobytes()
        

            c.execute("INSERT INTO faces (face) VALUES (?)", (face_data,))
    

        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        
        cv2.imshow('Face Detection', frame)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        elapsed_time = time.time() - start_time
        if elapsed_time > 5:
            break


    cap.release()
    conn.commit()
    conn.close()
    cv2.destroyAllWindows()

def two():
    conn = sqlite3.connect('test.db')
    c = conn.cursor()

    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)

    c.execute("SELECT id, face FROM faces")
    faces_data = c.fetchall()

    while True:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

        for (x, y, w, h) in faces:
            face_roi = gray[y:y+h, x:x+w]

            for face_id, stored_face_blob in faces_data:
                stored_face = np.frombuffer(stored_face_blob, dtype=np.uint8)
                stored_face_resized = cv2.resize(stored_face, (w, h))

                result = cv2.matchTemplate(face_roi, stored_face_resized, cv2.TM_CCOEFF_NORMED)
                _, max_val, _, _ = cv2.minMaxLoc(result)

                print(f"Face ID: {face_id} - Max value: {max_val}")

                if max_val > 0.7:  # Adjust the threshold as needed
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
                    cv2.putText(frame, f'Face ID: {face_id} - Face detected.', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                    print(f"Face ID: {face_id} - Face detected.")

        cv2.imshow('Face Detection', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    conn.close()
    cv2.destroyAllWindows()





def run():
    inp=input()
    if inp == "1":
        one()
    if inp == "2":
        two()

        

run()

Leave a Comment