File size: 16,376 Bytes
ef07f3f
2b42f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef07f3f
 
 
 
0e514e1
 
ef07f3f
 
 
 
 
2b42f4f
 
 
 
 
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
0e514e1
ef07f3f
 
 
 
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
0e514e1
 
 
ef07f3f
0e514e1
ef07f3f
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
 
 
 
 
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
 
 
 
0e514e1
ef07f3f
 
 
 
0e514e1
2b42f4f
ef07f3f
2b42f4f
 
 
 
 
 
 
 
ef07f3f
2b42f4f
ef07f3f
 
0e514e1
ef07f3f
2b42f4f
0e514e1
 
 
 
ef07f3f
 
 
 
 
 
0e514e1
2b42f4f
ef07f3f
0e514e1
ef07f3f
 
 
4c6ee84
ef07f3f
 
0e514e1
ef07f3f
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
0e514e1
ef07f3f
 
 
 
 
0e514e1
2b42f4f
ef07f3f
2b42f4f
 
 
ef07f3f
0e514e1
2b42f4f
0e514e1
ef07f3f
 
0e514e1
2b42f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e514e1
ef07f3f
 
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
 
0e514e1
 
 
ef07f3f
 
 
 
 
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
 
 
 
 
0e514e1
ef07f3f
 
 
 
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
4c6ee84
ef07f3f
0e514e1
ef07f3f
 
 
 
0e514e1
ef07f3f
 
 
 
 
 
0e514e1
ef07f3f
 
 
 
 
 
0e514e1
ef07f3f
 
 
 
 
 
0e514e1
17cfe59
0e514e1
ef07f3f
 
 
 
 
 
 
0e514e1
ef07f3f
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
0e514e1
ef07f3f
4c6ee84
ef07f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e514e1
ef07f3f
0e514e1
ef07f3f
 
 
 
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
0e514e1
ef07f3f
 
 
0e514e1
ef07f3f
 
2b42f4f
ef07f3f
 
0e514e1
ef07f3f
 
 
4c6ee84
 
0e514e1
ef07f3f
0e514e1
4c6ee84
 
0e514e1
 
4c6ee84
 
0e514e1
ef07f3f
0e514e1
4c6ee84
 
2b42f4f
ef07f3f
4c6ee84
2b42f4f
 
 
 
4c6ee84
0e514e1
ef07f3f
0e514e1
4c6ee84
 
0e514e1
 
4c6ee84
 
0e514e1
ef07f3f
0e514e1
4c6ee84
 
2b42f4f
ef07f3f
4c6ee84
 
 
 
 
 
0e514e1
ef07f3f
0e514e1
4c6ee84
 
2b42f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef07f3f
0e514e1
4386899
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Install required packages
import os
import subprocess
import sys
import importlib
import pkg_resources

def install_package(package, version=None):
    package_spec = f"{package}=={version}" if version else package
    print(f"Installing {package_spec}...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", package_spec])
    except subprocess.CalledProcessError as e:
        print(f"Failed to install {package_spec}: {e}")
        raise

def ensure_package(package, version=None):
    try:
        if version:
            pkg_resources.require(f"{package}=={version}")
        else:
            importlib.import_module(package)
        print(f"{package} is already installed with the correct version.")
    except (ImportError, pkg_resources.VersionConflict, pkg_resources.DistributionNotFound) as e:
        print(f"Package requirement failed: {e}")
        install_package(package, version)

# Check environment and install dependencies
if not os.path.exists("/.dockerenv") and not os.path.exists("/kaggle"):
    print("Setting up environment...")
    
    # Install core dependencies
    ensure_package("numpy", "1.23.5")
    ensure_package("protobuf", "3.20.3")
    ensure_package("tensorflow", "2.10.0")
    ensure_package("opencv-python-headless", "4.7.0.72")  # Fix for cv2 error
    ensure_package("deepface", "0.0.79")
    ensure_package("gradio", "3.50.2")
    
    # Install additional required packages
    for pkg in ["matplotlib", "pillow", "pandas"]:
        ensure_package(pkg)

# Now import the required modules
import gradio as gr
import json
import cv2
import numpy as np
from deepface import DeepFace
import matplotlib.pyplot as plt
from PIL import Image
import tempfile
import pandas as pd
import shutil

# Google Drive integration (for Colab users)
if 'google.colab' in sys.modules:
    from google.colab import drive
    drive.mount('/content/drive')

