File size: 7,623 Bytes
9793c7d
 
 
 
 
 
 
8992780
9793c7d
 
 
 
 
 
 
 
 
 
8992780
9793c7d
 
 
 
 
 
 
 
 
c7dac6a
 
9793c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbc055b
90b7b6f
9793c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
5862051
9793c7d
 
 
 
 
9ee4db0
9793c7d
 
9ee4db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9793c7d
 
 
9ee4db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9793c7d
dbc055b
9793c7d
 
 
 
 
 
9ee4db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9793c7d
 
9ee4db0
 
9793c7d
 
 
9ee4db0
 
9793c7d
 
9ee4db0
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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 tensorflow as tf
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)
    tf.random.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}"

ssim_reference_url = "https://files.catbox.moe/b6p5fj.png"
face_reference_url = "https://files.catbox.moe/m0g0ek.png"
ssim_reference_path = "ssim_reference.png"
face_reference_path = "face_reference.png"

# 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 multichannel SSIM
    ssim_value = ssim(face1, face2, data_range=1.0, channel_axis=2, win_size=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_area = DeepFace.extract_faces(temp_path)[0]["facial_area"]
        
        # Calculate SSIM against SSIM reference
        ssim_value = compare_face_ssim(image, ssim_reference, face_area)
        
        predicted_class = "True" if verified else "False"
        probability = 1 - abs(distance)

        success = f"""
<div style="font-family: 'Courier New', monospace; background: #1a1b26; color: #a9b1d6; padding: 20px; border-radius: 8px;">
    <span style="color: #7aa2f7">guest@terminal</span>:<span style="color: #9ece6a">~</span>$ ssh [email protected]<br>
    Connecting to husseumi.space on port 22...<br><br>
    
    <span style="color: #bb9af7">✧ Initiating facial authentication... ✧</span><br>
    <span style="color: #7dcfff">⋆。°✩ Scanning face... ✩°。⋆</span><br>
    <span style="color: #7dcfff">.。*゚ Matching with database... ゚*。.</span><br>
    <span style="color: #9ece6a">✧・゚: Biometric verification complete! :・゚✧</span><br><br>
    
    <div style="background: #1f2335; padding: 10px; border-radius: 4px; margin: 10px 0;">
    <pre style="color: #7aa2f7; margin: 0; font-family: monospace;">
╭──── 🌠 Welcome to Jelly's Space 🌠 ─────╮
│   *:・゚✧ Authentication successful! ✧゚・:*  |
│         a-awawawa... welcome back!        │
╰─────────────- ✧◝(⁰▿⁰)◜✧ -────────────╯</pre>
    </div>

    Last login: Wed Mar 13 12:34:56 2024 from 192.168.1.1<br>
    <span style="color: #bb9af7">This server is powered by dewaweb™ - Empowering Your Digital Dreams ⋆。°✩</span><br><br>
    
    <span style="color: #7aa2f7">jerry@husseumi</span>:<span style="color: #9ece6a">~</span>$ cat ~/.auth/metrics.log<br>
    <span style="color: #7dcfff">⭑⋆˙⟡ Facial Match   : {predicted_class}</span><br>
    <span style="color: #7dcfff">⭑⋆˙⟡ Match Score    : {probability:.4f}</span><br>
    <span style="color: #7dcfff">⭑⋆˙⟡ Similarity     : {ssim_value:.4f}</span><br><br>
    
    <span style="color: #7aa2f7">jerry@husseumi</span>:<span style="color: #9ece6a">~</span>$ sudo cat /etc/secrets/flag.txt<br>
    <span style="color: #f7768e">⋆。°✩ {flag} ✩°。⋆</span><br><br>
    
    <span style="color: #7aa2f7">jerry@husseumi</span>:<span style="color: #9ece6a">~</span>$ exit<br>
    <span style="color: #bb9af7">✧・゚: A-awawawa... goodbye! Have a lovely day! :・゚✧</span><br>
    <span style="color: #bb9af7">.。*゚+.*.。(っ°v°c)。.*+.゚*。.</span><br><br>
    
    Connection to husseumi.space closed.
</div>
"""

        fail = f"""
<div style="font-family: 'Courier New', monospace; background: #1a1b26; color: #a9b1d6; padding: 20px; border-radius: 8px;">
    <span style="color: #7aa2f7">guest@terminal</span>:<span style="color: #9ece6a">~</span>$ ssh [email protected]<br>
    Connecting to husseumi.space on port 22...<br><br>
    
    <span style="color: #bb9af7">✧ Initiating facial authentication... ✧</span><br>
    <span style="color: #7dcfff">⋆。°✩ Scanning face... ✩°。⋆</span><br>
    <span style="color: #7dcfff">.。*゚ Matching with database... ゚*。.</span><br>
    <span style="color: #f7768e">(◞‸◟;) Biometric verification failed</span><br><br>
    
    Authentication Report:<br>
    <span style="color: #7dcfff">。・゚゚・ Face Match    : {predicted_class}</span><br>
    <span style="color: #7dcfff">。・゚゚・ Match Score   : {probability:.4f}</span><br>
    <span style="color: #7dcfff">。・゚゚・ Similarity    : {ssim_value:.4f}</span><br><br>
    
    <span style="color: #f7768e">Permission denied (publickey,facial).</span><br>
    Connection to husseumi.space closed.
</div>
"""
        return success if ssim_value>=0.96 and predicted_class == 'True' else fail
        
    finally:
        # Cleanup
        if os.path.exists(temp_path):
            os.remove(temp_path)

custom_css = """
#output-html {
    font-family: 'Courier New', monospace;
    background: #1a1b26;
    color: #a9b1d6;
    padding: 20px;
    border-radius: 8px;
    min-height: 200px;
    margin-top: 10px;
}
"""

default_output = """
<div style="font-family: 'Courier New', monospace; background: #1a1b26; color: #a9b1d6; padding: 20px; border-radius: 8px;">
    <span style="color: #7aa2f7">guest@terminal</span>:<span style="color: #9ece6a">~</span>$ ssh [email protected]<br>
    <span style="color: #7dcfff">Awaiting facial authentication...</span><br>
    <span style="color: #bb9af7">Please submit an image to proceed ˚ ༘ ೀ⋆。˚</span>
</div>
"""

iface = gr.Interface(
    fn=predict_and_compare,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=gr.HTML(value=default_output),
    title="Jelly's Authentication System 🌠",
    description="Submit your image to authenticate!",
    allow_flagging="never",
    css=custom_css,
    theme="allenai/gradio-theme"
)

iface.queue()
iface.launch(show_api=False)