File size: 23,311 Bytes
0cd6025 |
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
#!/usr/bin/env python3
"""
Audio-visual Speech Separation Gradio App - Hugging Face Space Version
Automatically detects and separates all speakers in videos
"""
import warnings
warnings.filterwarnings("ignore")
import os
import gradio as gr
import numpy as np
import shutil
import tempfile
import time
import sys
import threading
from PIL import Image, ImageDraw, ImageFont
from moviepy import *
import spaces
from face_detection_utils import detect_faces
# Use HF Space's temp directory
TEMP_DIR = os.environ.get('TMPDIR', '/tmp')
# Shared state for relaying GPU-side status back to the UI thread.
GPU_PROGRESS_STATE = {"progress": 0.0, "status": "Processing on GPU..."}
GPU_PROGRESS_LOCK = threading.Lock()
class LogCollector:
"""Collect logs in a list"""
def __init__(self):
self.logs = []
def add(self, message):
if message and message.strip():
timestamp = time.strftime("%H:%M:%S")
self.logs.append(f"[{timestamp}] {message.strip()}")
def get_text(self, last_n=None):
if last_n:
return "\n".join(self.logs[-last_n:])
return "\n".join(self.logs)
# Global log collector for capturing print statements
GLOBAL_LOG = LogCollector()
class StdoutCapture:
"""Capture stdout and add to log"""
def __init__(self, original):
self.original = original
def write(self, text):
self.original.write(text)
if text.strip():
GLOBAL_LOG.add(text.strip())
def flush(self):
self.original.flush()
def remove_duplicate_faces(boxes, probs, iou_threshold=0.5):
"""Remove duplicate face detections using IoU (Intersection over Union)"""
if len(boxes) <= 1:
return boxes, probs
# Calculate IoU between all pairs of boxes
def calculate_iou(box1, box2):
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
intersection = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
union = area1 + area2 - intersection
return intersection / union if union > 0 else 0
# Keep track of which boxes to keep
keep = []
used = set()
# Sort by confidence (if available) or by area
if probs is not None:
sorted_indices = np.argsort(probs)[::-1]
else:
areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
sorted_indices = np.argsort(areas)[::-1]
for i in sorted_indices:
if i in used:
continue
keep.append(i)
used.add(i)
# Mark overlapping boxes as used
for j in range(len(boxes)):
if j != i and j not in used:
iou = calculate_iou(boxes[i], boxes[j])
if iou > iou_threshold:
used.add(j)
# Return filtered boxes and probs
keep = sorted(keep) # Maintain original order
filtered_boxes = boxes[keep]
filtered_probs = probs[keep] if probs is not None else None
return filtered_boxes, filtered_probs
def process_detected_faces(boxes, probs, frame_rgb, frame_pil):
"""Process detected faces and return face images"""
face_images = []
full_frame_annotated = frame_rgb.copy()
if boxes is None or len(boxes) == 0:
return [], 0, full_frame_annotated, "No faces detected"
boxes = np.asarray(boxes, dtype=np.float32)
# Filter by confidence if available
if probs is not None:
# Keep faces with confidence > 0.9
confident_indices = probs > 0.9
boxes = boxes[confident_indices]
probs = probs[confident_indices]
print(f"After filtering by confidence: {len(boxes)} faces")
if len(boxes) == 0:
return [], 0, full_frame_annotated, "No faces passed the confidence filter"
# Remove duplicate detections
boxes, probs = remove_duplicate_faces(boxes, probs, iou_threshold=0.3)
print(f"After removing duplicates: {len(boxes)} faces")
if len(boxes) == 0:
return [], 0, full_frame_annotated, "No faces remained after duplicate removal"
# Sort boxes by area (larger faces first)
areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
sorted_indices = np.argsort(areas)[::-1]
boxes = boxes[sorted_indices]
# Annotate full frame
full_frame_pil = Image.fromarray(full_frame_annotated)
draw = ImageDraw.Draw(full_frame_pil)
# Try to use a better font
try:
font = ImageFont.load_default()
except:
font = None
# Extract face images and annotate
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]
for i, box in enumerate(boxes):
color = colors[i % len(colors)]
# Draw bounding box
draw.rectangle(box.tolist(), outline=color, width=4)
label = f"Speaker {i+1}"
# Draw label
if font:
draw.text((box[0] + 5, box[1] - 20), label, fill=color, font=font)
# Extract face with margin
margin = 50
x1 = max(0, int(box[0] - margin))
y1 = max(0, int(box[1] - margin))
x2 = min(frame_rgb.shape[1], int(box[2] + margin))
y2 = min(frame_rgb.shape[0], int(box[3] + margin))
face_crop = frame_rgb[y1:y2, x1:x2]
# Resize maintaining aspect ratio
face_crop = Image.fromarray(face_crop)
face_crop.thumbnail((250, 250), Image.Resampling.LANCZOS)
face_crop = np.array(face_crop)
face_images.append(face_crop)
full_frame_annotated = np.array(full_frame_pil)
return face_images, len(boxes), full_frame_annotated, None
@spaces.GPU(duration=60, enable_queue=True)
def detect_faces_gpu(frame_pil):
"""GPU-accelerated face detection"""
print("Detecting faces with RetinaFace")
frame_array = np.array(frame_pil)
boxes, probs = detect_faces(
frame_array,
threshold=0.9,
allow_upscaling=False,
)
if boxes is None or len(boxes) == 0:
print("No faces detected at high threshold, relaxing criteria...")
boxes, probs = detect_faces(
frame_array,
threshold=0.7,
allow_upscaling=True,
)
return boxes, probs
def detect_and_extract_all_faces(video_path):
"""Detect all faces in the first frame and extract them"""
print("Starting face detection...")
# Check if video file exists
if not os.path.exists(video_path):
print(f"Error: Video file does not exist at path: {video_path}")
return [], 0, None, f"Video file not found: {video_path}"
print(f"Video path: {video_path}")
print(f"File size: {os.path.getsize(video_path) / 1024 / 1024:.2f} MB")
# Use moviepy to read video
print("Opening video with moviepy...")
try:
clip = VideoFileClip(video_path)
# Get video properties
fps = clip.fps
duration = clip.duration
total_frames = int(fps * duration)
print(f"Video info: FPS: {fps}, Duration: {duration}s, Total frames: {total_frames}")
# Get first frame
frame = clip.get_frame(0) # MoviePy returns RGB
frame_rgb = (frame * 255).astype(np.uint8) if frame.max() <= 1.0 else frame.astype(np.uint8)
print(f"Successfully read frame with moviepy: {frame_rgb.shape}")
# Close the clip to free resources
clip.close()
# Convert to PIL for downstream processing
frame_pil = Image.fromarray(frame_rgb)
# Detect faces using RetinaFace
print("Detecting faces with RetinaFace...")
boxes, probs = detect_faces(
frame_rgb,
threshold=0.9,
allow_upscaling=False,
)
if boxes is None or len(boxes) == 0:
print("No faces detected at high threshold, trying relaxed settings...")
boxes, probs = detect_faces(
frame_rgb,
threshold=0.7,
allow_upscaling=True,
)
if boxes is not None and len(boxes) > 0:
print(f"Detected {len(boxes)} faces")
return process_detected_faces(boxes, probs, frame_rgb, frame_pil)
else:
return [], 0, frame_rgb, "No faces detected in the first frame"
except Exception as e:
print(f"MoviePy failed: {e}")
import traceback
traceback.print_exc()
return [], 0, None, f"Failed to open video file. Error: {str(e)}"
@spaces.GPU(duration=300, enable_queue=True)
def process_video_gpu(video_file, temp_dir, num_speakers):
"""GPU-accelerated video processing"""
try:
from Inference_with_status import process_video_with_status
# Define status callback inside GPU function
def gpu_status_callback(message):
status_text = message.get('status', 'Processing...')
print(f"GPU Processing: {status_text}")
progress_value = message.get('progress')
with GPU_PROGRESS_LOCK:
GPU_PROGRESS_STATE["status"] = status_text
if progress_value is not None:
try:
numeric_progress = float(progress_value)
GPU_PROGRESS_STATE["progress"] = min(max(numeric_progress, 0.0), 1.0)
except (TypeError, ValueError):
pass
output_files = process_video_with_status(
input_file=video_file,
output_path=temp_dir,
number_of_speakers=num_speakers,
detect_every_N_frame=8,
scalar_face_detection=1.5,
status_callback=gpu_status_callback
)
return output_files
except ImportError:
from Inference import process_video
print("Using standard process_video (status callbacks not available)")
output_files = process_video(
input_file=video_file,
output_path=temp_dir,
number_of_speakers=num_speakers,
detect_every_N_frame=8,
scalar_face_detection=1.5
)
return output_files
def process_video_auto(video_file, progress=gr.Progress()):
"""Process video with automatic speaker detection and stream status updates"""
global GLOBAL_LOG
GLOBAL_LOG = LogCollector()
old_stdout = sys.stdout
sys.stdout = StdoutCapture(old_stdout)
status_value = "โณ Ready to process..."
detected_info_output = gr.update(visible=False)
face_gallery_output = gr.update(visible=False)
output_video_output = gr.update(visible=False)
video_dict_value = None
annotated_frame_output = gr.update(visible=False)
def snapshot():
return (
status_value,
detected_info_output,
face_gallery_output,
output_video_output,
video_dict_value,
annotated_frame_output,
GLOBAL_LOG.get_text()
)
try:
if video_file is None:
status_value = "โ ๏ธ Please upload a video file"
yield snapshot()
return
progress(0, desc="Starting processing...")
status_value = "๐ Starting processing..."
GLOBAL_LOG.add("Starting video processing...")
yield snapshot()
temp_dir = None
try:
temp_dir = tempfile.mkdtemp(dir=TEMP_DIR)
print(f"Created temporary directory: {temp_dir}")
progress(0.1, desc="Detecting speakers in video...")
status_value = "๐ Detecting speakers in video..."
print("Starting face detection in video...")
yield snapshot()
face_images, num_speakers, annotated_frame, error_msg = detect_and_extract_all_faces(video_file)
print(f"Face detection completed. Found {num_speakers} speakers.")
if error_msg:
print(f"Error: {error_msg}")
status_value = f"โ {error_msg}"
if annotated_frame is not None:
annotated_frame_output = gr.update(value=annotated_frame, visible=True)
yield snapshot()
return
if num_speakers == 0:
print("No speakers detected in the video.")
status_value = "โ No speakers detected in the video. Please ensure faces are visible in the first frame."
if annotated_frame is not None:
annotated_frame_output = gr.update(value=annotated_frame, visible=True)
yield snapshot()
return
face_gallery_images = [(img, f"Speaker {i+1}") for i, img in enumerate(face_images)]
detected_info = f"๐ฏ Detected {num_speakers} speaker{'s' if num_speakers > 1 else ''} in the video"
detected_info_output = gr.update(value=detected_info, visible=True)
face_gallery_output = gr.update(value=face_gallery_images, visible=True)
if annotated_frame is not None:
annotated_frame_output = gr.update(value=annotated_frame, visible=True)
progress(0.3, desc=f"Separating {num_speakers} speakers...")
status_value = f"๐ฌ Separating {num_speakers} speakers..."
print(f"Starting audio-visual separation for {num_speakers} speakers...")
yield snapshot()
try:
print("Starting GPU-accelerated video processing...")
with GPU_PROGRESS_LOCK:
GPU_PROGRESS_STATE["progress"] = 0.0
GPU_PROGRESS_STATE["status"] = "Processing on GPU..."
progress(0.4, desc="Processing on GPU...")
status_value = "Processing on GPU..."
yield snapshot()
gpu_result = {"output_files": None, "exception": None}
def run_gpu_processing():
try:
gpu_result["output_files"] = process_video_gpu(
video_file=video_file,
temp_dir=temp_dir,
num_speakers=num_speakers
)
except Exception as exc:
gpu_result["exception"] = exc
gpu_thread = threading.Thread(target=run_gpu_processing, daemon=True)
gpu_thread.start()
last_reported_progress = 0.4
last_status_message = "Processing on GPU..."
while gpu_thread.is_alive():
time.sleep(0.5)
with GPU_PROGRESS_LOCK:
gpu_status = GPU_PROGRESS_STATE.get("status", "Processing on GPU...")
gpu_progress_value = GPU_PROGRESS_STATE.get("progress", 0.0)
mapped_progress = 0.4 + 0.5 * gpu_progress_value
mapped_progress = min(mapped_progress, 0.89)
if (
mapped_progress > last_reported_progress + 0.01
or gpu_status != last_status_message
):
progress(mapped_progress, desc=gpu_status)
last_reported_progress = mapped_progress
last_status_message = gpu_status
status_value = gpu_status
yield snapshot()
gpu_thread.join()
if gpu_result["exception"] is not None:
raise gpu_result["exception"]
output_files = gpu_result["output_files"]
progress(0.9, desc="Preparing results...")
status_value = "๐ฆ Preparing results..."
print("Processing completed successfully!")
print(f"Generated {num_speakers} output videos")
yield snapshot()
video_dict_value = {i: output_files[i] for i in range(num_speakers)}
video_dict_value['temp_dir'] = temp_dir
output_video_output = gr.update(value=output_files[0], visible=True)
progress(1.0, desc="Complete!")
status_value = f"โ
Successfully separated {num_speakers} speakers! Click on any face below to view their video."
yield snapshot()
except Exception as e:
print(f"Processing failed: {str(e)}")
import traceback
traceback.print_exc()
status_value = f"โ Processing failed: {str(e)}"
output_video_output = gr.update(visible=False)
video_dict_value = None
yield snapshot()
return
except Exception as e:
if temp_dir and os.path.exists(temp_dir):
try:
shutil.rmtree(temp_dir)
except Exception:
pass
print(f"Error: {str(e)}")
import traceback
traceback.print_exc()
status_value = f"โ Error: {str(e)}"
detected_info_output = gr.update(visible=False)
face_gallery_output = gr.update(visible=False)
output_video_output = gr.update(visible=False)
annotated_frame_output = gr.update(visible=False)
video_dict_value = None
yield snapshot()
return
finally:
sys.stdout = old_stdout
def on_face_click(evt: gr.SelectData, video_dict):
"""Handle face gallery click events"""
if video_dict is None or evt.index not in video_dict:
return None
return video_dict[evt.index]
# Create the Gradio interface
custom_css = """
.face-gallery {
border-radius: 10px;
overflow: hidden;
}
.face-gallery img {
border-radius: 8px;
transition: transform 0.2s ease-in-out;
}
.face-gallery img:hover {
transform: scale(1.05);
cursor: pointer;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}
.detected-info {
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
}
"""
with gr.Blocks(
title="Video Speaker Auto-Separation",
theme=gr.themes.Soft(),
css=custom_css
) as demo:
gr.Markdown(
"""
# ๐ฅ Dolphin: Efficient Audio-Visual Speech Separation with Discrete Lip Semantics and Hierarchical Top-Down Attention
<p align="left">
<img src="https://visitor-badge.laobi.icu/badge?page_id=JusperLee.Dolphin" alt="่ฎฟๅฎข็ป่ฎก" /><img src="https://img.shields.io/github/stars/JusperLee/Dolphin?style=social" alt="GitHub stars" /><img alt="Static Badge" src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" />
</p>
### Automatically detect and separate ALL speakers in your video
Simply upload a video and the system will:
1. ๐ Automatically detect all speakers in the video
2. ๐ญ Show you each detected speaker's face
3. ๐ฌ Generate individual videos for each speaker with their isolated audio
"""
)
with gr.Row():
with gr.Column(scale=2):
video_input = gr.Video(
label="๐น Upload Your Video",
height=300,
interactive=True
)
# Add example video section
gr.Markdown("### ๐ฌ Try with Example Video")
gr.Examples(
examples=[["demo1/mix.mp4"]],
inputs=video_input,
label="Click to load example video",
cache_examples=False
)
process_btn = gr.Button(
"๐ Auto-Detect and Process",
variant="primary",
size="lg"
)
status = gr.Textbox(
label="Status",
interactive=False,
value="โณ Ready to process..."
)
processing_log = gr.Textbox(
label="๐ Processing Details",
lines=10,
max_lines=15,
interactive=False,
value=""
)
with gr.Column(scale=3):
annotated_frame = gr.Image(
label="๐ธ Detected Speakers in First Frame",
visible=False,
height=300
)
detected_info = gr.Markdown(
visible=False,
elem_classes="detected-info"
)
gr.Markdown("### ๐ Click on any face below to view that speaker's video")
face_gallery = gr.Gallery(
label="Detected Speaker Faces",
show_label=False,
columns=5,
rows=1,
height=200,
visible=False,
object_fit="contain",
elem_classes="face-gallery"
)
output_video = gr.Video(
label="๐ฌ Selected Speaker's Video",
height=300,
visible=False,
autoplay=True
)
# Hidden state
video_dict = gr.State()
gr.Markdown(
"""
---
### ๐ How it works:
1. **Upload** - Select any video file
2. **Process** - Click the button to start automatic detection
3. **Review** - See all detected speakers and their positions
4. **Select** - Click on any face to watch that speaker's separated video
### ๐ก Tips for best results:
- โ
Ensure all speakers' faces are visible in the first frame
- โ
Use videos with good lighting and clear face views
- โ
Works best with frontal or near-frontal face angles
- โฑ๏ธ Processing time depends on video length and number of speakers
### ๐ Powered by:
- RetinaFace for face detection
- Dolphin model for audio-visual separation
- GPU acceleration when available
<footer style="display:none;">
<a href='https://clustrmaps.com/site/1c828' title='Visit tracker'>
<img src='//clustrmaps.com/map_v2.png?cl=080808&w=300&t=tt&d=XYmTC4S_SxuX7G06iJ16lU43VCNkCBFRLXMfEM5zvmo&co=ffffff&ct=808080'/>
</a>
</footer>
"""
)
# Event handlers
outputs_list = [
status,
detected_info,
face_gallery,
output_video,
video_dict,
annotated_frame,
processing_log
]
process_btn.click(
fn=process_video_auto,
inputs=[video_input],
outputs=outputs_list,
show_progress=True
)
face_gallery.select(
fn=on_face_click,
inputs=[video_dict],
outputs=output_video
)
# Launch the demo - HF Space will handle this automatically
if __name__ == "__main__":
import os
demo.launch(server_name="0.0.0.0", server_port=7860)
|