Repository navigation
Expand file tree
/
Copy pathroop_api.py
More file actions
437 lines (374 loc) · 16.7 KB
/
Copy pathroop_api.py
File metadata and controls
437 lines (374 loc) · 16.7 KB
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
# roop_api.py - Complete API for roop-unleashed
import os
import cv2
import numpy as np
import torch
import threading
from typing import List, Optional, Union, Dict, Any, Callable, Tuple
from datetime import datetime
from pathlib import Path
import tempfile
import shutil
from PIL import Image
# Import from your actual roop module structure
from roop.core import live_swap, batch_process_regular, batch_process_with_options
from roop.ProcessOptions import ProcessOptions
from roop.ProcessMgr import ProcessMgr
from roop.face_util import extract_face_images
from roop.ProcessEntry import ProcessEntry
from roop.FaceSet import FaceSet
from roop.StreamWriter import StreamWriter
from roop.virtualcam import start_virtual_cam, stop_virtual_cam, cam_active
from roop.vr_util import get_perspective
from roop.utilities import (
replace_template, detect_fps, get_temp_directory_path,
get_temp_frame_paths, create_temp, clean_temp, delete_temp_frames,
has_image_extension, is_image, is_video, get_destfilename_from_path,
open_folder, compute_cosine_distance, clean_dir, shuffle_array,
sort_filenames_ignore_path, sort_rename_frames
)
from roop.util_ffmpeg import (
extract_frames, create_video, restore_audio, cut_video,
join_videos, create_gif_from_video, repair_video
)
import roop.globals
# Import from your actual clip module structure
try:
from clip.clip import load, available_models
from clip.simple_tokenizer import SimpleTokenizer
CLIP_AVAILABLE = True
except ImportError as e:
CLIP_AVAILABLE = False
print(f"CLIP modules not available: {e}")
try:
from clip.clipseg import CLIPDensePredT
CLIPSEG_AVAILABLE = True
except ImportError as e:
CLIPSEG_AVAILABLE = False
print(f"CLIPSeg modules not available: {e}")
class RoopUnleashedAPI:
def __init__(self):
self.initialized = False
self.process_mgr = None
self.stream_writer = None
self.processing_callback = None
self.virtual_cam_thread = None
self.virtual_cam_active = False
self.clip_model = None
self.clip_preprocess = None
self.clipseg_model = None
self.clip_tokenizer = None
def initialize(self,
execution_providers: List[str] = None,
max_threads: int = 2,
max_memory: int = 16,
output_path: str = "./output",
video_encoder: str = "libx264",
video_quality: int = 14,
log_level: str = "error",
output_image_format: str = "png",
output_video_format: str = "mp4",
face_distance_threshold: float = 0.65,
blend_ratio: float = 0.5,
subsample_size: int = 128,
enable_vr_mode: bool = False,
load_clip_models: bool = True,
load_clipseg_model: bool = True,
clip_model_name: str = "ViT-B/32") -> bool:
if execution_providers is None:
execution_providers = self._auto_detect_providers()
roop.globals.execution_providers = execution_providers
roop.globals.execution_threads = max_threads
roop.globals.max_memory = max_memory
roop.globals.video_encoder = video_encoder
roop.globals.video_quality = video_quality
roop.globals.output_path = output_path
roop.globals.log_level = log_level
roop.globals.distance_threshold = face_distance_threshold
roop.globals.blend_ratio = blend_ratio
roop.globals.subsample_size = subsample_size
roop.globals.vr_mode = enable_vr_mode
# Set output formats
if not hasattr(roop.globals, 'CFG'):
class Config:
pass
roop.globals.CFG = Config()
roop.globals.CFG.output_image_format = output_image_format
roop.globals.CFG.output_video_format = output_video_format
roop.globals.INPUT_FACESETS = []
roop.globals.TARGET_FACES = []
# Initialize face analysis
roop.globals.g_desired_face_analysis = [
"landmark_3d_68", "landmark_2d_106", "detection", "recognition"
]
# Create output directory
os.makedirs(output_path, exist_ok=True)
# Load CLIP models if requested
if load_clip_models and CLIP_AVAILABLE:
self._load_clip_models(clip_model_name)
# Load CLIPSeg model if requested
if load_clipseg_model and CLIPSEG_AVAILABLE:
self._load_clipseg_model()
self.initialized = True
print(f"✓ Roop Unleashed initialized with {execution_providers}")
return True
def _auto_detect_providers(self) -> List[str]:
try:
import onnxruntime
available = onnxruntime.get_available_providers()
if 'CUDAExecutionProvider' in available:
return ['CUDAExecutionProvider']
elif 'DmlExecutionProvider' in available:
return ['DmlExecutionProvider']
elif 'ROCMExecutionProvider' in available:
return ['ROCMExecutionProvider']
else:
return ['CPUExecutionProvider']
except:
return ['CPUExecutionProvider']
def _load_clip_models(self, model_name: str = "ViT-B/32"):
try:
print(f"Loading CLIP model: {model_name}")
self.clip_model, self.clip_preprocess = load(model_name, device="cpu")
self.clip_tokenizer = SimpleTokenizer()
print(f"✓ CLIP model {model_name} loaded successfully")
except Exception as e:
print(f"Failed to load CLIP models: {e}")
self.clip_model = None
self.clip_preprocess = None
self.clip_tokenizer = None
def _load_clipseg_model(self):
try:
print("Loading CLIPSeg model...")
self.clipseg_model = CLIPDensePredT(version='ViT-B/32', reduce_dim=64, complex_trans_conv=True)
self.clipseg_model.eval()
print("✓ CLIPSeg model loaded successfully")
except Exception as e:
print(f"Failed to load CLIPSeg model: {e}")
self.clipseg_model = None
def load_source_faces(self,
source_image_paths: Union[str, List[str]],
face_index: int = 0,
extra_padding: float = -1.0) -> int:
if isinstance(source_image_paths, str):
source_image_paths = [source_image_paths]
roop.globals.INPUT_FACESETS = []
loaded_count = 0
for source_path in source_image_paths:
if not os.path.exists(source_path):
print(f"Source image not found: {source_path}")
continue
faces_data = extract_face_images(source_path, (False, 0), extra_padding)
if faces_data and len(faces_data) > face_index:
face_set = FaceSet()
face_set.faces = [faces_data[face_index][0]]
face_set.ref_images = [faces_data[face_index][1]]
roop.globals.INPUT_FACESETS.append(face_set)
loaded_count += 1
print(f"✓ Loaded source face from {os.path.basename(source_path)}")
else:
print(f"No face found in {os.path.basename(source_path)}")
return loaded_count
def swap_single_image(self,
source_image_path: str,
target_image_path: str,
output_path: str = None,
swap_model: str = "inswapper_128_fp16",
enhancer: str = None,
swap_mode: str = "all",
face_restorer: str = None,
face_restorer_visibility: float = 1.0,
blending_ratio: float = 0.65,
**kwargs) -> bool:
if not self.initialized:
print("API not initialized. Call initialize() first.")
return False
if not os.path.exists(source_image_path):
print(f"Source image not found: {source_image_path}")
return False
if not os.path.exists(target_image_path):
print(f"Target image not found: {target_image_path}")
return False
# Load source faces
source_faces_loaded = self.load_source_faces([source_image_path])
if source_faces_loaded == 0:
print("No source faces found")
return False
# Generate output path if not provided
if output_path is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
target_filename = Path(target_image_path).stem
output_path = f"result_{target_filename}_{timestamp}.png"
output_path = os.path.join(roop.globals.output_path, output_path)
# Create ProcessOptions
options = ProcessOptions(
execution_providers=roop.globals.execution_providers,
execution_threads=roop.globals.execution_threads,
max_memory=roop.globals.max_memory,
face_swap_model=swap_model,
face_enhancer=enhancer,
face_restorer=face_restorer,
face_restorer_visibility=face_restorer_visibility,
blending_ratio=blending_ratio,
swap_mode=swap_mode,
vr_mode=roop.globals.vr_mode
)
try:
print(f"Processing image: {os.path.basename(target_image_path)}")
result = batch_process_regular(
target_path=target_image_path,
output_path=output_path,
process_options=options
)
if result and os.path.exists(output_path):
print(f"✓ Successfully created: {output_path}")
return True
else:
print(f"✗ Failed to process: {target_image_path}")
return False
except Exception as e:
print(f"Error during face swap: {e}")
return False
def batch_process_images(self,
source_image_path: str,
target_folder: str,
output_folder: str = None,
swap_model: str = "inswapper_128_fp16",
enhancer: str = None,
swap_mode: str = "all",
**kwargs) -> Dict[str, bool]:
if not self.initialized:
print("API not initialized. Call initialize() first.")
return {}
if not os.path.exists(source_image_path):
print(f"Source image not found: {source_image_path}")
return {}
if not os.path.exists(target_folder):
print(f"Target folder not found: {target_folder}")
return {}
# Load source faces
source_faces_loaded = self.load_source_faces([source_image_path])
if source_faces_loaded == 0:
print("No source faces found")
return {}
# Set output folder
if output_folder is None:
output_folder = os.path.join(roop.globals.output_path, "batch_output")
os.makedirs(output_folder, exist_ok=True)
# Get all image files
valid_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.webp')
image_files = []
for filename in os.listdir(target_folder):
if filename.lower().endswith(valid_extensions):
image_files.append(filename)
if not image_files:
print("No valid image files found in target folder")
return {}
results = {}
# Create ProcessOptions
options = ProcessOptions(
execution_providers=roop.globals.execution_providers,
execution_threads=roop.globals.execution_threads,
max_memory=roop.globals.max_memory,
face_swap_model=swap_model,
face_enhancer=enhancer,
swap_mode=swap_mode,
vr_mode=roop.globals.vr_mode
)
print(f"Processing {len(image_files)} images...")
for i, filename in enumerate(image_files):
target_path = os.path.join(target_folder, filename)
output_filename = f"swapped_{filename}"
output_path = os.path.join(output_folder, output_filename)
try:
result = batch_process_regular(
target_path=target_path,
output_path=output_path,
process_options=options
)
success = result and os.path.exists(output_path)
results[target_path] = success
if success:
print(f"✓ Processed {i+1}/{len(image_files)}: {filename}")
else:
print(f"✗ Failed {i+1}/{len(image_files)}: {filename}")
if self.processing_callback:
self.processing_callback((i+1, len(image_files)))
except Exception as e:
print(f"Error processing {filename}: {e}")
results[target_path] = False
success_count = sum(results.values())
print(f"✓ Batch processing complete: {success_count}/{len(image_files)} successful")
return results
def process_video(self,
source_image_path: str,
target_video_path: str,
output_path: str = None,
swap_model: str = "inswapper_128_fp16",
enhancer: str = None,
swap_mode: str = "all",
keep_frames: bool = False,
keep_audio: bool = True,
**kwargs) -> bool:
if not self.initialized:
print("API not initialized. Call initialize() first.")
return False
if not os.path.exists(source_image_path):
print(f"Source image not found: {source_image_path}")
return False
if not os.path.exists(target_video_path):
print(f"Target video not found: {target_video_path}")
return False
# Load source faces
source_faces_loaded = self.load_source_faces([source_image_path])
if source_faces_loaded == 0:
print("No source faces found")
return False
# Generate output path if not provided
if output_path is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
video_filename = Path(target_video_path).stem
output_path = f"video_{video_filename}_{timestamp}.mp4"
output_path = os.path.join(roop.globals.output_path, output_path)
# Create ProcessOptions
options = ProcessOptions(
execution_providers=roop.globals.execution_providers,
execution_threads=roop.globals.execution_threads,
max_memory=roop.globals.max_memory,
face_swap_model=swap_model,
face_enhancer=enhancer,
swap_mode=swap_mode,
vr_mode=roop.globals.vr_mode
)
try:
print(f"Processing video: {os.path.basename(target_video_path)}")
print("This may take a while depending on video length...")
result = batch_process_with_options(
target_path=target_video_path,
output_path=output_path,
process_options=options,
keep_frames=keep_frames,
keep_audio=keep_audio
)
if result and os.path.exists(output_path):
print(f"✓ Successfully created video: {output_path}")
return True
else:
print(f"✗ Failed to process video: {target_video_path}")
return False
except Exception as e:
print(f"Error during video processing: {e}")
return False
def set_processing_callback(self, callback: Callable):
self.processing_callback = callback
def cleanup(self):
if self.virtual_cam_active:
self.stop_virtual_camera()
if self.stream_writer:
self.stream_writer.Close()
self.clip_model = None
self.clip_preprocess = None
self.clip_tokenizer = None
self.clipseg_model = None
self.initialized = False
print("✓ Roop Unleashed cleaned up")