trts commited on
Commit
20f7338
·
verified ·
1 Parent(s): fd12502

Upload 14 files

Browse files
Files changed (14) hide show
  1. UserDetail.py +5 -0
  2. add.py +35 -0
  3. app.py +24 -0
  4. clear.py +33 -0
  5. db.py +58 -0
  6. encdec.py +15 -0
  7. home.py +59 -0
  8. image.py +70 -0
  9. known_user/.nomedia +0 -0
  10. organized_user/.nomedia +0 -0
  11. register.py +45 -0
  12. requirements.txt +49 -0
  13. show.py +18 -0
  14. 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
add.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import streamlit as st
4
+
5
+ import image
6
+ from db import Database
7
+
8
+
9
+ def add():
10
+ options = ["Camera", "Upload"]
11
+ initial_selection = options.index("Upload")
12
+ method = st.radio("Method", options, index=initial_selection, key = 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"], key = "uploadPic")
17
+ button = st.button("Add")
18
+
19
+ if button and picture:
20
+ unknown_user_dir = "./unknown_user/"
21
+ unknown_user_name = "unknown_user"
22
+ image.save_image(picture, unknown_user_dir, unknown_user_name)
23
+
24
+ is_match, user_id = image.compare_faces_in_directory("./known_user/", unknown_user_dir)
25
+
26
+ if is_match:
27
+ db = Database()
28
+ st.write(user_id)
29
+ user_detail = db.get_user_detail(user_id)
30
+ image.save_image(picture, "./organized_user/" + user_detail.name + "/", user_detail.name + str(time.time_ns()))
31
+ st.write(user_detail)
32
+ else:
33
+ st.error("No Match Found")
34
+
35
+ image.delete_image(unknown_user_dir + unknown_user_name)#a
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import clear
3
+ import add
4
+ import register
5
+ import show
6
+ import home
7
+ import os
8
+
9
+
10
+
11
+ home_tab, register_tab, add_tab, show_tab, clear_tab = st.tabs(["Home", "Register", "Add", "Show","Clear"])
12
+
13
+
14
+
15
+ with home_tab:
16
+ home.home()
17
+ with register_tab:
18
+ register.register()
19
+ with add_tab:
20
+ add.add()
21
+ with show_tab:
22
+ show.show()
23
+ with clear_tab:
24
+ clear.clear()
clear.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ organized = os.path.join(os.path.dirname(os.path.abspath(__file__)), "organized_user")
13
+ db = os.path.join(os.path.dirname(os.path.abspath(__file__)), "db.sqlite")
14
+ if clearbtn and passowrd == encdec.encdec():
15
+ try:
16
+ if os.path.exists(known):
17
+ shutil.rmtree(known)
18
+ os.makedirs(known)
19
+ if os.path.exists(unknown):
20
+ shutil.rmtree(unknown)
21
+ os.makedirs(unknown)
22
+ if os.path.exists(organized):
23
+ shutil.rmtree(organized)
24
+ os.makedirs(organized)
25
+ if os.path.exists(db):
26
+ os.remove(db)
27
+ st.success("Cleared")
28
+ except Exception as e:
29
+ st.error(e)
30
+ if clearbtn and passowrd != encdec.encdec():
31
+ st.error("Password entered is incorrect")
32
+
33
+
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,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
15
+
home.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ def home():
4
+ st.markdown("""
5
+
6
+ # Image Organizer 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 organize images using face recognition.
13
+
14
+ ## Features
15
+
16
+ - Face detection, recognition and image organizer
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/image-organizer-web-app.git
25
+ cd image-organizer-web-app
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 add page to add images to be organized under registered face
47
+ - You can use show page to see organized images
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
+ """)
image.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import face_recognition as fr
4
+
5
+
6
+ def save_image(picture, directory, filename):
7
+ if not os.path.exists(directory):
8
+ os.makedirs(directory)
9
+
10
+ if picture:
11
+ with open(directory + filename + ".jpg", "wb") as f:
12
+ f.write(picture.getvalue())
13
+
14
+
15
+ def delete_image(image_path):
16
+ if os.path.exists(image_path):
17
+ os.remove(image_path)
18
+
19
+
20
+ def get_all_images(directory):
21
+ return [file for file in os.listdir(directory) if file.endswith('.jpg') or file.endswith('.png')]
22
+
23
+
24
+ def get_sub_directory_paths(start_directory):
25
+ sub_directories = [os.path.join(start_directory, f) for f in os.listdir(start_directory) if os.path.isdir(os.path.join(start_directory, f))]
26
+ return sub_directories
27
+
28
+
29
+ def compare_faces_in_directory(known_image_dir, unknown_image_dir):
30
+ # Get list of image files in the directory
31
+ known_image_files = get_all_images(known_image_dir)
32
+ unknown_image_files = get_all_images(unknown_image_dir)
33
+
34
+ if not unknown_image_files:
35
+ return False, -1
36
+
37
+ # Iterate over image files
38
+ for j, known_image_file in enumerate(known_image_files):
39
+ # Load images
40
+ unknown_img_path = os.path.join(unknown_image_dir, unknown_image_files[0])
41
+ known_img_path = os.path.join(known_image_dir, known_image_file)
42
+ image1 = fr.load_image_file(unknown_img_path)
43
+ image2 = fr.load_image_file(known_img_path)
44
+
45
+ # Find face encodings
46
+ face_encodings1 = fr.face_encodings(image1)
47
+ face_encodings2 = fr.face_encodings(image2)
48
+
49
+ if len(face_encodings1) > 0 and len(face_encodings2) > 0:
50
+ # Compare the first face encoding from each image
51
+ face_encoding1 = face_encodings1[0]
52
+ face_encoding2 = face_encodings2[0]
53
+
54
+ # Calculate similarity
55
+ similarity = fr.face_distance([face_encoding1], face_encoding2)
56
+
57
+ # Set a threshold for similarity
58
+ threshold = 0.4
59
+
60
+ # Output comparison result
61
+ user_id = os.path.splitext(known_image_file)[0]
62
+ if similarity[0] < threshold:
63
+ print(f"Images {unknown_image_files[0]} and {known_image_file} have similar faces.")
64
+ return True, user_id
65
+ else:
66
+ print(f"Images {unknown_image_files[0]} and {known_image_file} do not have similar faces.")
67
+ else:
68
+ print(f"No faces found in one or both images.")
69
+
70
+ return False, -1
known_user/.nomedia ADDED
File without changes
organized_user/.nomedia ADDED
File without changes
register.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
7
+ import image
8
+
9
+
10
+ def register():
11
+ options = ["Camera", "Upload"]
12
+ initial_selection = options.index("Upload")
13
+ method = st.radio("Method", options, index=initial_selection)
14
+ if method == "Camera":
15
+ picture = st.camera_input("picture", key="regpic", label_visibility='hidden')
16
+ if method == "Upload":
17
+ picture = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])
18
+
19
+ if picture:
20
+ form = st.form("Register")
21
+ name = form.text_input("Username")
22
+ min_date = datetime.datetime(1900, 1, 1)
23
+ max_date = datetime.datetime.today()
24
+ dob = form.date_input("DOB", min_value=min_date, max_value=max_date)
25
+ city = form.text_input("City")
26
+ submit = form.form_submit_button("submit")
27
+ if submit:
28
+ if not name or not dob or not city:
29
+ st.error("Please enter your name, DOB and city")
30
+ else:
31
+ st.success("registered successfully")
32
+ user_id = insert_user_detail(city, dob, name)
33
+ know_user_dir = "./known_user/"
34
+ image.save_image(picture, know_user_dir, str(user_id))
35
+
36
+
37
+ def insert_user_detail(city, dob, name):
38
+ user_detail = UserDetail(name, dob, city)
39
+ db = Database()
40
+ user_id = db.insert_user_detail(user_detail)
41
+ return user_id
42
+
43
+
44
+
45
+
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
show.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ import image
4
+ import os
5
+
6
+
7
+ def show():
8
+ organized_user_dir = "./organized_user/"
9
+ image_dir_paths = image.get_sub_directory_paths(organized_user_dir)
10
+
11
+ for image_dir_path in image_dir_paths:
12
+ user_name = os.path.basename(image_dir_path)
13
+ expander = st.expander(user_name)
14
+ images = image.get_all_images(image_dir_path)
15
+
16
+ for img in images:
17
+ image_path = os.path.join(image_dir_path, img)
18
+ expander.image(image_path)
unknown_user/.nomedia ADDED
File without changes