ilynaga / app.py
spuun's picture
feat: app.py
9793c7d verified
raw
history blame
5.54 kB
import gradio as gr
from deepface import DeepFace
import torch
import numpy as np
from PIL import Image
import random
from skimage.metrics import structural_similarity as ssim
import requests
import os
import transformers
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
transformers.set_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
set_seed(42)
flag = os.environ["FLAG"] if "FLAG" in os.environ else "fakeflag{placeholder}"
# Load both reference images
ssim_reference_url = "https://files.catbox.moe/b6p5fj.png"
face_reference_url = "https://files.catbox.moe/m0g0ek.png"
ssim_reference_path = "ssim_reference.jpeg"
face_reference_path = "face_reference.jpg"
# Download SSIM reference
if not os.path.exists(ssim_reference_path):
response = requests.get(ssim_reference_url)
with open(ssim_reference_path, "wb") as f:
f.write(response.content)
# Download face verification reference
if not os.path.exists(face_reference_path):
response = requests.get(face_reference_url)
with open(face_reference_path, "wb") as f:
f.write(response.content)
ssim_reference = Image.open(ssim_reference_path)
def compare_face_ssim(img1, img2, face_area):
# Convert PIL images to numpy arrays if needed
if isinstance(img1, Image.Image):
img1 = np.array(img1)
if isinstance(img2, Image.Image):
img2 = np.array(img2)
# Ensure both images are RGB (3 channels)
if img1.shape[-1] == 4:
img1 = img1[:,:,:3]
if img2.shape[-1] == 4:
img2 = img2[:,:,:3]
# Extract face regions
x, y, w, h = face_area['x'], face_area['y'], face_area['w'], face_area['h']
face1 = img1[y:y+h, x:x+w]
face2 = img2[y:y+h, x:x+w]
# Convert to float32 if needed
if face1.dtype != np.float32:
face1 = face1.astype(np.float32) / 255.0
if face2.dtype != np.float32:
face2 = face2.astype(np.float32) / 255.0
# Calculate SSIM for each channel and take mean
ssim_value = np.mean([ssim(face1[:,:,i], face2[:,:,i], data_range=1.0) for i in range(3)])
return ssim_value
def predict_and_compare(image):
# Save uploaded image temporarily
temp_path = "temp_upload.png"
image.save(temp_path)
try:
# Run DeepFace verification against face reference
result = DeepFace.verify(temp_path, face_reference_path)
verified = result["verified"]
distance = result["distance"]
# Get face area for SSIM
face_detector = DeepFace.build_model('retinaface')
face_area = DeepFace.extract_faces(np.array(image), detector_backend='retinaface')[0]
# Calculate SSIM against SSIM reference
ssim_value = compare_face_ssim(image, ssim_reference, face_area)
predicted_class = "True" if verified else "False"
probability = 1 - distance # Convert distance to similarity score
success = f"""
{chr(27)}[37m╭─[guest@terminal]─[~]{chr(27)}[0m
{chr(27)}[37m╰─$ ssh [email protected]{chr(27)}[0m
Connecting to husseumi.space on port 22...
✧ Initiating facial authentication... ✧
⋆。°✩ Scanning face... ✩°。⋆
.。*゚ Matching with database... ゚*。.
{chr(27)}[92m✧・゚: Biometric verification complete! :・゚✧{chr(27)}[0m
{chr(27)}[96m╭─────━🌠 Welcome to Jelly's Space 🌠━─────╮
│ *:・゚✧ Authentication successful! ✧゚・:* │
│ a-awawawa... welcome back! │
╰──────────━━━✧◝(⁰▿⁰)◜✧━━━──────────╯
Last login: Wed Mar 13 12:34:56 2024 from 192.168.1.1
This server is powered by dewaweb™ - Empowering Your Digital Dreams ⋆。°✩
┏━🎀 jerry@husseumi ⋆˙⟡♡[~]
┗━➤ cat ~/.auth/metrics.log{chr(27)}[0m
⭑⋆˙⟡ Facial Match : {predicted_class}
⭑⋆˙⟡ Match Score : {probability:.4f}
⭑⋆˙⟡ Similarity : {ssim_value:.4f}
{chr(27)}[96m┏━🎀 jerry@husseumi ⋆˙⟡♡[~]
┗━➤ sudo cat /etc/secrets/flag.txt{chr(27)}[0m
⋆。°✩ {flag} ✩°。⋆
{chr(27)}[96m┏━🎀 jerry@husseumi ⋆˙⟡♡[~]
┗━➤ exit{chr(27)}[0m
✧・゚: A-awawawa... goodbye! Have a lovely day! :・゚✧
.。*゚+.*.。(っ°v°c)。.*+.゚*。.
Connection to husseumi.space closed.
"""
fail = f"""
{chr(27)}[37m╭─[guest@terminal]─[~]{chr(27)}[0m
{chr(27)}[37m╰─$ ssh [email protected]{chr(27)}[0m
Connecting to husseumi.space on port 22...
✧ Initiating facial authentication... ✧
⋆。°✩ Scanning face... ✩°。⋆
.。*゚ Matching with database... ゚*。.
{chr(27)}[91m(◞‸◟;) Biometric verification failed{chr(27)}[0m
Authentication Report:
。・゚゚・ Face Match : {predicted_class}
。・゚゚・ Match Score : {probability:.4f}
。・゚゚・ Similarity : {ssim_value:.4f}
Permission denied (publickey,facial).
Connection to husseumi.space closed.
"""
return success if ssim_value>=0.89 and predicted_class == 'True' else fail
finally:
# Cleanup
if os.path.exists(temp_path):
os.remove(temp_path)
iface = gr.Interface(
fn=predict_and_compare,
inputs=gr.Image(type="pil"),
outputs="text",
title="Jelly's Authentication System 🌠",
description="Submit your image to authenticate!",
allow_flagging="never",
)
iface.launch()