-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayonlyface.py
80 lines (60 loc) · 2.4 KB
/
displayonlyface.py
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
import cv2
import mediapipe as mp
import numpy as np
# Function to update parameters based on trackbar values
def update_parameters(value):
global feathering, smoothing
feathering = cv2.getTrackbarPos('Feathering', 'Settings')
smoothing = cv2.getTrackbarPos('Smoothing', 'Settings')
def main():
global feathering, smoothing
# Initialize MediaPipe Selfie Segmentation
mp_selfie_segmentation = mp.solutions.selfie_segmentation
selfie_segmentation = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)
# Initialize webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
# Create window for settings
cv2.namedWindow('Settings')
# Create trackbars for adjusting parameters
cv2.createTrackbar('Feathering', 'Settings', 20, 100, update_parameters)
cv2.createTrackbar('Smoothing', 'Settings', 82, 100, update_parameters)
# Set initial parameter values
feathering = 20
smoothing = 82
while True:
# Read frame from webcam
ret, frame = cap.read()
if not ret:
break
# Convert the BGR image to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Segment the frame into foreground and background
results = selfie_segmentation.process(frame_rgb)
# Convert segmentation mask to binary mask
mask = results.segmentation_mask
# Convert mask to data type uint8
mask = np.uint8(mask)
# Apply the binary mask to the frame to get the foreground
foreground = cv2.bitwise_and(frame, frame, mask=mask)
# Apply feathering to the mask
if feathering > 0:
kernel = cv2.getGaussianKernel(feathering * 2 + 1, feathering)
mask = cv2.filter2D(mask, -1, kernel)
# Smooth the mask
if smoothing > 0:
mask = cv2.GaussianBlur(mask, (smoothing * 2 + 1, smoothing * 2 + 1), 0)
# Apply the modified mask to the frame
foreground = cv2.bitwise_and(frame, frame, mask=mask)
# Display the resulting frame with only the foreground (person)
cv2.imshow('Selfie Segmentation', foreground)
# Exit on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all windows
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()