0% found this document useful (0 votes)
16 views7 pages

Face Recognition with Firebase Integration

Uploaded by

rajeshkollu81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Face Recognition with Firebase Integration

Uploaded by

rajeshkollu81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# import cv2 as cv;

# import pickle

# import face_recognition

# # img=[Link]("driver_1.jpeg")

# # [Link]('driver',img)

# # [Link](0)

# # reading video

# # video=[Link](1)

# # [Link](3,1280)

# # [Link](4,720)

# # while True:

# # success,img=[Link]()

# # [Link]("Face Recognised",img)

# # [Link](0)

# video = [Link](0) # Try different index values if needed (0, 1, 2, ...)

# [Link](3, 640) # Set width

# [Link](4, 480) # Set height

# img_to_recognize = [Link]("[Link]")

# backgroungImg=[Link]("Template_img.jpeg")

# print("load the encodings")

# file=open('Encode.p','rb')
# encodelistknownwithId=[Link](file)

# [Link]()

# encodelistknown,driverId=encodelistknownwithId

# print("loading completed")

# # while True:

## success, img = [Link]()

## if not success:

## print("Failed to read frame")

## break

# imgS=[Link](img_to_recognize,(0,0),None,0.25,0.25)

# imgS=[Link](img_to_recognize,cv.COLOR_BGR2RGB)

# faceCurFrame=face_recognition.face_locations(imgS)

# encodeCurFrame=face_recognition.face_encodings(imgS,faceCurFrame)

# # backgroungImg[135:135+480,16:16+640]=img

# for encodeFace,faceLoc in zip(encodeCurFrame,faceCurFrame):

# matches=face_recognition.compare_faces(encodelistknown,encodeFace)

# facedistance=face_recognition.face_distance(encodelistknown,encodeFace)

# print("facedistance",facedistance)

# print("matches",matches)

# # [Link]("WebCam", img)

# # [Link]("Face recognition",backgroungImg)
# # Check for key press to exit

# # key = [Link](1)

# # if key == 27: # Press 'ESC' key to exit

# # break

# [Link]()

# [Link]()

import cv2 as cv

import face_recognition

import pickle

import firebase_admin

from firebase_admin import credentials

from firebase_admin import db

from firebase_admin import storage

import base64

import numpy as np

from io import BytesIO

from PIL import Image

import os

# Initialize Firebase

cred = [Link]("[Link]")

firebase_admin.initialize_app(cred, {

'databaseURL': '[Link]

'storageBucket': '[Link]'

})
# Load the image to recognize

img_to_recognize = [Link]("[Link]")

# backgroungImg = [Link]("Template_img.jpeg")

# Check if the images are loaded correctly

if img_to_recognize is None:

print("Error: Image to recognize not loaded.")

exit()

# if backgroungImg is None:

# print("Error: Background image not loaded.")

# exit()

# Display the image for verification

# [Link]("Input Image", img_to_recognize)

# [Link](0)

# [Link]()

# Load known face encodings

print("Loading the encodings")

with open('Encode.p', 'rb') as file:

encodelistknownwithId = [Link](file)

encodelistknown, driverId = encodelistknownwithId

print("Loading completed")

# Resize and convert the image for face recognition

print("Resizing and converting the image")

imgS = [Link](img_to_recognize, (0, 0), fx=0.25, fy=0.25)

imgS = [Link](imgS, cv.COLOR_BGR2RGB)


# Display the resized image for verification

# [Link]("Resized Image", [Link](imgS, cv.COLOR_RGB2BGR))

# [Link](0)

# [Link]()

# Detect faces and encode them

print("Detecting faces")

faceCurFrame = face_recognition.face_locations(imgS)

encodeCurFrame = face_recognition.face_encodings(imgS, faceCurFrame)

counter=0

# Print the face locations to debug

print("Face locations:", faceCurFrame)

# Check if any faces were detected

print(f"Number of faces detected: {len(faceCurFrame)}")

if len(faceCurFrame) == 0:

print("No faces detected in the image.")

else:

for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):

matches = face_recognition.compare_faces(encodelistknown, encodeFace)

face_distance = face_recognition.face_distance(encodelistknown, encodeFace)

print("Face distance:", face_distance)

print("Matches:", matches)

# Find the best match

best_match_index = face_distance.argmin()

# Verify if the best match is a true match


if matches[best_match_index] and face_distance[best_match_index] < 0.6: # using a threshold
of 0.6 for a valid match

driver_id = driverId[best_match_index]

print(f"Match found: {driver_id}")

# Scale the face location back to the original image size

top, right, bottom, left = faceLoc

top, right, bottom, left = top * 4, right * 4, bottom * 4, left * 4

# Draw a rectangle around the face

[Link](img_to_recognize, (left, top), (right, bottom), (0, 255, 0), 2)

# Display the driver's ID

[Link](img_to_recognize, driver_id, (left, top - 10), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,


255, 0), 2)

if counter==0:

counter=counter+1

else:

print("No valid match found.")

if counter!=0:

if counter==1:

driverinfo=[Link](f"Photo/{driver_id}").get()

print(driverinfo)

counter+=1

# Show the result

# [Link]("Recognized Image", img_to_recognize)

# [Link](0)

[Link]()

_, buffer = [Link]('.jpg', img_to_recognize)

img_base64 = base64.b64encode(buffer).decode('utf-8')
# data_provider.py

def get_output():

output = driverinfo

output["image"] = img_base64

return output

You might also like