Answer the question
In order to leave comments, you need to log in
How to make a full face face?
I'm trying to normalize the image, namely to make it look in front. I read the idea in an article that says that the user's face can be aligned to the eyes:
1) Coordinates of the midpoints of the left and right eyes (Left and Right):
XL = (X45 + X42) /2 ;
YL = (Y45 + Y42) /2;
XR = (X39 + X36) /2;
YR \u003d (Y39 + Y 36) / 2;
2) Beginning of UCS:
X0 =( XL + XR)/2;
Y0 =( YL + YR)/2;
Distances between the midpoints of the eyes along the X and Y axes:
DX = XR - XL
DY = YR - YL;
Actual distance L between the midpoints of the eyes (according to the Pythagorean theorem):
L = sqrt ( DX** 2 + DY**2)
UCS rotation angle trigonometric functions:
sin_AL = DY/L
cos_AL = DX/L
3) We pass from the coordinates in the window CS to the coordinates in the UCS, using the parameters X0,Y0, L, sin AL, cos AL:
X_User_0 = 2 (X_Window - X0 ) / L;
Y_User_0 = - 2 (Y_Window - Y0 ) / L ;
X_User = X_User_0 * cos_AL - Y_User_0 * sin_AL;
Y_User = X_User_0 * sin_AL + Y_User_0 * cos_AL;
COD
import math
import cv2
import dlib
def face(roi_gray,cos_AL,sin_AL,X_o,Y_o):
weight, height, RGB = roi_gray.shape
for x in range(weight):
for y in range(height):
X_USER_0 = 2 * (x - X_o) / L
Y_USER_0 = 2 * (y - Y_o) / L
X_NEW = int((X_USER_0) * cos_AL - Y_USER_0 * sin_AL)
Y_NEW = int(X_USER_0 * sin_AL + Y_USER_0 * cos_AL)
roi_gray[x][y] = roi_gray[X_NEW][Y_NEW]
cv2.imwrite('result.jpg', cv2.resize(roi_gray, (400, 400)))
# Для детектирования лиц используем каскады Хаара
Path = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(Path)
# Запуск видео потока
cap = cv2.VideoCapture(0)
# Модель для поиска ключевых точек
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
cos_AL,sin_AL, X_o,Y_o=0,0,0,0
while True:
# Получение изображения из видео потока
ret, frame = cap.read()
#Мы это сделали, т.к. если лица не будет , не сможем получить roi_gray, будет ошибка
if ret == 0:
break
# Конвертирование изображения в черно-белое
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Обнаружение лиц и построение прямоугольного контура
faces =faceCascade.detectMultiScale(grayFrame, minNeighbors = 10)
for x, y, width, height in faces:
cv2.rectangle(frame, (x, y), (x + width, y + height), color=(255, 255, 255), thickness=1)
face=dlib.rectangle(x, y , x+width, y+height)
landmarks = predictor(grayFrame, face)
#left_eys
x_r_36 = landmarks.part(36).x
y_r_36 = landmarks.part(36).y
x_r_39 = landmarks.part(39).x
y_r_39 = landmarks.part(39).y
X_r=(x_r_39 + x_r_36)/2
Y_r=(y_r_39 + y_r_36)/2
#rigth_eys
x_l_42 = landmarks.part(42).x
y_l_42 = landmarks.part(42).y
x_l_45 = landmarks.part(45).x
y_l_45 = landmarks.part(45).y
X_l = (x_l_42 + x_l_45) / 2
Y_l = (y_l_42 + y_l_45) / 2
#nachalo_psk
X_o=(X_l+X_r)/2
Y_o=(Y_l+Y_r)/2
#Расстояния между средними точками глаз вдоль осей Х и Y:
DX=X_r-X_l
DY=Y_r-Y_l
#Действительное расстояние L между средними точками глаз (по теореме Пифагора):
L=pow((DX**2)+(DY**2),0.5)
#Тригонометрические функции угла поворота ПСК
sin_AL=DY/L
cos_AL=DX/L
cv2.imshow("FACE IN IMAGE", frame)
if cv2.waitKey(10) & 0xFF == ord('e'): # <<<<< 0xFF
break
roi_gray = frame[y:y + height, x:x + width]
cv2.imwrite('result.jpg', cv2.resize(roi_gray, (400, 400)))
L=round(math.degrees(math.asin(sin_AL)))
rotation_matrix =cv2.getRotationMatrix2D((X_o,Y_o),L, 0.5)
anfas=cv2.warpAffine(roi_gray,rotation_matrix,(400,400))
cv2.imshow('ANFAS',anfas)
cv2.waitKey()
#ПОПЫТКА СДЕЛАТЬ ЛИЦО В АНФАС без cv2.getRotationMatrix2 и warpAffine
#face(roi_gray,cos_AL,sin_AL,X_o,Y_o)
cap.release()
cv2.destroyAllWindows()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question