Spaces:
Sleeping
Sleeping
Upload 12 files
Browse files- UserDetail.py +5 -0
- app.py +17 -0
- clear.py +29 -0
- db.py +58 -0
- encdec.py +14 -0
- home.py +60 -0
- image.py +64 -0
- known_user/.nomedia +0 -0
- login.py +25 -0
- register.py +44 -0
- requirements.txt +49 -0
- unknown_user/.nomedia +0 -0
UserDetail.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class UserDetail:
|
2 |
+
def __init__(self, name, dob, city):
|
3 |
+
self.name = name
|
4 |
+
self.dob = dob
|
5 |
+
self.city = city
|
app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import clear
|
3 |
+
import home
|
4 |
+
import register
|
5 |
+
import login
|
6 |
+
|
7 |
+
homeTab, registerTab, loginTab, clear_tab = st.tabs(["Home", "Register", "Login", "Clear"])
|
8 |
+
|
9 |
+
|
10 |
+
with homeTab:
|
11 |
+
home.home()
|
12 |
+
with registerTab:
|
13 |
+
register.register()
|
14 |
+
with loginTab:
|
15 |
+
login.login()
|
16 |
+
with clear_tab:
|
17 |
+
clear.clear()
|
clear.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import encdec
|
4 |
+
import shutil
|
5 |
+
|
6 |
+
|
7 |
+
def clear():
|
8 |
+
passowrd = st.text_input("Enter a password", type="password")
|
9 |
+
clearbtn = st.button("clear")
|
10 |
+
known = os.path.join(os.path.dirname(os.path.abspath(__file__)), "known_user")
|
11 |
+
unknown = os.path.join(os.path.dirname(os.path.abspath(__file__)), "unknown_user")
|
12 |
+
db = os.path.join(os.path.dirname(os.path.abspath(__file__)), "db.sqlite")
|
13 |
+
if clearbtn and passowrd == encdec.encdec():
|
14 |
+
try:
|
15 |
+
if os.path.exists(known):
|
16 |
+
shutil.rmtree(known)
|
17 |
+
os.makedirs(known)
|
18 |
+
if os.path.exists(unknown):
|
19 |
+
shutil.rmtree(unknown)
|
20 |
+
os.makedirs(unknown)
|
21 |
+
if os.path.exists(db):
|
22 |
+
os.remove(db)
|
23 |
+
st.success("Cleared")
|
24 |
+
except Exception as e:
|
25 |
+
st.error(e)
|
26 |
+
if clearbtn and passowrd != encdec.encdec():
|
27 |
+
st.error("Password entered is incorrect")
|
28 |
+
|
29 |
+
|
db.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
import streamlit
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
from UserDetail import UserDetail
|
7 |
+
|
8 |
+
|
9 |
+
class Database:
|
10 |
+
def __init__(self):
|
11 |
+
self.conn = sqlite3.connect('./db.sqlite')
|
12 |
+
self.create_table()
|
13 |
+
|
14 |
+
def __del__(self):
|
15 |
+
self.conn.close()
|
16 |
+
|
17 |
+
def create_table(self):
|
18 |
+
c = self.conn.cursor()
|
19 |
+
c.execute('''
|
20 |
+
CREATE TABLE IF NOT EXISTS user_detail (
|
21 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
22 |
+
name TEXT,
|
23 |
+
dob DATE,
|
24 |
+
city TEXT
|
25 |
+
)
|
26 |
+
''')
|
27 |
+
self.conn.commit()
|
28 |
+
c.close()
|
29 |
+
|
30 |
+
def insert_user_detail(self, userDetail):
|
31 |
+
c = self.conn.cursor()
|
32 |
+
c.execute('''
|
33 |
+
INSERT INTO user_detail (name, dob, city) VALUES (?, ?, ?)
|
34 |
+
''', (userDetail.name, userDetail.dob, userDetail.city))
|
35 |
+
self.conn.commit()
|
36 |
+
user_id = c.lastrowid
|
37 |
+
c.close()
|
38 |
+
return user_id
|
39 |
+
|
40 |
+
def get_user_detail(self, user_id):
|
41 |
+
c = self.conn.cursor()
|
42 |
+
c.execute('''
|
43 |
+
SELECT * FROM user_detail WHERE id = ?
|
44 |
+
''', user_id)
|
45 |
+
user_detail = self.fetch_data_and_map_to_objects(c)
|
46 |
+
c.close()
|
47 |
+
return user_detail
|
48 |
+
|
49 |
+
def fetch_data_and_map_to_objects(self, cursor):
|
50 |
+
row = cursor.fetchone()
|
51 |
+
|
52 |
+
name = row[1]
|
53 |
+
dob = row[2]
|
54 |
+
city = row[3]
|
55 |
+
|
56 |
+
obj = UserDetail(name, dob, city)
|
57 |
+
|
58 |
+
return obj
|
encdec.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from cryptography.fernet import Fernet
|
2 |
+
|
3 |
+
|
4 |
+
kbyte = b'wYZiHRIKIzWAAOa4J8-UNbq9Blj5IfKY6SRvDxIYnJ0='
|
5 |
+
encmsg = b'gAAAAABmHDtNQ7Z-6DmU8XIYGzSCof3DYHLLvWzuBNDzwXTBeWA7E65iTWXv0s_IFwGko7NP9GTreuTN5VpSazUod5wcYxzvyA=='
|
6 |
+
|
7 |
+
def decrypt_message(key, encrypted_message):
|
8 |
+
cipher_suite = Fernet(key)
|
9 |
+
decrypted_message = cipher_suite.decrypt(encrypted_message).decode()
|
10 |
+
return decrypted_message
|
11 |
+
|
12 |
+
def encdec():
|
13 |
+
decrypted = decrypt_message(kbyte, encmsg)
|
14 |
+
return decrypted
|
home.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
def home():
|
4 |
+
st.markdown("""
|
5 |
+
|
6 |
+
# Face Recognition Web App
|
7 |
+
|
8 |
+
A web application using face recognition in Python with Streamlit.
|
9 |
+
|
10 |
+
## Introduction
|
11 |
+
|
12 |
+
This project utilizes the face recognition Python module to build a web app powered by Streamlit. It allows users to perform face recognition tasks interactively.
|
13 |
+
|
14 |
+
## Features
|
15 |
+
|
16 |
+
- Face detection and recognition
|
17 |
+
- Streamlit for a user-friendly web interface
|
18 |
+
|
19 |
+
## Installation
|
20 |
+
|
21 |
+
1. Clone the repository:
|
22 |
+
|
23 |
+
```bash
|
24 |
+
git clone https://github.com/CrimsonREwind/face-recognition-web-app.git
|
25 |
+
cd face_recognition
|
26 |
+
```
|
27 |
+
|
28 |
+
2. Install dependencies:
|
29 |
+
|
30 |
+
```bash
|
31 |
+
pip install -r requirements.txt
|
32 |
+
```
|
33 |
+
|
34 |
+
3. Run the Streamlit app:
|
35 |
+
|
36 |
+
```bash
|
37 |
+
streamlit run app.py
|
38 |
+
```
|
39 |
+
|
40 |
+
Visit `http://localhost:8501` in your browser to access the web app.
|
41 |
+
|
42 |
+
## Usage
|
43 |
+
|
44 |
+
- Open the web app in your browser.
|
45 |
+
- You can use register page to register your face in database
|
46 |
+
- You can use login page to see if user is registered or not
|
47 |
+
- All the activities like login, register will be stored in log file
|
48 |
+
|
49 |
+
## Contributing
|
50 |
+
|
51 |
+
Feel free to contribute to the project! If you find bugs, have feature requests, or want to improve the code, follow these steps:
|
52 |
+
|
53 |
+
1. Fork the repository.
|
54 |
+
2. Create a new branch: `git checkout -b feature/bugfix-your-feature`.
|
55 |
+
3. Commit your changes: `git commit -m 'Add some feature'`.
|
56 |
+
4. Push to the branch: `git push origin feature/bugfix-your-feature`.
|
57 |
+
5. Submit a pull request.
|
58 |
+
|
59 |
+
|
60 |
+
""")
|
image.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import face_recognition as fr
|
3 |
+
|
4 |
+
|
5 |
+
def save_image(picture, directory, filename):
|
6 |
+
if not os.path.exists(directory):
|
7 |
+
os.makedirs(directory)
|
8 |
+
|
9 |
+
if picture:
|
10 |
+
with open(directory + filename + ".jpg", "wb") as f:
|
11 |
+
f.write(picture.getvalue())
|
12 |
+
|
13 |
+
|
14 |
+
def delete_image(image_path):
|
15 |
+
if os.path.exists(image_path):
|
16 |
+
os.remove(image_path)
|
17 |
+
|
18 |
+
|
19 |
+
def get_all_images(directory):
|
20 |
+
return [file for file in os.listdir(directory) if file.endswith('.jpg') or file.endswith('.png')]
|
21 |
+
|
22 |
+
|
23 |
+
def compare_faces_in_directory(known_image_dir, unknown_image_dir):
|
24 |
+
# Get list of image files in the directory
|
25 |
+
known_image_files = get_all_images(known_image_dir)
|
26 |
+
unknown_image_files = get_all_images(unknown_image_dir)
|
27 |
+
|
28 |
+
if not unknown_image_files:
|
29 |
+
return False, -1
|
30 |
+
|
31 |
+
# Iterate over image files
|
32 |
+
for j, known_image_file in enumerate(known_image_files):
|
33 |
+
# Load images
|
34 |
+
unknown_img_path = os.path.join(unknown_image_dir, unknown_image_files[0])
|
35 |
+
known_img_path = os.path.join(known_image_dir, known_image_file)
|
36 |
+
image1 = fr.load_image_file(unknown_img_path)
|
37 |
+
image2 = fr.load_image_file(known_img_path)
|
38 |
+
|
39 |
+
# Find face encodings
|
40 |
+
face_encodings1 = fr.face_encodings(image1)
|
41 |
+
face_encodings2 = fr.face_encodings(image2)
|
42 |
+
|
43 |
+
if len(face_encodings1) > 0 and len(face_encodings2) > 0:
|
44 |
+
# Compare the first face encoding from each image
|
45 |
+
face_encoding1 = face_encodings1[0]
|
46 |
+
face_encoding2 = face_encodings2[0]
|
47 |
+
|
48 |
+
# Calculate similarity
|
49 |
+
similarity = fr.face_distance([face_encoding1], face_encoding2)
|
50 |
+
|
51 |
+
# Set a threshold for similarity
|
52 |
+
threshold = 0.4
|
53 |
+
|
54 |
+
# Output comparison result
|
55 |
+
user_id = os.path.splitext(known_image_file)[0]
|
56 |
+
if similarity[0] < threshold:
|
57 |
+
print(f"Images {unknown_image_files[0]} and {known_image_file} have similar faces.")
|
58 |
+
return True, user_id
|
59 |
+
else:
|
60 |
+
print(f"Images {unknown_image_files[0]} and {known_image_file} do not have similar faces.")
|
61 |
+
else:
|
62 |
+
print(f"No faces found in one or both images.")
|
63 |
+
|
64 |
+
return False, -1
|
known_user/.nomedia
ADDED
File without changes
|
login.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
import image
|
4 |
+
from db import Database
|
5 |
+
|
6 |
+
|
7 |
+
def login():
|
8 |
+
picture = st.camera_input("picture",key = "registercam", label_visibility='hidden')
|
9 |
+
|
10 |
+
if picture:
|
11 |
+
unknown_user_dir = "./unknown_user/"
|
12 |
+
unknown_user_name = "unknown_user"
|
13 |
+
image.save_image(picture, unknown_user_dir, unknown_user_name)
|
14 |
+
|
15 |
+
is_match, user_id = image.compare_faces_in_directory("./known_user/", unknown_user_dir)
|
16 |
+
|
17 |
+
if is_match:
|
18 |
+
db = Database()
|
19 |
+
st.write(user_id)
|
20 |
+
user_detail = db.get_user_detail(user_id)
|
21 |
+
st.write(user_detail)
|
22 |
+
else:
|
23 |
+
st.error("No Match Found")
|
24 |
+
|
25 |
+
image.delete_image(unknown_user_dir + unknown_user_name)
|
register.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import face_recognition as fr
|
3 |
+
from UserDetail import UserDetail
|
4 |
+
from db import Database
|
5 |
+
import datetime
|
6 |
+
import image
|
7 |
+
|
8 |
+
|
9 |
+
def register():
|
10 |
+
options = ["Camera", "Upload"]
|
11 |
+
initial_selection = options.index("Upload")
|
12 |
+
method = st.radio("Method", options, index=initial_selection)
|
13 |
+
if method == "Camera":
|
14 |
+
picture = st.camera_input("picture", key="loginPic", label_visibility='hidden')
|
15 |
+
if method == "Upload":
|
16 |
+
picture = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])
|
17 |
+
|
18 |
+
if picture:
|
19 |
+
form = st.form("Register")
|
20 |
+
name = form.text_input("Username")
|
21 |
+
min_date = datetime.datetime(1900, 1, 1)
|
22 |
+
max_date = datetime.datetime.today()
|
23 |
+
dob = form.date_input("DOB", min_value=min_date, max_value=max_date)
|
24 |
+
city = form.text_input("City")
|
25 |
+
submit = form.form_submit_button("submit")
|
26 |
+
if submit:
|
27 |
+
if not name or not dob or not city:
|
28 |
+
st.error("Please enter your name, DOB and city")
|
29 |
+
else:
|
30 |
+
st.success("registered successfully")
|
31 |
+
user_id = insert_user_detail(city, dob, name)
|
32 |
+
know_user_dir = "./known_user/"
|
33 |
+
image.save_image(picture, know_user_dir, str(user_id))
|
34 |
+
|
35 |
+
|
36 |
+
def insert_user_detail(city, dob, name):
|
37 |
+
user_detail = UserDetail(name, dob, city)
|
38 |
+
db = Database()
|
39 |
+
user_id = db.insert_user_detail(user_detail)
|
40 |
+
return user_id
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==5.2.0
|
2 |
+
attrs==23.2.0
|
3 |
+
blinker==1.7.0
|
4 |
+
cachetools==5.3.3
|
5 |
+
certifi==2024.2.2
|
6 |
+
charset-normalizer==3.3.2
|
7 |
+
click==8.1.7
|
8 |
+
cryptography==42.0.5
|
9 |
+
dlib==19.24.2
|
10 |
+
face-recognition==1.3.0
|
11 |
+
face-recognition-models==0.3.0
|
12 |
+
gitdb==4.0.11
|
13 |
+
GitPython==3.1.42
|
14 |
+
idna==3.6
|
15 |
+
importlib-metadata==7.0.1
|
16 |
+
Jinja2==3.1.3
|
17 |
+
jsonschema==4.21.1
|
18 |
+
jsonschema-specifications==2023.12.1
|
19 |
+
markdown-it-py==3.0.0
|
20 |
+
MarkupSafe==2.1.5
|
21 |
+
mdurl==0.1.2
|
22 |
+
numpy==1.26.4
|
23 |
+
opencv-python==4.6.0.66
|
24 |
+
packaging==23.2
|
25 |
+
pandas==2.2.1
|
26 |
+
protobuf==4.25.3
|
27 |
+
pyarrow==15.0.0
|
28 |
+
pydeck==0.8.1b0
|
29 |
+
Pygments==2.17.2
|
30 |
+
python-dateutil==2.9.0.post0
|
31 |
+
pytz==2024.1
|
32 |
+
referencing==0.33.0
|
33 |
+
requests==2.31.0
|
34 |
+
rich==13.7.1
|
35 |
+
rpds-py==0.18.0
|
36 |
+
six==1.16.0
|
37 |
+
smmap==5.0.1
|
38 |
+
streamlit==1.31.1
|
39 |
+
tenacity==8.2.3
|
40 |
+
toml==0.10.2
|
41 |
+
toolz==0.12.1
|
42 |
+
tornado==6.4
|
43 |
+
typing_extensions==4.10.0
|
44 |
+
tzdata==2024.1
|
45 |
+
tzlocal==5.2
|
46 |
+
urllib3==2.2.1
|
47 |
+
validators==0.22.0
|
48 |
+
watchdog==4.0.0
|
49 |
+
zipp==3.17.0
|
unknown_user/.nomedia
ADDED
File without changes
|