34 lines
1.4 KiB
Python
34 lines
1.4 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
def roi(frame):
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
|
|
height, width = blurred.shape
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
faces = face_cascade.detectMultiScale(blurred, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
|
|
|
|
for (x, y, w, h) in faces:
|
|
c_x = int(x + w / 2)
|
|
c_y = int(y + h / 2)
|
|
|
|
c_x2 = int((int(c_x - w / 2 * 0.8) + int(c_x + w / 2 * 0.8)) / 2)
|
|
c_y2 = int((int(c_y - h / 2 * 1.4) + int(c_y + h / 2 * 1.1)) / 2)
|
|
|
|
center_ellipse = (c_x2, c_y2)
|
|
axes = (int(w / 2 * 1), int(h / 2 * 1.4)) # Major axis, Minor axis
|
|
angle = 0 # Rotation angle
|
|
color = (255, 255, 255) # Color in BGR format (white)
|
|
# thickness = 2
|
|
mask = np.zeros((height, width), dtype=np.uint8)
|
|
cv2.ellipse(mask, center_ellipse, axes, angle, 0, 360, 0, -1) # 255 for a white oval
|
|
# Apply the inverted mask to the face
|
|
result = cv2.bitwise_and(frame, frame, mask=mask)
|
|
|
|
black_mask = (result[:, :, 3] == 0)# & (result[:, :, 1] == 0) & (result[:, :, 2] == 0)
|
|
#print(result[c_x2, c_y2,1])
|
|
# Replace black pixels with white pixels
|
|
result[black_mask] = [result[0, 0, 0], result[0, 0, 1], result[0, 0, 2]]
|
|
|
|
return frame
|