File size: 13,012 Bytes
e5a1544
 
 
 
 
 
107dab2
 
 
 
 
 
 
 
e5a1544
5f27df7
107dab2
5f27df7
4a53aae
 
2553966
b37a8e6
107dab2
e5a1544
 
 
 
 
107dab2
b37a8e6
 
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b37a8e6
e5a1544
107dab2
b37a8e6
4a53aae
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b37a8e6
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f27df7
107dab2
 
 
 
 
 
 
 
 
 
 
 
2553966
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b37a8e6
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2553966
 
107dab2
2553966
4a53aae
107dab2
 
 
4a53aae
107dab2
 
 
b37a8e6
107dab2
 
 
4a53aae
107dab2
 
 
b37a8e6
107dab2
 
 
e5a1544
 
107dab2
e5a1544
5148899
 
 
 
 
565e309
 
dfc63b4
5148899
 
d33634b
 
 
5148899
 
565e309
 
5148899
dfc63b4
 
d33634b
5148899
dfc63b4
d33634b
 
dfc63b4
5148899
 
e5a1544
 
107dab2
e5a1544
107dab2
 
 
 
 
 
 
f6a647b
e5a1544
 
5148899
107dab2
 
 
 
 
 
f6a647b
5148899
 
107dab2
 
 
 
 
 
 
4a53aae
5148899
 
107dab2
 
 
 
 
 
 
4a53aae
5148899
 
107dab2
 
 
 
 
 
 
f6a647b
 
 
5148899
4a53aae
5148899
 
107dab2
 
 
 
 
 
 
 
 
 
 
 
 
 
5148899
 
 
107dab2
5148899
 
 
107dab2
 
565e309
5148899
e5a1544
5148899
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import gradio as gr
import cv2
import numpy as np
import torch
from PIL import Image
import mediapipe as mp

from transformers import (
    AutoFeatureExtractor,
    AutoModel,
    AutoImageProcessor,
    AutoModelForImageClassification,
    AutoModelForSemanticSegmentation
)

# -----------------------------
# Configuration & Device Setup
# -----------------------------
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DESIRED_SIZE = (640, 480)

# -----------------------------
# Initialize Mediapipe Face Detection
# -----------------------------
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)

# -----------------------------
# Load New Models from Hugging Face
# -----------------------------

# 1. Facial Recognition & Identification (facebook/dino-vitb16)
facial_recognition_extractor = AutoFeatureExtractor.from_pretrained("facebook/dino-vitb16")
facial_recognition_model = AutoModel.from_pretrained("facebook/dino-vitb16")
facial_recognition_model.to(device)
facial_recognition_model.eval()

# Create a dummy database for demonstration (embeddings of dimension 768 assumed)
dummy_database = {
    "Alice": torch.randn(768).to(device),
    "Bob": torch.randn(768).to(device)
}

# 2. Emotion Detection (nateraw/facial-expression-recognition)
emotion_processor = AutoImageProcessor.from_pretrained("nateraw/facial-expression-recognition")
emotion_model = AutoModelForImageClassification.from_pretrained("nateraw/facial-expression-recognition")
emotion_model.to(device)
emotion_model.eval()

# 3. Age & Gender Prediction (oayu/age-gender-estimation)
age_gender_processor = AutoImageProcessor.from_pretrained("oayu/age-gender-estimation")
age_gender_model = AutoModelForImageClassification.from_pretrained("oayu/age-gender-estimation")
age_gender_model.to(device)
age_gender_model.eval()

# 4. Face Parsing (hila-chefer/face-parsing)
face_parsing_processor = AutoImageProcessor.from_pretrained("hila-chefer/face-parsing")
face_parsing_model = AutoModelForSemanticSegmentation.from_pretrained("hila-chefer/face-parsing")
face_parsing_model.to(device)
face_parsing_model.eval()

# 5. Deepfake Detection (microsoft/FaceForensics)
deepfake_processor = AutoImageProcessor.from_pretrained("microsoft/FaceForensics")
deepfake_model = AutoModelForImageClassification.from_pretrained("microsoft/FaceForensics")
deepfake_model.to(device)
deepfake_model.eval()

# -----------------------------
# Helper Functions for New Inferences
# -----------------------------

