File size: 14,148 Bytes
3c2d0be cde4007 3c2d0be 055c27b 3c2d0be 055c27b 3c2d0be cde4007 3c2d0be cde4007 3c2d0be cde4007 3c2d0be 13c5c64 3c2d0be 13c5c64 3c2d0be 4f98cdc 3c2d0be 055c27b 3c2d0be 13c5c64 3c2d0be c08ea23 3c2d0be 055c27b 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be f36e5ac c08ea23 3c2d0be c08ea23 3c2d0be 20f8528 3c2d0be c08ea23 3c2d0be 13c5c64 c08ea23 3c2d0be c08ea23 3c2d0be 20f8528 3c2d0be c08ea23 13c5c64 3c2d0be 13c5c64 3c2d0be c08ea23 3c2d0be 13c5c64 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be c08ea23 3c2d0be 055c27b 3c2d0be 4f98cdc 3c2d0be f36e5ac 3c2d0be 13c5c64 3c2d0be 20f8528 3c2d0be |
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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
import gradio as gr
import os
import requests
from PIL import Image
import json
# Configuration
API_BASE_URL = os.getenv("API_BASE_URL")
API_TOKEN = os.getenv("API_TOKEN")
def face_compare(frame1, frame2, request: gr.Request = None):
"""Face comparison with enhanced result display"""
try:
url = f"{API_BASE_URL}"
# Prepare files
files = {}
if frame1:
files['file1'] = open(frame1, 'rb')
if frame2:
files['file2'] = open(frame2, 'rb')
if not files:
return "<div class='error-message'>Please upload both images</div>"
# Add Bearer token to headers
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
# Make API request
response = requests.post(url=url, files=files, headers=headers)
result = response.json()
# Close files
for file in files.values():
file.close()
# Enhanced result processing
return format_face_comparison_result(result, frame1, frame2)
except Exception as e:
return f"<div class='error-message'>Error processing request</div>"
def format_face_comparison_result(result, img1_path, img2_path):
"""Format face comparison results with professional styling"""
detections = result.get("detections", [])
matches = result.get("match", [])
# Create result HTML
html = "<div class='result-content'>"
# Detection results - show all detected faces
if detections:
for i, detection in enumerate(detections):
face_image = detection.get("face", "")
first_face_index = detection.get("firstFaceIndex")
second_face_index = detection.get("secondFaceIndex")
# Matching results in the new table format
if matches:
html += """
<div>
<div class="matches-table">
<table>
<thead>
<tr>
<th>First Face</th>
<th>Second Face</th>
<th>Similarity Score</th>
<th>Result</th>
</tr>
</thead>
<tbody>
"""
# Group matches by first image face index for better organization
match_groups = {}
for match in matches:
first_face_index = match.get("firstFaceIndex", "N/A")
if first_face_index not in match_groups:
match_groups[first_face_index] = []
match_groups[first_face_index].append(match)
row_number = 1
for first_face_index in sorted(match_groups.keys()):
for match in match_groups[first_face_index]:
first_face_index = match.get("firstFaceIndex", "N/A")
second_face_index = match.get("secondFaceIndex", "N/A")
similarity = match.get("similarity", 0)
# Get face images for display
first_face_img = ""
second_face_img = ""
for detection in detections:
if detection.get("firstFaceIndex") == first_face_index:
first_face_img = detection.get("face", "")
if detection.get("secondFaceIndex") == second_face_index:
second_face_img = detection.get("face", "")
# Determine result and color
if similarity >= 0.6: # Threshold for same person
result_text = "same person"
result_class = "result-same"
else:
result_text = "different person"
result_class = "result-different"
first_face_display = f"<img src='data:image/png;base64,{first_face_img}' class='table-face-thumbnail' />" if first_face_img else f"Face {first_face_index}"
second_face_display = f"<img src='data:image/png;base64,{second_face_img}' class='table-face-thumbnail' />" if second_face_img else f"Face {second_face_index}"
html += f"""
<tr>
<td class="face-cell">
<div class="face-display">
{first_face_display}
<div class="face-label">Face {first_face_index}</div>
</div>
</td>
<td class="face-cell">
<div class="face-display">
{second_face_display}
<div class="face-label">Face {second_face_index}</div>
</div>
</td>
<td class="similarity-score">{similarity:.4f}</td>
<td><span class="result-text {result_class}">{result_text}</span></td>
</tr>
"""
row_number += 1
html += """
</tbody>
</table>
</div>
</div>
"""
else:
html += "<div class='no-results'>No face matches found.</div>"
html += "</div>"
return html
def get_custom_css():
"""Return simplified CSS styling that works for both light and dark themes"""
return """
/* Center everything */
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
/* Header styling - logo and text in same line */
.company-header {
background: var(--background-fill-primary);
padding: 10px;
text-align: center;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: 25px;
flex-wrap: wrap;
}
.header-logo {
flex-shrink: 0;
}
.header-logo img {
width: 80px;
height: auto;
}
.header-text {
text-align: center;
}
.header-text h1 {
font-size: 2.4em !important;
font-weight: 700;
color: var(--body-text-color);
}
.header-text p {
font-size: 1.3em !important;
color: var(--body-text-color);
opacity: 0.8;
}
/* Main content layout */
.main-content-row {
display: flex;
gap: 25px;
width: 100%;
}
.upload-section {
flex: 2;
display: flex;
flex-direction: column;
gap: 20px;
}
.result-section {
flex: 1.2;
}
.upload-images-row {
display: flex;
gap: 20px;
width: 100%;
}
.upload-image-col {
flex: 1;
}
/* Button styling */
.button-primary {
background: var(--button-primary-background-fill) !important;
border: none !important;
padding: 6px 12px !important;
font-size: 1.2em !important;
font-weight: 600 !important;
color: var(--button-primary-text-color) !important;
border-radius: 8px !important;
cursor: pointer !important;
transition: background-color 0.2s ease !important;
width: 100% !important;
}
.button-primary:hover {
background: var(--button-primary-background-fill-hover) !important;
}
.result-content {
width: 100%;
}
/* Detection cards */
.detections-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 15px;
justify-content: center;
}
.detection-card {
background: var(--background-fill-secondary);
padding: 4px;
border-radius: 8px;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
}
.face-thumbnail {
width: 60px;
height: 60px;
border-radius: 50%;
object-fit: cover;
}
/* Matching table - NEW STYLING */
.matches-table {
display: flex;
justify-content: center;
width: 100%;
overflow-x: auto;
}
.matches-table table {
width: 100%;
border-collapse: collapse;
font-size: 1em !important;
min-width: 450px;
}
.matches-table th {
background: var(--background-fill-secondary);
color: var(--body-text-color);
padding: 4px 2px !important;
text-align: center;
font-size: 1em !important;
font-weight: 700;
border-bottom: 2px solid var(--border-color-primary);
}
.matches-table td {
padding: 4px 2px !important;
border-bottom: 1px solid var(--border-color-primary);
text-align: center;
font-size: 0.95em !important;
color: var(--body-text-color);
}
.face-cell {
vertical-align: middle;
}
.face-display {
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}
.table-face-thumbnail {
width: 70px;
height: 70px;
border-radius: 50%;
object-fit: cover;
border: 2px solid var(--border-color-primary);
}
.face-label {
font-size: 0.9em !important;
color: var(--body-text-color);
opacity: 1;
font-weight: 600;
}
.similarity-score {
font-weight: 700;
color: var(--body-text-color);
font-size: 1.05em !important;
}
.result-text {
padding: 8px 12px !important;
border-radius: 12px;
font-size: 1.1em !important;
font-weight: 700;
text-transform: capitalize;
}
.result-same {
background: #d4edda;
color: #155724;
}
.result-different {
background: #f8d7da;
color: #721c24;
}
.no-results {
text-align: center;
padding: 40px;
color: var(--body-text-color);
opacity: 0.7;
font-style: italic;
font-size: 1.1em !important;
}
/* Error messages */
.error-message {
background: var(--background-fill-secondary);
color: var(--body-text-color);
padding: 20px;
border-radius: 8px;
text-align: center;
width: 100%;
opacity: 0.9;
font-size: 1.1em !important;
}
"""
# Create Gradio interface
with gr.Blocks(
title="MiniAiLive - Face Recognition WebAPI Playground",
css=get_custom_css()
) as demo:
with gr.Column(elem_classes="container"):
# Header Section - Logo and text in same line, centered
gr.HTML("""
<div class="company-header">
<div class="header-logo">
<img src="https://miniai.live/wp-content/uploads/2025/11/logo_new.png" alt="MiniAiLive Logo">
</div>
<div class="header-text">
<h1>MiniAiLive Face Recognition WebAPI Playground</h1>
<p>Experience our NIST FRVT Top Ranked 1:1 & 1:N Face Matching Technology</p>
</div>
</div>
""")
# Main Content - Upload and Results
with gr.Row(elem_classes="main-content-row"):
# Upload Section
with gr.Column(scale=0.6, elem_classes="upload-section"):
with gr.Row(elem_classes="upload-images-row"):
# First Image
with gr.Column(scale=1, elem_classes="upload-image-col"):
im_match_in1 = gr.Image(
type='filepath',
height=380,
label="First Image",
show_download_button=False
)
gr.Examples(
examples=[
"assets/1.jpg",
"assets/2.jpg",
"assets/3.jpg",
"assets/4.jpg",
],
inputs=im_match_in1,
label="First Image Examples"
)
# Second Image
with gr.Column(scale=1, elem_classes="upload-image-col"):
im_match_in2 = gr.Image(
type='filepath',
height=380,
label="Second Image",
show_download_button=False
)
gr.Examples(
examples=[
"assets/1-1.jpg",
"assets/2-1.jpg",
"assets/3-1.jpg",
"assets/4-1.jpg",
],
inputs=im_match_in2,
label="Second Image Examples"
)
btn_f_match = gr.Button(
"Compare Faces 🚀",
variant='primary',
elem_classes="button-primary"
)
# Results Section
with gr.Column(scale=0.4, elem_classes="result-section"):
txt_compare_out = gr.HTML(
value="<div style='text-align: center; padding: 10px; font-size: 1.1em;'>Results will appear here after comparison</div>"
)
# Connect the function
btn_f_match.click(
face_compare,
inputs=[im_match_in1, im_match_in2],
outputs=txt_compare_out
)
if __name__ == "__main__":
demo.launch(
share=False,
show_api=False,
server_name="0.0.0.0",
server_port=7860
) |