Spaces:
Runtime error
Runtime error
File size: 2,991 Bytes
4c16f3d | 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 | import cv2
import roop.globals
import ui.globals
import pyvirtualcam
import threading
import platform
cam_active = False
cam_thread = None
vcam = None
def virtualcamera(
swap_model, streamobs, use_xseg, use_mouthrestore, cam_num, width, height
):
from roop.ProcessOptions import ProcessOptions
from roop.core import live_swap, get_processing_plugins
global cam_active
# time.sleep(2)
print("Starting capture")
cap = cv2.VideoCapture(
cam_num,
cv2.CAP_DSHOW if platform.system() != "Darwin" else cv2.CAP_AVFOUNDATION,
)
if not cap.isOpened():
print("Cannot open camera")
cap.release()
del cap
return
pref_width = width
pref_height = height
pref_fps_in = 30
cap.set(cv2.CAP_PROP_FRAME_WIDTH, pref_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, pref_height)
cap.set(cv2.CAP_PROP_FPS, pref_fps_in)
cam_active = True
# native format UYVY
cam = None
if streamobs:
print("Detecting virtual cam devices")
cam = pyvirtualcam.Camera(
width=pref_width,
height=pref_height,
fps=pref_fps_in,
fmt=pyvirtualcam.PixelFormat.BGR,
print_fps=False,
)
if cam:
print(f"Using virtual camera: {cam.device}")
print(f"Using {cam.native_fmt}")
else:
print(f"Not streaming to virtual camera!")
subsample_size = roop.globals.subsample_size
options = ProcessOptions(
swap_model,
get_processing_plugins("mask_xseg" if use_xseg else None),
roop.globals.distance_threshold,
roop.globals.blend_ratio,
"all",
0,
None,
None,
1,
subsample_size,
False,
use_mouthrestore,
)
while cam_active:
ret, frame = cap.read()
if not ret:
break
if len(roop.globals.INPUT_FACESETS) > 0:
frame = live_swap(frame, options)
if cam:
cam.send(frame)
cam.sleep_until_next_frame()
ui.globals.ui_camera_frame = frame
if cam:
cam.close()
cap.release()
print("Camera stopped")
def start_virtual_cam(
swap_model, streamobs, use_xseg, use_mouthrestore, cam_number, resolution
):
global cam_thread, cam_active
if not cam_active:
width, height = map(int, resolution.split("x"))
cam_thread = threading.Thread(
target=virtualcamera,
args=[
swap_model,
streamobs,
use_xseg,
use_mouthrestore,
cam_number,
width,
height,
],
)
cam_thread.start()
def stop_virtual_cam():
global cam_active, cam_thread
if cam_active:
cam_active = False
cam_thread.join()
|