-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_camera.py
More file actions
216 lines (179 loc) · 6.82 KB
/
virtual_camera.py
File metadata and controls
216 lines (179 loc) · 6.82 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
#!/usr/bin/env python3
"""
Virtual Camera Output for WhatsApp Integration
Creates a virtual camera device that outputs the composited video
"""
import cv2
import numpy as np
import sys
import os
from typing import Optional, Tuple
from virtual_bg_pipeline import VirtualBackgroundEngine
# Try to import pyvirtualcam
try:
import pyvirtualcam
HAS_VIRTUALCAM = True
except ImportError:
HAS_VIRTUALCAM = False
print("Warning: pyvirtualcam not installed. Virtual camera will not be available.")
print("Install with: pip install pyvirtualcam")
try:
from PIL import Image
HAS_PIL = True
except ImportError:
HAS_PIL = False
class WhatsAppVirtualCamera:
"""
Virtual camera that outputs composited video to a virtual device
"""
def __init__(self, bg_path: Optional[str] = None, bg_3d_path: Optional[str] = None,
width: int = 1280, height: int = 720, fps: int = 30):
"""
Initialize virtual camera
Args:
bg_path: Path to background image/video
bg_3d_path: Path to 3D scene file
width: Output video width
height: Output video height
fps: Frames per second
"""
self.width = width
self.height = height
self.fps = fps
self.vcam = None
self.engine = None
self.cap = None
# Initialize virtual background engine
if bg_path or bg_3d_path:
self.engine = VirtualBackgroundEngine(bg_image_path=bg_path, bg_3d_path=bg_3d_path)
else:
self.engine = VirtualBackgroundEngine()
def start(self, webcam_index: int = 0):
"""
Start the virtual camera stream
Args:
webcam_index: Index of the physical webcam to capture from
"""
# Open physical webcam
self.cap = cv2.VideoCapture(webcam_index)
if not self.cap.isOpened():
raise RuntimeError(f"Could not open webcam at index {webcam_index}")
# Set capture properties
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
self.cap.set(cv2.CAP_PROP_FPS, self.fps)
print(f"Webcam opened: {self.width}x{self.height}@{self.fps}fps")
# Start virtual camera if available
if HAS_VIRTUALCAM:
try:
self.vcam = pyvirtualcam.Camera(width=self.width, height=self.height, fps=self.fps)
print(f"Virtual camera started: {self.vcam.device}")
except Exception as e:
print(f"Could not start virtual camera: {e}")
print("Virtual camera not available, but you can still preview")
# Run main loop
self._run_loop()
def _run_loop(self):
"""Main processing loop"""
print("Starting virtual camera loop...")
print("Press 'q' to quit, 's' to save screenshot")
print("Make sure to select the virtual camera in WhatsApp video call settings!")
while True:
ret, frame = self.cap.read()
if not ret:
print("Failed to capture frame from webcam")
break
# Resize frame to target resolution
frame = cv2.resize(frame, (self.width, self.height))
# Apply virtual background
composited = self.engine.apply(frame)
# Send to virtual camera
if self.vcam:
# pyvirtualcam expects RGB
rgb_frame = cv2.cvtColor(composited, cv2.COLOR_BGR2RGB)
self.vcam.send(rgb_frame)
self.vcam.sleep_until_next_frame()
# Also show preview window
cv2.imshow('WhatsApp Virtual Background - Preview', composited)
# Show small original for comparison
# cv2.imshow('Original (for reference)', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
filename = 'virtual_bg_capture.png'
cv2.imwrite(filename, composited)
print(f"Screenshot saved to {filename}")
self.stop()
def stop(self):
"""Clean up resources"""
if self.cap:
self.cap.release()
if self.vcam:
self.vcam.close()
if self.engine:
self.engine.release()
cv2.destroyAllWindows()
print("Virtual camera stopped")
def list_available_cameras():
"""List all available video capture devices"""
print("Detecting available cameras...")
available = []
for i in range(10): # Check first 10 indices
cap = cv2.VideoCapture(i)
if cap.isOpened():
ret, frame = cap.read()
if ret:
h, w = frame.shape[:2]
available.append((i, f"{w}x{h}"))
print(f" Camera {i}: {w}x{h}")
cap.release()
return available
def test_virtual_camera():
"""Test function to verify virtual camera setup"""
print("=== WhatsApp Virtual Background - Test ===")
print()
# List cameras
cameras = list_available_cameras()
if not cameras:
print("No cameras found! Please connect a webcam.")
return
print(f"\nFound {len(cameras)} camera(s)")
# Check virtual camera availability
if HAS_VIRTUALCAM:
print("Virtual camera support: YES")
else:
print("Virtual camera support: NO")
print(" Install pyvirtualcam for virtual camera output")
# Test with first camera
print("\nStarting test with camera 0...")
print("(Close the preview window or press 'q' to stop)")
try:
vcam = WhatsAppVirtualCamera(width=1280, height=720)
vcam.start(webcam_index=0)
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='WhatsApp Virtual Background')
parser.add_argument('--bg', type=str, help='Path to background image')
parser.add_argument('--bg-3d', type=str, help='Path to 3D scene file')
parser.add_argument('--camera', type=int, default=0, help='Webcam index (default: 0)')
parser.add_argument('--width', type=int, default=1280, help='Output width (default: 1280)')
parser.add_argument('--height', type=int, default=720, help='Output height (default: 720)')
parser.add_argument('--fps', type=int, default=30, help='FPS (default: 30)')
parser.add_argument('--test', action='store_true', help='Run test mode')
args = parser.parse_args()
if args.test:
test_virtual_camera()
else:
vcam = WhatsAppVirtualCamera(
bg_path=args.bg,
bg_3d_path=args.bg_3d,
width=args.width,
height=args.height,
fps=args.fps
)
vcam.start(webcam_index=args.camera)