def verify_faces(img1, img2, threshold=0.70, model="VGG-Face"):
    temp_dir = tempfile.mkdtemp()
    img1_path = os.path.join(temp_dir, "image1.jpg")
    img2_path = os.path.join(temp_dir, "image2.jpg")
    
    if isinstance(img1, np.ndarray):
        Image.fromarray(img1).save(img1_path)
    else:
        img1.save(img1_path)
        
    if isinstance(img2, np.ndarray):
        Image.fromarray(img2).save(img2_path)
    else:
        img2.save(img2_path)
    
    try:
        result = DeepFace.verify(
            img1_path=img1_path, 
            img2_path=img2_path,
            model_name=model,
            distance_metric="cosine",
            threshold=threshold
        )
        
        fig, ax = plt.subplots(1, 2, figsize=(10, 5))
        
        img1_display = cv2.imread(img1_path)
        img1_display = cv2.cvtColor(img1_display, cv2.COLOR_BGR2RGB)
        img2_display = cv2.imread(img2_path)
        img2_display = cv2.cvtColor(img2_display, cv2.COLOR_BGR2RGB)
        
        ax[0].imshow(img1_display)
        ax[0].set_title("Image 1")
        ax[0].axis("off")
        
        ax[1].imshow(img2_display)
        ax[1].set_title("Image 2")
        ax[1].axis("off")
        
        verification_result = "βœ… FACE MATCHED" if result["verified"] else "❌ FACE NOT MATCHED"
        confidence = round((1 - result["distance"]) * 100, 2)
        
        plt.suptitle(f"{verification_result}\nConfidence: {confidence}%\nDistance: {result['distance']:.4f}", 
                    fontsize=16, fontweight='bold', 
                    color='green' if result["verified"] else 'red')
        
        plt.tight_layout()
        
        os.remove(img1_path)
        os.remove(img2_path)
        os.rmdir(temp_dir)
        
        return fig, json.dumps(result, indent=2)
    
    except Exception as e:
        if os.path.exists(img1_path):
            os.remove(img1_path)
        if os.path.exists(img2_path):
            os.remove(img2_path)
        if os.path.exists(temp_dir):
            os.rmdir(temp_dir)
        
        error_msg = f"Error: {str(e)}"
        if "No face detected" in str(e):
            error_msg = "No face detected in one or both images. Please try different images."
        
        return None, error_msg

def find_faces(query_img, db_folder, threshold=0.70, model="VGG-Face"):
    temp_dir = tempfile.mkdtemp()
    query_path = os.path.join(temp_dir, "query.jpg")
    
    if isinstance(query_img, np.ndarray):
        Image.fromarray(query_img).save(query_path)
    else:
        query_img.save(query_path)
    
    # Handle cloud storage paths and uploaded files
    if isinstance(db_folder, str):
        # Check if it's a Google Drive path
        if db_folder.startswith("/content/drive"):
            db_path = db_folder
        else:
            # Handle regular path
            db_path = os.path.abspath(db_folder)
            if not os.path.exists(db_path):
                return None, "Invalid database path - directory does not exist"
    else:
        # Handle uploaded files
        db_path = os.path.join(temp_dir, "db")
        os.makedirs(db_path, exist_ok=True)
        
        for i, file in enumerate(db_folder):
            orig_filename = file.orig_name
            file_ext = os.path.splitext(orig_filename)[1]
            new_filename = f"image_{i}{file_ext}"
            shutil.copy(file.name, os.path.join(db_path, new_filename))
    
    try:
        dfs = DeepFace.find(
            img_path=query_path,
            db_path=db_path,
            model_name=model,
            distance_metric="cosine",
            threshold=threshold,
            silent=True
        )
        
        if isinstance(dfs, list):
            if len(dfs) == 0:
                return None, "No matching faces found in the database."
            df = dfs[0]
        else:
            df = dfs
            
        if df.empty:
            return None, "No matching faces found in the database."
        
        df = df.sort_values(by=["distance"])
        
        num_matches = min(4, len(df))
        fig, axes = plt.subplots(1, num_matches + 1, figsize=(15, 5))
        
        query_display = cv2.imread(query_path)
        query_display = cv2.cvtColor(query_display, cv2.COLOR_BGR2RGB)
        axes[0].imshow(query_display)
        axes[0].set_title("Query Image")
        axes[0].axis("off")
        
        valid_matches = 0
        for i in range(num_matches):
            if i >= len(df):
                break
                
            match_path = df.iloc[i]["identity"]
            if not os.path.exists(match_path):
                continue
                
            distance = df.iloc[i]["distance"]
            confidence = round((1 - distance) * 100, 2)
            
            try:
                match_img = cv2.imread(match_path)
                if match_img is None:
                    continue
                    
                match_img = cv2.cvtColor(match_img, cv2.COLOR_BGR2RGB)
                axes[valid_matches+1].imshow(match_img)
                axes[valid_matches+1].set_title(f"Match #{valid_matches+1}\nConfidence: {confidence}%")
                axes[valid_matches+1].axis("off")
                valid_matches += 1
            except:
                continue
        
        # Hide empty axes
        for j in range(valid_matches+1, num_matches+1):
            axes[j].axis("off")
        
        plt.suptitle(f"Found {len(df)} matching faces", fontsize=16, fontweight='bold')
        plt.tight_layout()
        
        results = df[["identity", "distance"]].copy()
        results["confidence"] = (1 - results["distance"]) * 100
        results["confidence"] = results["confidence"].round(2)
        results = results.rename(columns={"identity": "Image Path"})
        
        os.remove(query_path)
        if not isinstance(db_folder, str):
            shutil.rmtree(db_path)
        
        return fig, results.to_dict('records')
    
    except Exception as e:
        if os.path.exists(query_path):
            os.remove(query_path)
        
        error_msg = f"Error: {str(e)}"
        if "No face detected" in str(e):
            error_msg = "No face detected in the query image. Please try a different image."
        elif "No such file or directory" in str(e):
            error_msg = "Invalid database path or corrupted image files"
        
        return None, error_msg