def compute_facial_recognition(image):
    """
    Detects a face using MediaPipe, crops it, and computes its embedding with DINO-ViT.
    Compares the embedding against a dummy database to "identify" the person.
    """
    frame = np.array(image)
    frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    frame_resized = cv2.resize(frame_bgr, DESIRED_SIZE)
    frame_rgb = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
    
    face_results = face_detection.process(frame_rgb)
    if face_results.detections:
        detection = face_results.detections[0]
        bbox = detection.location_data.relative_bounding_box
        h, w, _ = frame_rgb.shape
        x = int(bbox.xmin * w)
        y = int(bbox.ymin * h)
        box_w = int(bbox.width * w)
        box_h = int(bbox.height * h)
        face_crop = frame_rgb[y:y+box_h, x:x+box_w]
        face_image = Image.fromarray(face_crop)
        
        inputs = facial_recognition_extractor(face_image, return_tensors="pt").to(device)
        with torch.no_grad():
            outputs = facial_recognition_model(**inputs)
        # Use mean pooling over the last hidden state to get an embedding vector
        embeddings = outputs.last_hidden_state.mean(dim=1).squeeze()
        
        # Compare against dummy database using cosine similarity
        best_score = -1
        best_name = "Unknown"
        for name, db_emb in dummy_database.items():
            cos_sim = torch.nn.functional.cosine_similarity(embeddings, db_emb, dim=0)
            if cos_sim > best_score:
                best_score = cos_sim
                best_name = name
        threshold = 0.7  # dummy threshold for identification
        if best_score > threshold:
            result = f"Identified as {best_name} (sim: {best_score:.2f})"
        else:
            result = f"No match found (best: {best_name}, sim: {best_score:.2f})"
        return face_crop, result
    else:
        return frame, "No face detected"

def compute_emotion_detection(image):
    """
    Detects a face, crops it, and classifies the facial expression.
    """
    frame = np.array(image)
    frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    frame_resized = cv2.resize(frame_bgr, DESIRED_SIZE)
    frame_rgb = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
    
    face_results = face_detection.process(frame_rgb)
    if face_results.detections:
        detection = face_results.detections[0]
        bbox = detection.location_data.relative_bounding_box
        h, w, _ = frame_rgb.shape
        x = int(bbox.xmin * w)
        y = int(bbox.ymin * h)
        box_w = int(bbox.width * w)
        box_h = int(bbox.height * h)
        face_crop = frame_rgb[y:y+box_h, x:x+box_w]
        face_image = Image.fromarray(face_crop)
        
        inputs = emotion_processor(face_image, return_tensors="pt").to(device)
        with torch.no_grad():
            outputs = emotion_model(**inputs)
        logits = outputs.logits
        pred = logits.argmax(-1).item()
        label = emotion_model.config.id2label[pred]
        return face_crop, f"Emotion: {label}"
    else:
        return frame, "No face detected"

def compute_age_gender(image):
    """
    Detects a face, crops it, and predicts the age & gender.
    """
    frame = np.array(image)
    frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    frame_resized = cv2.resize(frame_bgr, DESIRED_SIZE)
    frame_rgb = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
    
    face_results = face_detection.process(frame_rgb)
    if face_results.detections:
        detection = face_results.detections[0]
        bbox = detection.location_data.relative_bounding_box
        h, w, _ = frame_rgb.shape
        x = int(bbox.xmin * w)
        y = int(bbox.ymin * h)
        box_w = int(bbox.width * w)
        box_h = int(bbox.height * h)
        face_crop = frame_rgb[y:y+box_h, x:x+box_w]
        face_image = Image.fromarray(face_crop)
        
        inputs = age_gender_processor(face_image, return_tensors="pt").to(device)
        with torch.no_grad():
            outputs = age_gender_model(**inputs)
        logits = outputs.logits
        pred = logits.argmax(-1).item()
        label = age_gender_model.config.id2label[pred]
        return face_crop, f"Age & Gender: {label}"
    else:
        return frame, "No face detected"

