-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_camera_face_only.py
More file actions
228 lines (190 loc) · 8.3 KB
/
Copy pathvirtual_camera_face_only.py
File metadata and controls
228 lines (190 loc) · 8.3 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
"""
BlurBerry Face-Only Virtual Camera Script
This script:
1. Runs camera with face enrollment
2. Starts virtual camera with face privacy protection only
3. Whitelists enrolled faces (your face stays clear)
4. Blurs all other faces
5. Streams to Zoom/Discord via OBS Virtual Camera
"""
import cv2
import numpy as np
import sys
import time
import os
from datetime import datetime
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from shared_types import PipelineConfig
from blurberry.video.virtual_video_loop import VirtualVideoLoop
from blurberry.video.face_pipeline import FacePipeline
# Virtual camera import
try:
import pyvirtualcam
VIRTUAL_CAMERA_AVAILABLE = True
print("✅ Virtual camera available")
except ImportError:
VIRTUAL_CAMERA_AVAILABLE = False
print("⚠️ Virtual camera not available, using local display")
FRAMES_NEEDED = 60 # collect 60 frames for enrollment
def face_only_virtual_camera():
"""Face-only virtual camera with privacy protection"""
print("\n" + "="*60)
print("🎭 BlurBerry AI - Face-Only Virtual Camera")
print("="*60)
print("\n🔒 Privacy Protection Features:")
print(" ✅ Face Recognition (your face stays clear)")
print(" ❌ Other Faces (auto-blurred)")
print("\n📋 Controls:")
print(" E - Enroll your face")
print(" S - Skip enrollment, start virtual camera")
print(" Q - Quit")
print("\n🎥 Output: OBS Virtual Camera (for Zoom/Discord)")
print("="*60)
# Configuration with face detection only
config = PipelineConfig(
blur_faces=True, # Enable face blur (whitelisted faces won't be blurred)
blur_plates=False, # Skip object detection
blur_cards=False, # Skip object detection
blur_nsfw=False, # Skip object detection
blur_text_pii=False, # Skip object detection
detection_cadence=15, # Not used for face-only
blur_strength=51, # Gaussian blur kernel size
face_similarity_threshold=0.38 # Face recognition threshold
)
# Initialize face pipeline
face_pipeline = FacePipeline(config)
# Setup camera for enrollment
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
collecting = False
embeddings_collected = []
enrolled = False
# Enrollment phase
while True:
ret, frame = cap.read()
if not ret:
break
display = frame.copy()
faces = face_pipeline.face_app.get(frame)
if collecting:
if faces:
face = max(faces, key=lambda f: (f.bbox[2]-f.bbox[0]) * (f.bbox[3]-f.bbox[1]))
embeddings_collected.append(face.embedding)
# Draw progress bar
progress = len(embeddings_collected) / FRAMES_NEEDED
bar_w = int(400 * progress)
cv2.rectangle(display, (120, 680), (520, 700), (50, 50, 50), -1)
cv2.rectangle(display, (120, 680), (120 + bar_w, 700), (0, 220, 100), -1)
cv2.putText(display, f"Enrolling: {len(embeddings_collected)}/{FRAMES_NEEDED}",
(120, 675), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 220, 100), 2)
# Draw box around face being enrolled
x1, y1, x2, y2 = [int(v) for v in face.bbox]
cv2.rectangle(display, (x1, y1), (x2, y2), (0, 220, 100), 2)
cv2.putText(display, "Enrolling...", (x1, y1 - 8),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 220, 100), 2)
else:
cv2.putText(display, "No face detected — look at camera",
(120, 450), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 100, 255), 2)
# Auto-finish when enough frames collected
if len(embeddings_collected) >= FRAMES_NEEDED:
collecting = False
face_pipeline.enroll_from_embeddings(embeddings_collected, name="owner")
enrolled = True
print(f"\n✅ [Enrollment] Done! Collected {len(embeddings_collected)} frames.")
print("🎯 Your face is now whitelisted and will stay clear!")
else:
# Draw face boxes while idle
for face in faces:
x1, y1, x2, y2 = [int(v) for v in face.bbox]
cv2.rectangle(display, (x1, y1), (x2, y2), (200, 200, 200), 1)
# Status overlay
status = ""
if enrolled:
status = "✅ Face enrolled! Press S to start virtual camera"
color = (0, 220, 100)
elif collecting:
status = "📸 Collecting frames..."
color = (0, 200, 255)
else:
status = "Press E to enroll your face | Press S to skip enrollment"
color = (200, 200, 200)
cv2.putText(display, status, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)
# Privacy features status
privacy_status = "🔒 Face Privacy: Your face clear, others blurred"
cv2.putText(display, privacy_status, (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 165, 0), 2)
# Virtual camera status
if VIRTUAL_CAMERA_AVAILABLE:
cv2.putText(display, "🎥 Virtual Camera Ready", (10, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Instructions
cv2.putText(display, "E: Enroll | S: Start Camera | Q: Quit", (10, 720-20),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.imshow("BlurBerry — Face-Only Virtual Camera", display)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
sys.exit(0)
elif key == ord('e') and not collecting:
embeddings_collected = []
collecting = True
print("\n📸 [Enrollment] Started collecting frames. Hold still and face the camera.")
print("🎯 Your face will be whitelisted and stay clear in virtual camera!")
elif key == ord('s'):
cap.release()
cv2.destroyAllWindows()
break
# ── Start Face-Only Virtual Camera Streaming ──
print("\n" + "="*60)
print("🚀 Starting Face-Only Virtual Camera...")
print("="*60)
if enrolled:
print("✅ Your enrolled face will NOT be blurred.")
print("🎯 All other faces WILL be blurred.")
else:
print("⚠️ No face enrolled. ALL detected faces will be blurred.")
print("\n🔒 Face Privacy Protection Active:")
print(" ✅ Your Face - Clear (if enrolled)")
print(" ❌ Other Faces - Blurred")
print("\n🎥 Virtual Camera: OBS Virtual Camera")
print("📱 Compatible: Zoom, Discord, Teams, Meet")
print("⏹️ Press Q to quit")
print("="*60)
# Create virtual video loop
loop = VirtualVideoLoop(config, source=0, debug=False)
# Pass the already-enrolled face pipeline into the loop
loop.face_pipeline = face_pipeline
# Note: No object detector added - face-only mode
print("🎭 Face detection loaded:")
print(" - Face recognition (your face clear)")
print(" - Face blurring (other faces blurred)")
try:
# Start virtual camera stream with face-only privacy
loop.run(use_virtual_camera=True)
except KeyboardInterrupt:
print("\n🛑 Stopped by user")
except Exception as e:
print(f"❌ Error: {e}")
print("💡 Make sure OBS Studio is installed for virtual camera")
finally:
loop.stop()
print("\n🔚 Virtual camera stream stopped")
print("🎯 Face privacy protection disabled")
print("="*60)
if __name__ == "__main__":
print("🎭 BlurBerry AI - Face-Only Privacy Virtual Camera")
print("="*60)
# Check virtual camera availability
if not VIRTUAL_CAMERA_AVAILABLE:
print("⚠️ Warning: Virtual camera not available")
print(" Install with: pip install pyvirtualcam")
print(" Or install OBS Studio for virtual camera driver")
response = input("Continue with local display? (y/n): ").strip().lower()
if response != 'y':
sys.exit(0)
# Run face-only virtual camera
face_only_virtual_camera()