FirstFace
Browse files- .gitignore +2 -0
- .idea/.gitignore +3 -0
- .idea/facial_recog.iml +10 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- Dockerfile +26 -0
- FastAPI.py +74 -0
- Procfile +1 -0
- encode_pkl.py +65 -0
- predict.py +45 -0
- requirements.txt +0 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
socioverse-2025-firebase-adminsdk-gcc6m-6bfb53e6d9.json
|
2 |
+
venv
|
.idea/.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
.idea/facial_recog.iml
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$">
|
5 |
+
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
6 |
+
</content>
|
7 |
+
<orderEntry type="inheritedJdk" />
|
8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
9 |
+
</component>
|
10 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (facial_recog)" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/facial_recog.iml" filepath="$PROJECT_DIR$/.idea/facial_recog.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
Dockerfile
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11.4
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN apt-get update && \
|
8 |
+
apt-get install -y python3 python3-pip libgl1-mesa-glx && \
|
9 |
+
rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
RUN pip install --upgrade pip
|
12 |
+
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
14 |
+
|
15 |
+
RUN useradd -m -u 1000 user
|
16 |
+
|
17 |
+
USER user
|
18 |
+
|
19 |
+
ENV HOME=/home/user \
|
20 |
+
PATH=/home/user/.local/bin:$PATH
|
21 |
+
|
22 |
+
WORKDIR $HOME/app
|
23 |
+
|
24 |
+
COPY --chown=user . $HOME/app
|
25 |
+
|
26 |
+
CMD ["uvicorn", "FastAPI:app", "--host", "0.0.0.0", "--port", "7860"]
|
FastAPI.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel, HttpUrl
|
3 |
+
import requests
|
4 |
+
import face_recognition
|
5 |
+
import pickle
|
6 |
+
import cv2
|
7 |
+
import pyrebase
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
# Initialize Firebase using Pyrebase
|
12 |
+
config = {
|
13 |
+
"apiKey": "AIzaSyClnRJAnrJgAgkYjuYnlvu-CJ6Cxyklebo",
|
14 |
+
"databaseURL": "https://console.firebase.google.com/project/socioverse-2025/database/socioverse-2025-default-rtdb/data/~2F",
|
15 |
+
"authDomain": "socioverse-2025.firebaseapp.com",
|
16 |
+
"projectId": "socioverse-2025",
|
17 |
+
"storageBucket": "socioverse-2025.appspot.com",
|
18 |
+
"messagingSenderId": "689574504641",
|
19 |
+
"appId": "1:689574504641:web:a22f6a2fa343e4221acc40",
|
20 |
+
"serviceAccount":"socioverse-2025-firebase-adminsdk-gcc6m-6bfb53e6d9.json"
|
21 |
+
}
|
22 |
+
|
23 |
+
firebase = pyrebase.initialize_app(config)
|
24 |
+
storage = firebase.storage()
|
25 |
+
|
26 |
+
|
27 |
+
storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")
|
28 |
+
|
29 |
+
|
30 |
+
app = FastAPI()
|
31 |
+
|
32 |
+
class ImgInput(BaseModel):
|
33 |
+
image_url: HttpUrl
|
34 |
+
|
35 |
+
class ImgOutput(BaseModel):
|
36 |
+
label: str
|
37 |
+
|
38 |
+
def recognize_face(image_url: HttpUrl) -> ImgOutput:
|
39 |
+
# Downloading image
|
40 |
+
response = requests.get(image_url)
|
41 |
+
with open("examp.jpg", 'wb') as file:
|
42 |
+
file.write(response.content)
|
43 |
+
|
44 |
+
# Load the stored face encodings and labels from the pickle file
|
45 |
+
with open("face_encodings.pkl", "rb") as file:
|
46 |
+
data = pickle.load(file)
|
47 |
+
face_encodings = data["encodings"]
|
48 |
+
labels = data["labels"]
|
49 |
+
|
50 |
+
# Load a new image you want to recognize
|
51 |
+
new_image = cv2.imread("examp.jpg")
|
52 |
+
|
53 |
+
new_face_encoding = face_recognition.face_encodings(new_image)
|
54 |
+
|
55 |
+
if len(new_face_encoding) == 0:
|
56 |
+
print("No faces found in the new image.")
|
57 |
+
else:
|
58 |
+
# Compare the new face encoding to the stored encodings
|
59 |
+
results = face_recognition.compare_faces(face_encodings, new_face_encoding[0])
|
60 |
+
|
61 |
+
os.remove("examp.jpg")
|
62 |
+
|
63 |
+
for i, result in enumerate(results):
|
64 |
+
if result:
|
65 |
+
return ImgOutput(label=labels[i])
|
66 |
+
|
67 |
+
return ImgOutput(label="unable to detect")
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
@app.post('/')
|
72 |
+
async def scoring_endpoint(item:ImgInput):
|
73 |
+
result = recognize_face(item.image_url)
|
74 |
+
return result
|
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: gunicorn -w 2 -k uvicorn
|
encode_pkl.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import face_recognition
|
4 |
+
import pickle
|
5 |
+
import pyrebase
|
6 |
+
|
7 |
+
# Initialize Firebase using Pyrebase
|
8 |
+
config = {
|
9 |
+
"apiKey": "AIzaSyClnRJAnrJgAgkYjuYnlvu-CJ6Cxyklebo",
|
10 |
+
"databaseURL": "https://console.firebase.google.com/project/socioverse-2025/database/socioverse-2025-default-rtdb/data/~2F",
|
11 |
+
"authDomain": "socioverse-2025.firebaseapp.com",
|
12 |
+
"projectId": "socioverse-2025",
|
13 |
+
"storageBucket": "socioverse-2025.appspot.com",
|
14 |
+
"messagingSenderId": "689574504641",
|
15 |
+
"appId": "1:689574504641:web:a22f6a2fa343e4221acc40",
|
16 |
+
"serviceAccount":"socioverse-2025-firebase-adminsdk-gcc6m-6bfb53e6d9.json"
|
17 |
+
}
|
18 |
+
|
19 |
+
firebase = pyrebase.initialize_app(config)
|
20 |
+
storage = firebase.storage()
|
21 |
+
|
22 |
+
# Define the folder containing face images in the Firebase Storage bucket
|
23 |
+
storage_folder = "Faces/"
|
24 |
+
|
25 |
+
# Create an array to store encodings and corresponding labels
|
26 |
+
face_encodings = []
|
27 |
+
labels = []
|
28 |
+
|
29 |
+
# List all files in the Firebase Storage folder
|
30 |
+
blobs = storage.child(storage_folder).list_files()
|
31 |
+
|
32 |
+
|
33 |
+
for blob in blobs:
|
34 |
+
if blob.name.startswith(storage_folder) and (blob.name.endswith(".jpeg") or blob.name.endswith(".jpg") or blob.name.endswith(".png")):
|
35 |
+
# Download the image to a local file
|
36 |
+
url = storage.child(f"{blob.name}").get_url(None)
|
37 |
+
|
38 |
+
storage.child(url).download(f"{blob.name}","temp.jpeg")
|
39 |
+
# Load the image using OpenCV
|
40 |
+
img = cv2.imread("temp.jpeg")
|
41 |
+
|
42 |
+
# Convert the BGR image to RGB
|
43 |
+
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
44 |
+
|
45 |
+
encode = face_recognition.face_encodings(img)[0]
|
46 |
+
face_encodings.append(encode)
|
47 |
+
name_parts = blob.name.split('/')
|
48 |
+
name = name_parts[-1]
|
49 |
+
name_parts = name.split('$')
|
50 |
+
name = name_parts[0]
|
51 |
+
labels.append(name)
|
52 |
+
|
53 |
+
# Delete the temporary downloaded image
|
54 |
+
os.remove("temp.jpeg")
|
55 |
+
|
56 |
+
# Save the encodings and labels to a pickle file
|
57 |
+
data = {"encodings": face_encodings, "labels": labels}
|
58 |
+
with open("face_encodings.pkl", "wb") as file:
|
59 |
+
pickle.dump(data, file)
|
60 |
+
|
61 |
+
# Upload the pickle file to Firebase Storage
|
62 |
+
pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
|
63 |
+
pkl_blob.put("face_encodings.pkl")
|
64 |
+
|
65 |
+
print("Face encodings and labels saved to Firebase Storage in 'faces/pkl' folder as 'face_encodings.pkl'.")
|
predict.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import face_recognition
|
2 |
+
import pickle
|
3 |
+
import cv2
|
4 |
+
import pyrebase
|
5 |
+
|
6 |
+
|
7 |
+
# Initialize Firebase using Pyrebase
|
8 |
+
config = {
|
9 |
+
"apiKey": "AIzaSyClnRJAnrJgAgkYjuYnlvu-CJ6Cxyklebo",
|
10 |
+
"databaseURL": "https://console.firebase.google.com/project/socioverse-2025/database/socioverse-2025-default-rtdb/data/~2F",
|
11 |
+
"authDomain": "socioverse-2025.firebaseapp.com",
|
12 |
+
"projectId": "socioverse-2025",
|
13 |
+
"storageBucket": "socioverse-2025.appspot.com",
|
14 |
+
"messagingSenderId": "689574504641",
|
15 |
+
"appId": "1:689574504641:web:a22f6a2fa343e4221acc40",
|
16 |
+
"serviceAccount":"socioverse-2025-firebase-adminsdk-gcc6m-6bfb53e6d9.json"
|
17 |
+
}
|
18 |
+
|
19 |
+
firebase = pyrebase.initialize_app(config)
|
20 |
+
storage = firebase.storage()
|
21 |
+
|
22 |
+
|
23 |
+
storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")
|
24 |
+
|
25 |
+
# Load the stored face encodings and labels from the pickle file
|
26 |
+
with open("face_encodings.pkl", "rb") as file:
|
27 |
+
data = pickle.load(file)
|
28 |
+
face_encodings = data["encodings"]
|
29 |
+
labels = data["labels"]
|
30 |
+
|
31 |
+
# Load a new image you want to recognize
|
32 |
+
new_image = cv2.imread("download.jpg")
|
33 |
+
|
34 |
+
|
35 |
+
new_face_encoding = face_recognition.face_encodings(new_image)
|
36 |
+
|
37 |
+
if len(new_face_encoding) == 0:
|
38 |
+
print("No faces found in the new image.")
|
39 |
+
else:
|
40 |
+
# Compare the new face encoding to the stored encodings
|
41 |
+
results = face_recognition.compare_faces(face_encodings, new_face_encoding[0])
|
42 |
+
|
43 |
+
for i, result in enumerate(results):
|
44 |
+
if result:
|
45 |
+
print(f"Recognised : {labels[i]}")
|
requirements.txt
ADDED
Binary file (590 Bytes). View file
|
|