-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smoothing.py
More file actions
129 lines (105 loc) · 3.82 KB
/
Copy pathtest_smoothing.py
File metadata and controls
129 lines (105 loc) · 3.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
#!/usr/bin/env python3
"""
Test silhouette smoothing with live comparison of presets.
Press 1-4 to switch presets, ESC to exit.
"""
import sys
import time
import cv2
import numpy as np
import pygame
import mediapipe as mp
from silhouette import SilhouetteProcessor, PRESETS, create_processor
def main():
print("=" * 50)
print("SILHOUETTE SMOOTHING TEST")
print("=" * 50)
print("\nControls:")
print(" 1 = Default preset")
print(" 2 = Responsive preset (less smoothing)")
print(" 3 = Smooth preset (more smoothing)")
print(" 4 = Raw preset (no smoothing)")
print(" ESC = Exit")
print()
# Initialize MediaPipe
mp_selfie = mp.solutions.selfie_segmentation
segmentation = mp_selfie.SelfieSegmentation(model_selection=1)
# Initialize processor with default preset
processor = create_processor("default")
current_preset = "default"
# Initialize webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("ERROR: Cannot open webcam")
return 1
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((320, 240))
pygame.display.set_caption("Silhouette Smoothing Test")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 24)
print("Running... (press ESC to exit)")
frame_times = []
running = True
while running:
frame_start = time.time()
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_1:
processor = create_processor("default")
current_preset = "default"
print("Switched to: default")
elif event.key == pygame.K_2:
processor = create_processor("responsive")
current_preset = "responsive"
print("Switched to: responsive")
elif event.key == pygame.K_3:
processor = create_processor("smooth")
current_preset = "smooth"
print("Switched to: smooth")
elif event.key == pygame.K_4:
processor = create_processor("raw")
current_preset = "raw"
print("Switched to: raw")
# Capture frame
ret, frame = cap.read()
if not ret:
continue
# Get segmentation mask
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = segmentation.process(frame_rgb)
# Process silhouette
silhouette = processor.create_silhouette_image(
results.segmentation_mask, 320, 240
)
# Convert to Pygame surface
silhouette = np.transpose(silhouette, (1, 0, 2))
silhouette = np.flip(silhouette, axis=0)
surface = pygame.surfarray.make_surface(silhouette)
# Draw
screen.blit(surface, (0, 0))
# Draw preset name and FPS
frame_time = time.time() - frame_start
frame_times.append(frame_time)
if len(frame_times) > 30:
frame_times.pop(0)
avg_fps = 1.0 / (sum(frame_times) / len(frame_times))
text = font.render(f"{current_preset} | {avg_fps:.0f} FPS", True, (128, 128, 128))
screen.blit(text, (5, 5))
pygame.display.flip()
clock.tick(30)
# Cleanup
cap.release()
segmentation.close()
pygame.quit()
print("\nDone!")
return 0
if __name__ == "__main__":
sys.exit(main())