File size: 4,081 Bytes
c5380c5 d1e5541 c5380c5 d1e5541 c5380c5 56a798e c5380c5 56a798e c5380c5 9b9e94c c5380c5 56a798e c5380c5 56a798e c5380c5 56a798e c5380c5 56a798e c5380c5 56a798e c5380c5 56a798e d1e5541 c5380c5 d1e5541 56a798e |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
import requests
import face_recognition
import pickle
import cv2
import pyrebase
import os
# Initialize Firebase using Pyrebase
config = {
"apiKey": "AIzaSyClnRJAnrJgAgkYjuYnlvu-CJ6Cxyklebo",
"databaseURL": "https://console.firebase.google.com/project/socioverse-2025/database/socioverse-2025-default-rtdb/data/~2F",
"authDomain": "socioverse-2025.firebaseapp.com",
"projectId": "socioverse-2025",
"storageBucket": "socioverse-2025.appspot.com",
"messagingSenderId": "689574504641",
"appId": "1:689574504641:web:a22f6a2fa343e4221acc40",
"serviceAccount":"socioverse-2025-firebase-adminsdk-gcc6m-6bfb53e6d9.json"
}
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
# Define the folder containing face images in the Firebase Storage bucket
storage_folder = "Faces/"
app = FastAPI()
class ImgSave(BaseModel):
image_url: HttpUrl
user_name: str
class ImgInput(BaseModel):
image_url: HttpUrl
class ImgOutput(BaseModel):
label: list
class UserSaved(BaseModel):
label: str
def recognize_face(image_url: HttpUrl) -> ImgOutput:
storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")
# Downloading image
response = requests.get(image_url)
with open("examp.jpg", 'wb') as file:
file.write(response.content)
# Load the stored face encodings and labels from the pickle file
with open("face_encodings.pkl", "rb") as file:
data = pickle.load(file)
face_encodings = data["encodings"]
labels = data["labels"]
# Load the new image you want to recognize
new_image = cv2.imread("examp.jpg")
# Find face encodings in the new image
new_face_encodings = face_recognition.face_encodings(new_image)
if len(new_face_encodings) == 0:
print("No faces found in the new image.")
return ImgOutput(label=["unable to detect"])
else:
output_labels = []
for new_face_encoding in new_face_encodings:
# Compare the new face encoding to the stored encodings
results = face_recognition.compare_faces(face_encodings, new_face_encoding)
for i, result in enumerate(results):
if result:
output_labels.append(labels[i])
os.remove("examp.jpg")
if output_labels:
return ImgOutput(label=output_labels)
else:
out = ["unable to detect"]
return ImgOutput(label=out)
def add_face(image_url: HttpUrl,user_name : str):
# Downloading image
response = requests.get(image_url)
with open("examp.jpg", 'wb') as file:
file.write(response.content)
# Load the stored face encodings and labels from the pickle file
with open("face_encodings.pkl", "rb") as file:
data = pickle.load(file)
face_encodings = data["encodings"]
labels = data["labels"]
# Load a new image you want to recognize
new_image = cv2.imread("examp.jpg")
# Convert the BGR image to RGB
rgb_img = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(new_image)[0]
face_encodings.append(encode)
labels.append(user_name)
# Delete the temporary downloaded image
os.remove("examp.jpg")
# Save the encodings and labels to a pickle file
data = {"encodings": face_encodings, "labels": labels}
with open("face_encodings.pkl", "wb") as file:
pickle.dump(data, file)
# Upload the pickle file to Firebase Storage
pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
pkl_blob.put("face_encodings.pkl")
@app.post('/')
async def scoring_endpoint(item:ImgInput):
result = recognize_face(item.image_url)
return result
@app.post('/user/')
async def scoring_endpoint(item:ImgSave):
add_face(item.image_url, item.user_name)
results = ["User Saved"]
return ImgOutput(label=results) |