Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from deepface import DeepFace
|
| 4 |
+
import os
|
| 5 |
+
from googleapiclient.discovery import build
|
| 6 |
+
from google.oauth2 import service_account
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
# Google Drive API setup
|
| 10 |
+
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 11 |
+
SERVICE_ACCOUNT_FILE = 'service_account.json'
|
| 12 |
+
|
| 13 |
+
credentials = service_account.Credentials.from_service_account_file(
|
| 14 |
+
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 15 |
+
drive_service = build('drive', 'v3', credentials=credentials)
|
| 16 |
+
REFERENCE_FOLDER_ID = 'your_google_drive_folder_id'
|
| 17 |
+
|
| 18 |
+
# Helper function to download reference files
|
| 19 |
+
def list_reference_files(folder_id):
|
| 20 |
+
query = f"'{folder_id}' in parents and trashed=false"
|
| 21 |
+
results = drive_service.files().list(q=query, fields="files(id, name)").execute()
|
| 22 |
+
return results.get("files", [])
|
| 23 |
+
|
| 24 |
+
def download_file(file_id, file_name):
|
| 25 |
+
request = drive_service.files().get_media(fileId=file_id)
|
| 26 |
+
file_io = io.BytesIO()
|
| 27 |
+
downloader = MediaIoBaseDownload(file_io, request)
|
| 28 |
+
done = False
|
| 29 |
+
while not done:
|
| 30 |
+
_, done = downloader.next_chunk()
|
| 31 |
+
os.makedirs("reference_images", exist_ok=True)
|
| 32 |
+
file_path = os.path.join("reference_images", file_name)
|
| 33 |
+
with open(file_path, "wb") as f:
|
| 34 |
+
f.write(file_io.getvalue())
|
| 35 |
+
return file_path
|
| 36 |
+
|
| 37 |
+
# Streamlit app
|
| 38 |
+
st.title("Face Authentication Demo")
|
| 39 |
+
st.write("Upload a selfie to authenticate.")
|
| 40 |
+
|
| 41 |
+
uploaded_file = st.file_uploader("Choose a selfie", type=["jpg", "jpeg", "png"])
|
| 42 |
+
|
| 43 |
+
if uploaded_file is not None:
|
| 44 |
+
selfie_path = os.path.join("static", "uploads", uploaded_file.name)
|
| 45 |
+
os.makedirs(os.path.dirname(selfie_path), exist_ok=True)
|
| 46 |
+
with open(selfie_path, "wb") as f:
|
| 47 |
+
f.write(uploaded_file.getbuffer())
|
| 48 |
+
|
| 49 |
+
# Download reference images from Google Drive
|
| 50 |
+
st.write("Downloading reference images...")
|
| 51 |
+
reference_files = list_reference_files(REFERENCE_FOLDER_ID)
|
| 52 |
+
reference_paths = [download_file(f['id'], f['name']) for f in reference_files]
|
| 53 |
+
|
| 54 |
+
# Compare selfie with reference images
|
| 55 |
+
st.write("Authenticating...")
|
| 56 |
+
authenticated = False
|
| 57 |
+
for ref_path in reference_paths:
|
| 58 |
+
if DeepFace.verify(img1_path=ref_path, img2_path=selfie_path,
|
| 59 |
+
model_name="ArcFace", enforce_detection=False)['verified']:
|
| 60 |
+
authenticated = True
|
| 61 |
+
break
|
| 62 |
+
|
| 63 |
+
if authenticated:
|
| 64 |
+
st.success("Authentication successful!")
|
| 65 |
+
else:
|
| 66 |
+
st.error("Authentication failed. ID not verified.")
|
| 67 |
+
|