def compute_face_parsing(image):
    """
    Runs face parsing (segmentation) on the provided image.
    """
    image_pil = Image.fromarray(np.array(image))
    inputs = face_parsing_processor(image_pil, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = face_parsing_model(**inputs)
    logits = outputs.logits  # shape: (batch, num_labels, H, W)
    segmentation = logits.argmax(dim=1)[0].cpu().numpy()
    # For visualization, we apply a color map to the segmentation mask.
    segmentation_norm = np.uint8(255 * segmentation / (segmentation.max() + 1e-5))
    segmentation_color = cv2.applyColorMap(segmentation_norm, cv2.COLORMAP_JET)
    return segmentation_color, "Face Parsing completed"

def compute_deepfake_detection(image):
    """
    Runs deepfake detection on the image.
    """
    image_pil = Image.fromarray(np.array(image))
    inputs = deepfake_processor(image_pil, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = deepfake_model(**inputs)
    logits = outputs.logits
    pred = logits.argmax(-1).item()
    label = deepfake_model.config.id2label[pred]
    return np.array(image), f"Deepfake Detection: {label}"

# -----------------------------
# Analysis Functions (Wrapping Inference & Green Text)
# -----------------------------

def analyze_facial_recognition(image):
    annotated_face, result = compute_facial_recognition(image)
    return annotated_face, f"<div style='color: lime !important;'>Facial Recognition: {result}</div>"

def analyze_emotion_detection(image):
    face_crop, result = compute_emotion_detection(image)
    return face_crop, f"<div style='color: lime !important;'>{result}</div>"

def analyze_age_gender(image):
    face_crop, result = compute_age_gender(image)
    return face_crop, f"<div style='color: lime !important;'>{result}</div>"

def analyze_face_parsing(image):
    segmentation, result = compute_face_parsing(image)
    return segmentation, f"<div style='color: lime !important;'>{result}</div>"

def analyze_deepfake_detection(image):
    output, result = compute_deepfake_detection(image)
    return output, f"<div style='color: lime !important;'>{result}</div>"

# -----------------------------
# Custom CSS (All Text in Green)
# -----------------------------
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
body {
    background-color: #0e0e0e;
    font-family: 'Orbitron', sans-serif;
    margin: 0;
    padding: 0;
    color: #32CD32;
}
.gradio-container {
    background: linear-gradient(135deg, #1a1a1a, #333333);
    border: 2px solid #32CD32;
    box-shadow: 0 0 15px #32CD32;
    border-radius: 10px;
    padding: 20px;
    max-width: 1200px;
    margin: auto;
}
.gradio-title, .gradio-description, .tab-item, .tab-item * {
    color: #32CD32 !important;
    text-shadow: 0 0 10px #32CD32;
}
input, button, .output {
    border: 1px solid #32CD32;
    box-shadow: 0 0 8px #32CD32;
    color: #32CD32;
}
"""

# -----------------------------
# Create Gradio Interfaces for New Models
# -----------------------------
facial_recognition_interface = gr.Interface(
    fn=analyze_facial_recognition,
    inputs=gr.Image(label="Upload a Face Image for Facial Recognition"),
    outputs=[gr.Image(type="numpy", label="Cropped Face / Embedding Visualization"), 
             gr.HTML(label="Facial Recognition Result")],
    title="Facial Recognition & Identification",
    description="Extracts facial embeddings using facebook/dino-vitb16 and identifies the face by comparing against a dummy database.",
    live=False
)

emotion_interface = gr.Interface(
    fn=analyze_emotion_detection,
    inputs=gr.Image(label="Upload a Face Image for Emotion Detection"),
    outputs=[gr.Image(type="numpy", label="Cropped Face"), 
             gr.HTML(label="Emotion Detection")],
    title="Emotion Detection",
    description="Classifies the facial expression using nateraw/facial-expression-recognition.",
    live=False
)

age_gender_interface = gr.Interface(
    fn=analyze_age_gender,
    inputs=gr.Image(label="Upload a Face Image for Age & Gender Prediction"),
    outputs=[gr.Image(type="numpy", label="Cropped Face"), 
             gr.HTML(label="Age & Gender Prediction")],
    title="Age & Gender Prediction",
    description="Predicts age and gender from the face using oayu/age-gender-estimation.",
    live=False
)

face_parsing_interface = gr.Interface(
    fn=analyze_face_parsing,
    inputs=gr.Image(label="Upload a Face Image for Face Parsing"),
    outputs=[gr.Image(type="numpy", label="Segmentation Overlay"), 
             gr.HTML(label="Face Parsing")],
    title="Face Parsing",
    description="Segments face regions (eyes, nose, lips, hair, etc.) using hila-chefer/face-parsing.",
    live=False
)

deepfake_interface = gr.Interface(
    fn=analyze_deepfake_detection,
    inputs=gr.Image(label="Upload an Image for Deepfake Detection"),
    outputs=[gr.Image(type="numpy", label="Input Image"), 
             gr.HTML(label="Deepfake Detection")],
    title="Deepfake Detection",
    description="Detects manipulated or deepfake images using microsoft/FaceForensics.",
    live=False
)

# -----------------------------
# Create a Tabbed Interface
# -----------------------------
tabbed_interface = gr.TabbedInterface(
    interface_list=[
        facial_recognition_interface, 
        emotion_interface, 
        age_gender_interface, 
        face_parsing_interface, 
        deepfake_interface
    ],
    tab_names=[
        "Facial Recognition",
        "Emotion Detection",
        "Age & Gender",
        "Face Parsing",
        "Deepfake Detection"
    ]
)

# -----------------------------
# Wrap in a Blocks Layout & Launch
# -----------------------------
demo = gr.Blocks(css=custom_css)
with demo:
    gr.Markdown("<h1 class='gradio-title' style='color: #32CD32;'>Multi-Analysis Face App</h1>")
    gr.Markdown("<p class='gradio-description' style='color: #32CD32;'>Upload an image to run advanced face analysis using state-of-the-art Hugging Face models.</p>")
    tabbed_interface.render()

if __name__ == "__main__":
    demo.launch()