def analyze_face(img, actions=['age', 'gender', 'race', 'emotion']):
    temp_dir = tempfile.mkdtemp()
    img_path = os.path.join(temp_dir, "analyze.jpg")
    
    if isinstance(img, np.ndarray):
        Image.fromarray(img).save(img_path)
    else:
        img.save(img_path)
    
    try:
        results = DeepFace.analyze(
            img_path=img_path,
            actions=actions,
            enforce_detection=True,
            detector_backend='opencv'
        )
        
        if isinstance(results, list):
            num_faces = len(results)
        else:
            num_faces = 1
            results = [results]
        
        fig = plt.figure(figsize=(14, 7))
        
        img_display = cv2.imread(img_path)
        img_display = cv2.cvtColor(img_display, cv2.COLOR_BGR2RGB)
        
        main_ax = plt.subplot2grid((2, 4), (0, 0), colspan=2, rowspan=2)
        main_ax.imshow(img_display)
        main_ax.set_title(f"Analyzed Image ({num_faces} face{'s' if num_faces > 1 else ''} detected)")
        main_ax.axis('off')
        
        for i, face_result in enumerate(results):
            if i >= 4:
                break
                
            age = face_result.get('age', 'N/A')
            gender = face_result.get('dominant_gender', 'N/A')
            race = face_result.get('dominant_race', 'N/A')
            emotion = face_result.get('dominant_emotion', 'N/A')
            
            gender_conf = 'N/A'
            if 'gender' in face_result and isinstance(face_result['gender'], dict):
                for g, conf in face_result['gender'].items():
                    if g.lower() == gender.lower():
                        gender_conf = f"{conf:.1f}%"
                        break
            
            race_conf = 'N/A'
            if 'race' in face_result and isinstance(face_result['race'], dict):
                for r, conf in face_result['race'].items():
                    if r.lower() == race.lower():
                        race_conf = f"{conf:.1f}%"
                        break
            
            emotion_conf = 'N/A'
            if 'emotion' in face_result and isinstance(face_result['emotion'], dict):
                for e, conf in face_result['emotion'].items():
                    if e.lower() == emotion.lower():
                        emotion_conf = f"{conf:.1f}%"
                        break
            
            ax = plt.subplot2grid((2, 4), (0 if i < 2 else 1, 2 + (i % 2)))
            
            text = (
                f"Face #{i+1}\n\n"
                f"Age: {age}\n\n"
                f"Gender: {gender} ({gender_conf})\n\n"
                f"Race: {race} ({race_conf})\n\n"
                f"Emotion: {emotion} ({emotion_conf})"
            )
            
            ax.text(0.5, 0.5, text, ha='center', va='center', fontsize=11)
            ax.axis('off')
        
        plt.tight_layout()
        
        os.remove(img_path)
        os.rmdir(temp_dir)
        
        formatted_results = []
        for i, res in enumerate(results[:8]):
            face_data = {
                "face_number": i+1,
                "age": res.get("age", "N/A"),
                "gender": {
                    "dominant": res.get("dominant_gender", "N/A"),
                    "confidence": res.get("gender", {})
                },
                "race": {
                    "dominant": res.get("dominant_race", "N/A"),
                    "confidence": res.get("race", {})
                },
                "emotion": {
                    "dominant": res.get("dominant_emotion", "N/A"),
                    "confidence": res.get("emotion", {})
                }
            }
            formatted_results.append(face_data)
        
        return fig, formatted_results
    
    except Exception as e:
        if os.path.exists(img_path):
            os.remove(img_path)
        if os.path.exists(temp_dir):
            os.rmdir(temp_dir)
        
        error_msg = f"Error: {str(e)}"
        if "No face detected" in str(e):
            error_msg = "No face detected in the image. Please try a different image."
        
        return None, error_msg

