File size: 2,746 Bytes
51edce2
 
0a7fa93
 
 
 
84a0b76
51edce2
 
84a0b76
 
 
75ccac1
 
 
84a0b76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
os.environ["DEEPFACE_HOME"] = "/tmp/deepface"  # Hugging Face allows writing to /tmp
import os
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
os.environ["STREAMLIT_CACHE_DIR"] = "/tmp/.streamlit/cache"

from deepface import DeepFace

import streamlit as st
from database import Database
from config import VECTOR_DB_PATH
from PIL import Image
import numpy as np
import pandas as pd

# Initialize face database
face_db = Database(db_path=VECTOR_DB_PATH)

# Helper function to extract face
def extract_face(img):
    face_dict = DeepFace.detection.extract_faces(img, detector_backend='yolov8',normalize_face=False)
    return face_dict

def register_user(name, image):
    if image is not None:
        # image = image.convert("RGB")  # Ensure RGB format
        # face_dict = extract_face(np.array(image))
        face_db.add_to_collection(image, {"name": name})
        return f"✅ User '{name}' registered successfully!"
    return "❌ Failed to register user."

def verify_user_image(image):
    if image is not None:
        # image = image.convert("RGB")  # Ensure RGB format
        # face_dict = extract_face(np.array(image))
        return face_db.verify(image)
    return "❌ Failed to verify user."

# Streamlit UI
st.set_page_config(page_title="Face Recognition App", layout="centered")
st.title("🧠 Face Recognition App")

tab1, tab2,tab3 = st.tabs(["Verify","Register","Delete"])


with tab1:
    st.subheader("Verify User")
    # verify_image = st.file_uploader("Upload or Capture Image", type=["jpg", "jpeg", "png"], key="verify")
    verify_image=st.camera_input("capture the webcam image")
    if verify_image:
        image = Image.open(verify_image).convert("RGB")
        face_dict=extract_face(np.array(image))
        st.image(face_dict[0]['face'], caption="Uploaded Image", use_container_width=True)
        if st.button("Verify"):
            status = verify_user_image(face_dict[0]['face'])
            # st.info(status)
            st.json(status,expanded=2)
        

with tab2:
    st.subheader("Register New User")
    name = st.text_input("Enter your name")
    uploaded_image = st.camera_input("Upload or Capture Image")
    if uploaded_image:
        image = Image.open(uploaded_image).convert("RGB")
        face_dict=extract_face(np.array(image))
        st.image(face_dict[0]['face'], caption="Uploaded Image", use_container_width=True)
        if st.button("Register"):
            status = register_user(name, face_dict[0]['face'])
            st.success(status)
            


with tab3:
    st.subheader("Delete User")
    record_id=st.text_input("enter the record id")
    if st.button(label="Delete"):
        status=face_db.delete_record(id=record_id)
        st.info(status)