File size: 1,646 Bytes
6bcb009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import roop.globals
import pyvirtualcam
import threading
import time


cam_active = False
cam_thread = None
vcam = None

def virtualcamera(cam_num):
    from roop.core import live_swap
    global cam_active

    time.sleep(2)
    print('Starting capture')
    cap = cv2.VideoCapture(cam_num, cv2.CAP_DSHOW)
    if not cap.isOpened():
        print("Cannot open camera")
        cap.release()
        del cap
        return

    pref_width = 1280
    pref_height = 720
    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)
    print('Starting VCAM')
    cam_active = True

    # native format UYVY

    with pyvirtualcam.Camera(width=pref_width, height=pref_height, fps=pref_fps_in, fmt=pyvirtualcam.PixelFormat.BGR, print_fps=True) as cam:

        print(f'Using virtual camera: {cam.device}')
        print(f'Using {cam.native_fmt}')

         # RGB

        while cam_active:
            ret, frame = cap.read()
            if not ret:
                break

            if len(roop.globals.INPUT_FACESETS) > 0:
                frame = live_swap(frame, "all", False, None)
                cam.send(frame)
            else:
                cam.send(frame)
                cam.sleep_until_next_frame()

    cap.release()
    print('End cam')



def start_virtual_cam(cam_number):
    global cam_thread, cam_active

    if not cam_active:
        cam_thread = threading.Thread(target=virtualcamera, args=[cam_number])
        cam_thread.start()



def stop_virtual_cam():
    global cam_active

    cam_active = False