-
Notifications
You must be signed in to change notification settings - Fork 1
/
mustache.py
147 lines (107 loc) · 5.99 KB
/
mustache.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
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
import cv2
import itertools
import numpy as np
from time import time
import mediapipe as mp
import matplotlib.pyplot as plt
import datetime
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh
face_mesh_images = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=2,
min_detection_confidence=0.5)
face_mesh_videos = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=2,
min_detection_confidence=0.5,min_tracking_confidence=0.3)
mp_drawing_styles = mp.solutions.drawing_styles
def detectFacialLandmarks(image, face_mesh, display = True):
results = face_mesh.process(image[:,:,::-1])
output_image = image[:,:,::-1].copy()
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(image=output_image, landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
mp_drawing.draw_landmarks(image=output_image, landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style())
if display:
plt.figure(figsize=[15,15])
plt.subplot(121);plt.imshow(image[:,:,::-1]);plt.title("Original Image");plt.axis('off');
plt.subplot(122);plt.imshow(output_image);plt.title("Output");plt.axis('off');
else:
return np.ascontiguousarray(output_image[:,:,::-1], dtype=np.uint8), results
def getSize(image, face_landmarks, INDEXES):
image_height, image_width, _ = image.shape
INDEXES_LIST = list(itertools.chain(*INDEXES))
landmarks = []
for INDEX in INDEXES_LIST:
landmarks.append([int(face_landmarks.landmark[INDEX].x * image_width),
int(face_landmarks.landmark[INDEX].y * image_height)])
_, _, width, height = cv2.boundingRect(np.array(landmarks))
landmarks = np.array(landmarks)
return width, height, landmarks
def overlay(image, filter_img, face_landmarks, face_part, INDEXES, display=True):
annotated_image = image.copy()
try:
filter_img_height, filter_img_width, _ = filter_img.shape
_, face_part_height, landmarks = getSize(image, face_landmarks, INDEXES)
required_height = int(face_part_height*3)
resized_filter_img = cv2.resize(filter_img, (int(filter_img_width*
(required_height/filter_img_height)),
required_height))
filter_img_height, filter_img_width, _ = resized_filter_img.shape
_, filter_img_mask = cv2.threshold(cv2.cvtColor(resized_filter_img, cv2.COLOR_BGR2GRAY),
1, 255, cv2.THRESH_BINARY_INV)
#filter_img_mask=image[:, :, 3]
center = landmarks.mean(axis=0).astype("int")
if face_part == 'MOUTH':
location = (int(center[0] - filter_img_width /2.3), int(center[1])-int(filter_img_height/1.5))
#print(filter_img_height,filter_img_width)
ROI = image[location[1]: location[1] + filter_img_height,
location[0]: location[0] + filter_img_width]
resultant_image = cv2.bitwise_and(ROI, ROI, mask=filter_img_mask)
resultant_image = cv2.bitwise_or(resultant_image, resized_filter_img)
annotated_image[location[1]: location[1] + filter_img_height,
location[0]: location[0] + filter_img_width] = resultant_image
except Exception as e:
pass
if display:
plt.figure(figsize=[10,10])
plt.imshow(annotated_image[:,:,::-1]);plt.title("Output Image");plt.axis('off');
else:
return annotated_image
def apply_mustache():
camera_video = cv2.VideoCapture(0)
camera_video.set(3,1280)
camera_video.set(4,960)
cv2.namedWindow('Mustache Filter', cv2.WINDOW_NORMAL)
mustache=cv2.imread('media\mustache1.png')
while camera_video.isOpened():
ok, frame = camera_video.read()
if not ok:
continue
#frame=cv2.imread('media\sample.jpg')
frame = cv2.flip(frame, 1)
_, face_mesh_results = detectFacialLandmarks(frame, face_mesh_videos, display=False)
if face_mesh_results.multi_face_landmarks:
for face_num, face_landmarks in enumerate(face_mesh_results.multi_face_landmarks):
frame = overlay(frame, mustache, face_landmarks,
'MOUTH', mp_face_mesh.FACEMESH_LIPS, display=False)
cv2.imshow('Moustache Filter', frame)
#cv2.waitKey(0) # Wait for any key press
#cv2.destroyAllWindows()
if cv2.waitKey(1) & 0xFF == ord('c'):
# Generate a unique filename based on the current timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"captured_image_{timestamp}.jpg"
# Save the frame as an image
cv2.imwrite(filename, frame)
print(f"Image captured and saved as {filename}")
k = cv2.waitKey(1) & 0xFF
if(k == 113):
break
camera_video.release()
cv2.destroyAllWindows()