# Create Gradio interface
with gr.Blocks(title="Complete Face Recognition Tool", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ” Complete Face Recognition Tool
    
    This tool provides three face recognition features:
    - **Verify Faces**: Compare two specific images to check if they contain the same person
    - **Find Faces**: Search for matching faces in a database/folder (supports Google Drive paths in Colab)
    - **Analyze Face**: Determine age, gender, race, and emotion from a facial image
    """)
    
    with gr.Tabs():
        with gr.TabItem("Verify Faces"):
            with gr.Row():
                img1_input = gr.Image(label="First Image", type="pil")
                img2_input = gr.Image(label="Second Image", type="pil")
            
            with gr.Row():
                verify_threshold = gr.Slider(minimum=0.1, maximum=0.9, value=0.6, step=0.05, 
                                       label="Similarity Threshold (lower = stricter matching)")
                verify_model = gr.Dropdown(
                    choices=["VGG-Face", "Facenet", "OpenFace", "DeepFace", "ArcFace"], 
                    value="VGG-Face", 
                    label="Face Recognition Model"
                )
            
            verify_button = gr.Button("Verify Faces", variant="primary")
            
            verify_result_plot = gr.Plot(label="Verification Result")
            verify_json = gr.JSON(label="Technical Details")

        with gr.TabItem("Find Faces"):
            query_img = gr.Image(label="Query Image (Face to find)", type="pil")
            db_path_input = gr.Textbox(
                label="Database Path (folder path or Google Drive path in Colab)",
                placeholder="/content/drive/MyDrive/your_folder"
            )
            db_files_input = gr.File(label="Or upload images for database", file_count="multiple")
            
            with gr.Row():
                find_threshold = gr.Slider(minimum=0.1, maximum=0.9, value=0.6, step=0.05, 
                                     label="Similarity Threshold (lower = stricter matching)")
                find_model = gr.Dropdown(
                    choices=["VGG-Face", "Facenet", "OpenFace", "DeepFace", "ArcFace"], 
                    value="VGG-Face", 
                    label="Face Recognition Model"
                )
            
            find_button = gr.Button("Find Matching Faces", variant="primary")
            
            find_result_plot = gr.Plot(label="Search Results")
            find_results_table = gr.JSON(label="Detailed Results")

        with gr.TabItem("Analyze Face"):
            analyze_img = gr.Image(label="Upload Image for Analysis", type="pil")
            actions_checkboxes = gr.CheckboxGroup(
                choices=["age", "gender", "race", "emotion"],
                value=["age", "gender", "race", "emotion"],
                label="Select Attributes to Analyze"
            )
            
            analyze_button = gr.Button("Analyze Face", variant="primary")
            
            analyze_result_plot = gr.Plot(label="Analysis Results")
            analyze_json = gr.JSON(label="Detailed Analysis")

    # Setup all button clicks
    verify_button.click(
        verify_faces, 
        inputs=[img1_input, img2_input, verify_threshold, verify_model], 
        outputs=[verify_result_plot, verify_json]
    )
    
    find_button.click(
        find_faces,
        inputs=[query_img, db_path_input, find_threshold, find_model],
        outputs=[find_result_plot, find_results_table]
    )
    
    db_files_input.change(
        lambda x: "",
        inputs=db_files_input,
        outputs=db_path_input
    )
    
    analyze_button.click(
        analyze_face,
        inputs=[analyze_img, actions_checkboxes],
        outputs=[analyze_result_plot, analyze_json]
    )

# Launch the app
demo.launch()