-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (138 loc) · 6.04 KB
/
app.py
File metadata and controls
169 lines (138 loc) · 6.04 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
import cv2
import pyvirtualcam
def check_virtual_camera_backends():
"""Check if virtual camera backends are available by attempting to create a test camera."""
print("Checking virtual camera backend availability...")
try:
# Try to create a minimal virtual camera to test if backends are available
with pyvirtualcam.Camera(width=640, height=480, fps=30) as test_cam:
print(f"Virtual camera backend working: {test_cam.device}")
return True
except RuntimeError as e:
print(f"Virtual camera backend not available: {e}")
print_installation_guide()
return False
except (OSError, ImportError) as e:
print(f"Error checking backends: {e}")
print_installation_guide()
return False
def print_installation_guide():
"""Print installation guide for virtual camera backends."""
print("\n" + "="*60)
print("VIRTUAL CAMERA SETUP REQUIRED")
print("="*60)
print("\nTo use this application, you need to install a virtual camera backend:")
print("\n🎯 RECOMMENDED - OBS Studio:")
print(" 1. Download and install OBS Studio from: https://obsproject.com/")
print(" 2. Start OBS Studio at least once")
print(" 3. The OBS Virtual Camera will be automatically available")
print(" ✅ Most reliable and widely supported option")
print("\n⚠️ Alternative - Unity Capture (Not Recommended):")
print(" 1. Download Unity Capture from: https://github.com/schellingb/UnityCapture")
print(" 2. Run the installer as administrator")
print(" 3. Register the virtual camera device")
print(" ⚠️ Note: Older software with limited compatibility")
print("\nAfter installation, restart this application.")
print("="*60)
def demo_mode():
"""Run in demo mode without virtual camera - shows processed video only."""
print("\n" + "="*50)
print("RUNNING IN DEMO MODE")
print("="*50)
print("Virtual camera not available, showing preview only.")
print("This demonstrates the image processing without virtual camera output.")
print("Press 'q' to exit.")
print("="*50)
# Open the physical webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
if not cap.isOpened():
print("Error: Could not open webcam for demo mode.")
return
try:
while True:
# Capture frame from webcam
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Apply the same processing as the main application
manipulated_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
manipulated_frame = cv2.cvtColor(
manipulated_frame, cv2.COLOR_GRAY2BGR)
# Display both original and processed frames
cv2.imshow('Original Feed', frame)
cv2.imshow('Processed Feed (Grayscale)', manipulated_frame)
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
print("Demo mode ended.")
def main():
print("WebcamWrapper - Virtual Camera Application")
print("-" * 40)
# Check if virtual camera backends are available
if not check_virtual_camera_backends():
response = input(
"\nWould you like to run in demo mode instead? (y/n): ").lower().strip()
if response == 'y' or response == 'yes':
demo_mode()
return
# Open the physical webcam (index 0 is usually the default webcam)
# Use DirectShow for better Windows compatibility
print("Initializing physical webcam...")
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
if not cap.isOpened():
print("Error: Could not open webcam.")
print("Please check that:")
print(" - Your webcam is connected and working")
print(" - No other applications are using the webcam")
print(" - Windows has permission to access the camera")
return
# Get webcam properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) or 30 # Default to 30 FPS if not detected
print(f"Physical webcam initialized: {width}x{height} @ {fps} FPS")
# Create virtual camera with enhanced error handling
try:
print("Creating virtual camera...")
with pyvirtualcam.Camera(width=width, height=height, fps=fps, fmt=pyvirtualcam.PixelFormat.BGR) as cam:
print(
f"Virtual camera started: {cam.device} ({width}x{height} @ {fps} FPS)")
print("Press 'q' in the preview window to exit")
while True:
# Capture frame from webcam
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Example manipulation: Convert frame to grayscale
manipulated_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert back to BGR for virtual camera (which expects 3 channels)
manipulated_frame = cv2.cvtColor(
manipulated_frame, cv2.COLOR_GRAY2BGR)
# Send manipulated frame to virtual camera
cam.send(manipulated_frame)
# Update the virtual camera (maintain FPS)
cam.sleep_until_next_frame()
# Optional: Display the manipulated frame for debugging
cv2.imshow('Virtual Camera Output', manipulated_frame)
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except RuntimeError as e:
print(f"\nError creating virtual camera: {e}")
print_installation_guide()
return
except (OSError, ImportError) as e:
print(f"\nUnexpected error: {e}")
return
finally:
# Clean up
cap.release()
cv2.destroyAllWindows()
print("Cleanup completed.")
if __name__ == "__main__":
main()