OpenCV MediaPipe實(shí)現(xiàn)顏值打分功能
顏值打分
定義可視化圖像函數(shù)
導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型
導(dǎo)入可視化函數(shù)和可視化樣式
將圖像模型輸入,獲取預(yù)測(cè)結(jié)果
BGR轉(zhuǎn)RGB
將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果
預(yù)測(cè)人人臉個(gè)數(shù)
獲取臉上關(guān)鍵點(diǎn)輪廓的坐標(biāo),并且將相應(yīng)的坐標(biāo)標(biāo)注出來(lái),在標(biāo)注點(diǎn)之間繪制連線(例如:左眼左眼角的識(shí)別點(diǎn)標(biāo)號(hào)為33號(hào))
# 顏值打分--五眼指標(biāo) import cv2 as cv import mediapipe as mp import numpy as np from tqdm import tqdm import time import matplotlib.pyplot as plt # 定義可視化圖像函數(shù) def look_img(img): img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB) plt.imshow(img_RGB) plt.show() # 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型 mp_face_mesh=mp.solutions.face_mesh # help(mp_face_mesh.FaceMesh) model=mp_face_mesh.FaceMesh( static_image_mode=True,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取 refine_landmarks=True,#使用Attention Mesh模型 max_num_faces=40, min_detection_confidence=0.5, #置信度閾值,越接近1越準(zhǔn) min_tracking_confidence=0.5,#追蹤閾值 ) # 導(dǎo)入可視化函數(shù)和可視化樣式 mp_drawing=mp.solutions.drawing_utils # mp_drawing_styles=mp.solutions.drawing_styles draw_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[66,77,229]) img=cv.imread("img.png") # 將圖像模型輸入,獲取預(yù)測(cè)結(jié)果 # BGR轉(zhuǎn)RGB img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB) scaler=1 h,w=img.shape[0],img.shape[1] # 將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果 results=model.process(img_RGB) # # 預(yù)測(cè)人人臉個(gè)數(shù) # len(results.multi_face_landmarks) # # print(len(results.multi_face_landmarks)) if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: mp_drawing.draw_landmarks( image=img, landmark_list=face_landmarks, connections=mp_face_mesh.FACEMESH_CONTOURS, landmark_drawing_spec=draw_spec, connection_drawing_spec=draw_spec ) for idx, coord in enumerate(face_landmarks.landmark): cx = int(coord.x * w) cy = int(coord.y * h) img = cv.putText(img, ' FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 0.1, (218, 112, 214), 1, 1) img = cv.putText(img, str(idx), (cx, cy), cv.FONT_HERSHEY_SIMPLEX, 0.3, (218, 112, 214), 1, 1) else: img = cv.putText(img, 'NO FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1.25, (218, 112, 214), 1, 8) look_img(img) cv.imwrite('face_id.jpg',img) # 連輪廓最左側(cè)點(diǎn) FL=results.multi_face_landmarks[0].landmark[234]; FL_X,FL_Y=int(FL.x*w),int(FL.y*h);FL_Color=(234,0,255) img=cv.circle(img,(FL_X,FL_Y),20,FL_Color,-1) look_img(img) # 臉上側(cè)邊緣 FT=results.multi_face_landmarks[0].landmark[10];# 10 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FT_X,FT_Y=int(FT.x*w),int(FT.y*h);FT_Color=(231,141,181) img=cv.circle(img,(FT_X,FT_Y),20,FT_Color,-1) look_img(img) # 下側(cè)邊緣 FB=results.multi_face_landmarks[0].landmark[152];# 152 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FB_X,FB_Y=int(FB.x*w),int(FB.y*h);FB_Color=(231,141,181) img=cv.circle(img,(FB_X,FB_Y),20,FB_Color,-1) look_img(img) # 右側(cè) FR=results.multi_face_landmarks[0].landmark[454];# 454 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FR_X,FR_Y=int(FR.x*w),int(FR.y*h);FR_Color=(0,255,0) img=cv.circle(img,(FR_X,FR_Y),20,FR_Color,-1) look_img(img) # 左眼左眼角 ELL=results.multi_face_landmarks[0].landmark[33];# 33坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELL_X,ELL_Y=int(ELL.x*w),int(ELL.y*h);ELL_Color=(0,255,0) img=cv.circle(img,(ELL_X,ELL_Y),20,ELL_Color,-1) look_img(img) #左眼右眼角 ELR=results.multi_face_landmarks[0].landmark[133];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELR_X,ELR_Y=int(ELR.x*w),int(ELR.y*h);ELR_Color=(0,255,0) img=cv.circle(img,(ELR_X,ELR_Y),20,ELR_Color,-1) look_img(img) # 右眼左眼角362 ERL=results.multi_face_landmarks[0].landmark[362];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERL_X,ERL_Y=int(ERL.x*w),int(ERL.y*h);ERL_Color=(233,255,128) img=cv.circle(img,(ERL_X,ERL_Y),20,ERL_Color,-1) look_img(img) # 右眼右眼角263 ERR=results.multi_face_landmarks[0].landmark[263];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERR_X,ERR_Y=int(ERR.x*w),int(ERR.y*h);ERR_Color=(23,255,128) img=cv.circle(img,(ERR_X,ERR_Y),20,ERR_Color,-1) look_img(img) # 從左往右六個(gè)點(diǎn)的橫坐標(biāo) Six_X=np.array([FL_X,ELL_X,ELR_X,ERL_X,ERR_X,FR_X]) # 從最左到最右的距離 Left_Right=FR_X-FL_X # 從左向右六個(gè)點(diǎn)的間隔的五個(gè)距離一并劃歸 Five_Distance=100*np.diff(Six_X)/Left_Right # 兩眼寬度的平均值 Eye_Width_Mean=np.mean((Five_Distance[1],Five_Distance[3])) # 五個(gè)距離分別與兩眼寬度均值的差 Five_Eye_Diff=Five_Distance-Eye_Width_Mean # 求L2范數(shù),作為顏值的指標(biāo) Five_Eye_Metrics=np.linalg.norm(Five_Eye_Diff) cv.line(img,(FL_X,FT_Y),(FL_X,FB_Y),FL_Color,3) cv.line(img,(ELL_X,FT_Y),(ELL_X,FB_Y),ELL_Color,3) cv.line(img,(ELR_X,FT_Y),(ELR_X,FB_Y),ELR_Color,3) cv.line(img,(ERL_X,FT_Y),(ERL_X,FB_Y),ERL_Color,3) cv.line(img,(ERR_X,FT_Y),(ERR_X,FB_Y),ERR_Color,3) cv.line(img,(FR_X,FT_Y),(FR_X,FB_Y),FR_Color,3) cv.line(img,(FL_X,FT_Y),(FR_X,FT_Y),FT_Color,3) cv.line(img,(FL_X,FB_Y),(FR_X,FB_Y),FB_Color,3) scaler=1 img = cv.putText(img, 'Five Eye Metrics{:.2f}'.format(Five_Eye_Metrics), (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 6, 6) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[0]), (25, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 5, 5) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[2]), (25, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 4, 4) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[4]), (25, 200), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 4) look_img(img) cv.imwrite("yanzhi.jpg",img)
攝像頭實(shí)時(shí)檢測(cè)顏值打分
最后一部分代碼是調(diào)用攝像頭的模板,可以直接使用
關(guān)鍵步驟在代碼注釋中有體現(xiàn)
import cv2 as cv import mediapipe as mp import numpy as np from tqdm import tqdm import time import matplotlib.pyplot as plt # 定義可視化圖像函數(shù) def look_img(img): img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB) plt.imshow(img_RGB) plt.show() # 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型 mp_face_mesh=mp.solutions.face_mesh # help(mp_face_mesh.FaceMesh) model=mp_face_mesh.FaceMesh( static_image_mode=False,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取 refine_landmarks=True,#使用Attention Mesh模型 max_num_faces=5, min_detection_confidence=0.5, #置信度閾值,越接近1越準(zhǔn) min_tracking_confidence=0.5,#追蹤閾值 ) # 導(dǎo)入可視化函數(shù)和可視化樣式 mp_drawing=mp.solutions.drawing_utils # mp_drawing_styles=mp.solutions.drawing_styles draw_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[66,77,229]) landmark_drawing_spec=mp_drawing.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229]) # 輪廓可視化 connection_drawing_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[233,155,6]) # 處理幀函數(shù) def process_frame(img): start_time = time.time() scaler = 1 h, w = img.shape[0], img.shape[1] img_RGB = cv.cvtColor(img, cv.COLOR_BGR2RGB) results = model.process(img_RGB) if results.multi_face_landmarks: # for face_landmarks in results.multi_face_landmarks: # 連輪廓最左側(cè)點(diǎn) FL = results.multi_face_landmarks[0].landmark[234]; FL_X, FL_Y = int(FL.x * w), int(FL.y * h); FL_Color = (234, 0, 255) img = cv.circle(img, (FL_X, FL_Y), 5, FL_Color, -1) look_img(img) # 臉上側(cè)邊緣 FT = results.multi_face_landmarks[0].landmark[10]; # 10 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FT_X, FT_Y = int(FT.x * w), int(FT.y * h); FT_Color = (231, 141, 181) img = cv.circle(img, (FT_X, FT_Y), 5, FT_Color, -1) look_img(img) # 下側(cè)邊緣 FB = results.multi_face_landmarks[0].landmark[152]; # 152 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FB_X, FB_Y = int(FB.x * w), int(FB.y * h); FB_Color = (231, 141, 181) img = cv.circle(img, (FB_X, FB_Y), 5, FB_Color, -1) look_img(img) # 右側(cè) FR = results.multi_face_landmarks[0].landmark[454]; # 454 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FR_X, FR_Y = int(FR.x * w), int(FR.y * h); FR_Color = (0, 255, 0) img = cv.circle(img, (FR_X, FR_Y), 5, FR_Color, -1) look_img(img) # 左眼左眼角 ELL = results.multi_face_landmarks[0].landmark[33]; # 33坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELL_X, ELL_Y = int(ELL.x * w), int(ELL.y * h); ELL_Color = (0, 255, 0) img = cv.circle(img, (ELL_X, ELL_Y), 5, ELL_Color, -1) look_img(img) # 左眼右眼角 ELR = results.multi_face_landmarks[0].landmark[133]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELR_X, ELR_Y = int(ELR.x * w), int(ELR.y * h); ELR_Color = (0, 255, 0) img = cv.circle(img, (ELR_X, ELR_Y), 5, ELR_Color, -1) look_img(img) # 右眼左眼角362 ERL = results.multi_face_landmarks[0].landmark[362]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERL_X, ERL_Y = int(ERL.x * w), int(ERL.y * h); ERL_Color = (233, 255, 128) img = cv.circle(img, (ERL_X, ERL_Y), 5, ERL_Color, -1) look_img(img) # 右眼右眼角263 ERR = results.multi_face_landmarks[0].landmark[263]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERR_X, ERR_Y = int(ERR.x * w), int(ERR.y * h); ERR_Color = (23, 255, 128) img = cv.circle(img, (ERR_X, ERR_Y), 5, ERR_Color, -1) look_img(img) # 從左往右六個(gè)點(diǎn)的橫坐標(biāo) Six_X = np.array([FL_X, ELL_X, ELR_X, ERL_X, ERR_X, FR_X]) # 從最左到最右的距離 Left_Right = FR_X - FL_X # 從左向右六個(gè)點(diǎn)的間隔的五個(gè)距離一并劃歸 Five_Distance = 100 * np.diff(Six_X) / Left_Right # 兩眼寬度的平均值 Eye_Width_Mean = np.mean((Five_Distance[1], Five_Distance[3])) # 五個(gè)距離分別與兩眼寬度均值的差 Five_Eye_Diff = Five_Distance - Eye_Width_Mean # 求L2范數(shù),作為顏值的指標(biāo) Five_Eye_Metrics = np.linalg.norm(Five_Eye_Diff) cv.line(img, (FL_X, FT_Y), (FL_X, FB_Y), FL_Color, 3) cv.line(img, (ELL_X, FT_Y), (ELL_X, FB_Y), ELL_Color, 3) cv.line(img, (ELR_X, FT_Y), (ELR_X, FB_Y), ELR_Color, 3) cv.line(img, (ERL_X, FT_Y), (ERL_X, FB_Y), ERL_Color, 3) cv.line(img, (ERR_X, FT_Y), (ERR_X, FB_Y), ERR_Color, 3) cv.line(img, (FR_X, FT_Y), (FR_X, FB_Y), FR_Color, 3) cv.line(img, (FL_X, FT_Y), (FR_X, FT_Y), FT_Color, 3) cv.line(img, (FL_X, FB_Y), (FR_X, FB_Y), FB_Color, 3) scaler = 1 img = cv.putText(img, 'Five Eye Metrics{:.2f}'.format(Five_Eye_Metrics), (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 2, 6) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[0]), (25, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 2, 5) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[2]), (25, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 2, 4) img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[4]), (25, 200), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 2, 4) else: img = cv.putText(img, 'NO FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1.25, (218, 112, 214), 1, 8) # 記錄該幀處理完畢的時(shí)間 end_time = time.time() # 計(jì)算每秒處理圖像的幀數(shù)FPS FPS = 1 / (end_time - start_time) scaler = 1 img = cv.putText(img, 'FPS' + str(int(FPS)), (25 * scaler, 300 * scaler), cv.FONT_HERSHEY_SIMPLEX, 1.25 * scaler, (0, 0, 255), 1, 8) return img # 調(diào)用攝像頭 cap=cv.VideoCapture(0) cap.open(0) # 無(wú)限循環(huán),直到break被觸發(fā) while cap.isOpened(): success,frame=cap.read() # if not success: # print('ERROR') # break frame=process_frame(frame) #展示處理后的三通道圖像 cv.imshow('my_window',frame) if cv.waitKey(1) &0xff==ord('q'): break cap.release() cv.destroyAllWindows()
好像也可以識(shí)別出哈士奇
達(dá)芬奇指標(biāo)
這里更加深化了上面的代碼,增加了更多的指標(biāo)
import cv2 as cv import mediapipe as mp import numpy as np from tqdm import tqdm import time import matplotlib.pyplot as plt # 定義可視化圖像函數(shù) def look_img(img): img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB) plt.imshow(img_RGB) plt.show() # 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型 mp_face_mesh=mp.solutions.face_mesh # help(mp_face_mesh.FaceMesh) model=mp_face_mesh.FaceMesh( static_image_mode=True,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取 refine_landmarks=True,#使用Attention Mesh模型 max_num_faces=40, min_detection_confidence=0.2, #置信度閾值,越接近1越準(zhǔn) min_tracking_confidence=0.5,#追蹤閾值 ) # 導(dǎo)入可視化函數(shù)和可視化樣式 mp_drawing=mp.solutions.drawing_utils # mp_drawing_styles=mp.solutions.drawing_styles draw_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[223,155,6]) # 讀取圖像 img=cv.imread("img.png") # width=img1.shape[1] # height=img1.shape[0] # img=cv.resize(img1,(width*10,height*10)) # look_img(img) # 將圖像模型輸入,獲取預(yù)測(cè)結(jié)果 # BGR轉(zhuǎn)RGB img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB) # 將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果 results=model.process(img_RGB) radius=12 lw=2 scaler=1 h,w=img.shape[0],img.shape[1] # 將RGB圖像輸入模型,獲取預(yù)測(cè)結(jié)果 # # 預(yù)測(cè)人人臉個(gè)數(shù) # len(results.multi_face_landmarks) # # print(len(results.multi_face_landmarks)) if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: mp_drawing.draw_landmarks( image=img, landmark_list=face_landmarks, connections=mp_face_mesh.FACEMESH_CONTOURS, landmark_drawing_spec=draw_spec, connection_drawing_spec=draw_spec ) for idx, coord in enumerate(face_landmarks.landmark): cx = int(coord.x * w) cy = int(coord.y * h) img = cv.putText(img, ' FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 0.1, (218, 112, 214), 1, 1) img = cv.putText(img, str(idx), (cx, cy), cv.FONT_HERSHEY_SIMPLEX, 0.3, (218, 112, 214), 1, 1) else: img = cv.putText(img, 'NO FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1.25, (218, 112, 214), 1, 8) look_img(img) # 連輪廓最左側(cè)點(diǎn) FL=results.multi_face_landmarks[0].landmark[234]; FL_X,FL_Y=int(FL.x*w),int(FL.y*h);FL_Color=(234,0,255) img=cv.circle(img,(FL_X,FL_Y),radius,FL_Color,-1) look_img(img) # 臉上側(cè)邊緣 FT=results.multi_face_landmarks[0].landmark[10];# 10 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FT_X,FT_Y=int(FT.x*w),int(FT.y*h);FT_Color=(231,141,181) img=cv.circle(img,(FT_X,FT_Y),radius,FT_Color,-1) look_img(img) # 下側(cè)邊緣 FB=results.multi_face_landmarks[0].landmark[152];# 152 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FB_X,FB_Y=int(FB.x*w),int(FB.y*h);FB_Color=(231,141,181) img=cv.circle(img,(FB_X,FB_Y),radius,FB_Color,-1) look_img(img) # 右側(cè) FR=results.multi_face_landmarks[0].landmark[454];# 454 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) FR_X,FR_Y=int(FR.x*w),int(FR.y*h);FR_Color=(0,255,0) img=cv.circle(img,(FR_X,FR_Y),radius,FR_Color,-1) look_img(img) # 左眼左眼角 ELL=results.multi_face_landmarks[0].landmark[33];# 33坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELL_X,ELL_Y=int(ELL.x*w),int(ELL.y*h);ELL_Color=(0,255,0) img=cv.circle(img,(ELL_X,ELL_Y),radius,ELL_Color,-1) look_img(img) #左眼右眼角 ELR=results.multi_face_landmarks[0].landmark[133];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELR_X,ELR_Y=int(ELR.x*w),int(ELR.y*h);ELR_Color=(0,255,0) img=cv.circle(img,(ELR_X,ELR_Y),radius,ELR_Color,-1) look_img(img) # 右眼左眼角362 ERL=results.multi_face_landmarks[0].landmark[362];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERL_X,ERL_Y=int(ERL.x*w),int(ERL.y*h);ERL_Color=(233,255,128) img=cv.circle(img,(ERL_X,ERL_Y),radius,ERL_Color,-1) look_img(img) # 右眼右眼角263 ERR=results.multi_face_landmarks[0].landmark[263];# 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERR_X,ERR_Y=int(ERR.x*w),int(ERR.y*h);ERR_Color=(23,255,128) img=cv.circle(img,(ERR_X,ERR_Y),radius,ERR_Color,-1) look_img(img) # 從左往右六個(gè)點(diǎn)的橫坐標(biāo) Six_X=np.array([FL_X,ELL_X,ELR_X,ERL_X,ERR_X,FR_X]) # 從最左到最右的距離 Left_Right=FR_X-FL_X # 從左向右六個(gè)點(diǎn)的間隔的五個(gè)距離一并劃歸 Five_Distance=100*np.diff(Six_X)/Left_Right # 兩眼寬度的平均值 Eye_Width_Mean=np.mean((Five_Distance[1],Five_Distance[3])) # 五個(gè)距離分別與兩眼寬度均值的差 Five_Eye_Diff=Five_Distance-Eye_Width_Mean # 求L2范數(shù),作為顏值的指標(biāo) Five_Eye_Metrics=np.linalg.norm(Five_Eye_Diff) # 三庭 # 眉心 MX=results.multi_face_landmarks[0].landmark[9];# 9 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) MX_X,MX_Y=int(MX.x*w),int(MX.y*h);MX_Color=(29,123,234) img=cv.circle(img,(MX_X,MX_Y),radius,MX_Color,-1) look_img(img) # 鼻翼下緣 2 NB=results.multi_face_landmarks[0].landmark[2];# 2 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) NB_X,NB_Y=int(NB.x*w),int(NB.y*h);NB_Color=(180,187,28) img=cv.circle(img,(NB_X,NB_Y),radius,NB_Color,-1) look_img(img) # 嘴唇中心 13 LC=results.multi_face_landmarks[0].landmark[13];# 17 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) LC_X,LC_Y=int(LC.x*w),int(LC.y*h);LC_Color=(0,0,258) img=cv.circle(img,(LC_X,LC_Y),radius,LC_Color,-1) look_img(img) # 嘴唇下緣 17 LB=results.multi_face_landmarks[0].landmark[17];# 17 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) LB_X,LB_Y=int(LB.x*w),int(LB.y*h);LB_Color=(139,0,0) img=cv.circle(img,(LB_X,LB_Y),radius,LB_Color,-1) look_img(img) Six_Y=np.array([FT_Y,MX_Y,NB_Y,LC_Y,LB_Y,FB_Y]) Top_Down=FB_Y-FT_Y Three_Section_Distance =100*np.diff(Six_Y)/Top_Down Three_Section_Mrtric_A=np.abs(Three_Section_Distance[1]-sum(Three_Section_Distance[2:])) # 鼻下到唇心距離 占第三庭的三分之一 Three_Section_Mrtric_B=np.abs(Three_Section_Distance[2]-sum(Three_Section_Distance[2:])/3) #唇心到下巴尖距離 占 第三庭的二分之一 Three_Section_Mrtric_C=np.abs(sum(Three_Section_Distance[3:])-sum(Three_Section_Distance[2:])/2) # 達(dá)芬奇 # 嘴唇左角 61 LL=results.multi_face_landmarks[0].landmark[61];# 61 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) LL_X,LL_Y=int(LL.x*w),int(LL.y*h);LL_Color=(255,255,255) img=cv.circle(img,(LL_X,LL_Y),radius,LL_Color,-1) look_img(img) # 嘴唇右角 291 LR=results.multi_face_landmarks[0].landmark[291];# 291 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) LR_X,LR_Y=int(LR.x*w),int(LR.y*h);LR_Color=(255,255,255) img=cv.circle(img,(LR_X,LR_Y),radius,LR_Color,-1) look_img(img) # 鼻子左緣 129 NL=results.multi_face_landmarks[0].landmark[129];# 291 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) NL_X,NL_Y=int(NL.x*w),int(NL.y*h);NL_Color=(255,255,255) img=cv.circle(img,(NL_X,NL_Y),radius,NL_Color,-1) look_img(img) # 鼻子右緣 358 NR=results.multi_face_landmarks[0].landmark[358];# 358 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) NR_X,NR_Y=int(NR.x*w),int(NR.y*h);NR_Color=(255,255,255) img=cv.circle(img,(NR_X,NR_Y),radius,NR_Color,-1) look_img(img) # 嘴寬為鼻寬的1.5/1.6倍 Da_Vinci=(LR.x-LL.x)/(NR.x-NL.x) # 眉毛 # 左眉毛左眉角 46 EBLL=results.multi_face_landmarks[0].landmark[46];# 46 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBLL_X,EBLL_Y=int(EBLL.x*w),int(EBLL.y*h);EBLL_Color=(255,355,155) img=cv.circle(img,(EBLL_X,EBLL_Y),radius,EBLL_Color,-1) look_img(img) # 左眉毛眉峰 105 EBLT=results.multi_face_landmarks[0].landmark[105];# 105 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBLT_X,EBLT_Y=int(EBLT.x*w),int(EBLT.y*h);EBLT_Color=(255,355,155) img=cv.circle(img,(EBLT_X,EBLT_Y),radius,EBLT_Color,-1) look_img(img) #左眉毛右角 107 EBLR=results.multi_face_landmarks[0].landmark[107];# 107 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBLR_X,EBLR_Y=int(EBLR.x*w),int(EBLR.y*h);EBLR_Color=(255,355,155) img=cv.circle(img,(EBLR_X,EBLR_Y),radius,EBLR_Color,-1) look_img(img) # 右眉毛左角 336 EBRL=results.multi_face_landmarks[0].landmark[336];# 336 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBRL_X,EBRL_Y=int(EBRL.x*w),int(EBRL.y*h);EBRL_Color=(295,355,105) img=cv.circle(img,(EBRL_X,EBRL_Y),radius,EBRL_Color,-1) look_img(img) # 右眉毛眉峰 334 EBRT=results.multi_face_landmarks[0].landmark[334];# 334 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBRT_X,EBRT_Y=int(EBRT.x*w),int(EBRT.y*h);EBRT_Color=( 355,155,155) img=cv.circle(img,(EBRT_X,EBRT_Y),radius,EBRT_Color,-1) look_img(img) # 右眉毛右角 276 EBRR=results.multi_face_landmarks[0].landmark[276];# 107 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) EBRR_X,EBRR_Y=int(EBRR.x*w),int(EBRR.y*h);EBRR_Color=(155,305,195) img=cv.circle(img,(EBRR_X,EBRR_Y),radius,EBRR_Color,-1) look_img(img) # 眉頭是否在眼角的正上方 EB_Metric_A=(EBLR_X-ELR_X)/Left_Right EB_Metric_B=(EBRL_X-ERL_X)/Left_Right EB_Metric_C=(EBLT_X-ELL_X)/Left_Right EB_Metric_D=(EBRT_X-ERR_X)/Left_Right EB_Metric_E=0.5*np.linalg.det([[EBLL_X,EBLL_Y,1],[ELL_X,ELL_Y,1],[NL_X,NL_Y,1]])/(Left_Right)**2 EB_Metric_F=0.5*np.linalg.det([[EBRR_X,EBRR_Y,1],[ERR_X,ERR_Y,1],[NR_X,NR_Y,1]])/(Left_Right)**2 cv.line(img,(EBLL_X,EBLL_Y),(ELL_X,ELL_Y),EBLL_Color,lw) cv.line(img,(ELL_X,ELL_Y),(NL_X,NL_Y),EBLL_Color,lw) cv.line(img,(EBLL_X,EBLL_Y),(NL_X,NL_Y),EBLL_Color,lw) cv.line(img,(EBRR_X,EBRR_Y),(ERR_X,ERR_Y),EBLL_Color,lw) cv.line(img,(EBRR_X,EBRR_Y),(NR_X,NR_Y),EBLL_Color,lw) cv.line(img,(EBRR_X,EBRR_Y),(NR_X,NR_Y),EBLL_Color,lw) look_img(img) #左內(nèi)眼角上點(diǎn) 157 ELRT=results.multi_face_landmarks[0].landmark[157];# 157 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELRT_X,ELRT_Y=int(ELRT.x*w),int(ELRT.y*h);ELRT_Color=(155,305,195) img=cv.circle(img,(ELRT_X,ELRT_Y),radius,ELRT_Color,-1) look_img(img) #左內(nèi)眼角下點(diǎn) 154 ELRB=results.multi_face_landmarks[0].landmark[154];# 154 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ELRB_X,ELRB_Y=int(ELRB.x*w),int(ELRB.y*h);ELRB_Color=(155,305,195) img=cv.circle(img,(ELRB_X,ELRB_Y),radius,ELRB_Color,-1) look_img(img) #右內(nèi)眼角上點(diǎn) 384 ERLT=results.multi_face_landmarks[0].landmark[384];# 384 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERLT_X,ERLT_Y=int(ERLT.x*w),int(ERLT.y*h);ERLT_Color=(155,305,195) img=cv.circle(img,(ERLT_X,ERLT_Y),radius,ERLT_Color,-1) look_img(img) # 右內(nèi)眼角下點(diǎn) 381 ERRB=results.multi_face_landmarks[0].landmark[381];# 384 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào) ERRB_X,ERRB_Y=int(ERRB.x*w),int(ERRB.y*h);ERRB_Color=(155,305,195) img=cv.circle(img,(ERRB_X,ERRB_Y),radius,ERRB_Color,-1) look_img(img) # 角度 vector_a=np.array([ELRT_X-ELR_X,ELRT_Y-ELR_Y]) vector_b=np.array([ELRB_X-ELR_X,ELRB_Y-ELR_Y]) cos=vector_a.dot(vector_b)/(np.linalg.norm(vector_a)*np.linalg.norm(vector_b)) EB_Metric_G=np.degrees(np.arccos(cos)) vector_a=np.array([ERLT_X-ERL_X,ERLT_Y-ERL_Y]) vector_b=np.array([ERRB_X-ERL_X,ERRB_Y-ERL_Y]) cos=vector_a.dot(vector_b)/(np.linalg.norm(vector_a)*np.linalg.norm(vector_b)) EB_Metric_H=np.degrees(np.arccos(cos)) # 可視化 cv.line(img,(FL_X,FT_Y),(FL_X,FB_Y),FL_Color,3) cv.line(img,(ELL_X,FT_Y),(ELL_X,FB_Y),ELL_Color,3) cv.line(img,(ELR_X,FT_Y),(ELR_X,FB_Y),ELR_Color,3) cv.line(img,(ERL_X,FT_Y),(ERL_X,FB_Y),ERL_Color,3) cv.line(img,(ERR_X,FT_Y),(ERR_X,FB_Y),ERR_Color,3) cv.line(img,(FR_X,FT_Y),(FR_X,FB_Y),FR_Color,3) cv.line(img,(FL_X,FT_Y),(FR_X,FT_Y),FT_Color,3) cv.line(img,(FL_X,FB_Y),(FR_X,FB_Y),FB_Color,3) cv.line(img,(FL_X,MX_Y),(FR_X,MX_Y),MX_Color,lw) cv.line(img,(FL_X,NB_Y),(FR_X,NB_Y),NB_Color,lw) cv.line(img,(FL_X,LC_Y),(FR_X,LC_Y),LC_Color,lw) cv.line(img,(FL_X,LB_Y),(FR_X,LB_Y),LB_Color,lw) scaler=1 img = cv.putText(img, 'Five Eye Metrics{:.2f}'.format(Five_Eye_Metrics), (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 3, 10) img = cv.putText(img, 'A{:.2f}'.format(Five_Eye_Diff[0]), (25, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 3, 10) img = cv.putText(img, 'B{:.2f}'.format(Five_Eye_Diff[2]), (25, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (218, 112, 214), 3, 10) img = cv.putText(img, 'C{:.2f}'.format(Five_Eye_Diff[4]), (25, 200), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 10) img = cv.putText(img, 'Three Scetion{:.2f}'.format(Three_Section_Mrtric_A), (25, 300), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 10) img = cv.putText(img, '1/3{:.2f}'.format(Three_Section_Mrtric_B), (25, 400), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 10) img = cv.putText(img, '1/2{:.2f}'.format(Three_Section_Mrtric_C), (25, 500), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 10) img = cv.putText(img, 'Da Vinci{:.2f}'.format(Da_Vinci), (25, 600), cv.FONT_HERSHEY_SIMPLEX,1, (218, 112, 214), 3, 10) look_img(img)
在這張圖上體現(xiàn)了更加細(xì)致的指標(biāo)
攝像頭實(shí)時(shí)達(dá)芬奇顏值指標(biāo)
與上面操作流程類似,可參考上面的流程?
import cv2 as cv
import mediapipe as mp
import numpy as np
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
# 定義可視化圖像函數(shù)
def look_img(img):
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# 導(dǎo)入三維人臉關(guān)鍵點(diǎn)檢測(cè)模型
mp_face_mesh=mp.solutions.face_mesh
# help(mp_face_mesh.FaceMesh)
model=mp_face_mesh.FaceMesh(
static_image_mode=False,#TRUE:靜態(tài)圖片/False:攝像頭實(shí)時(shí)讀取
refine_landmarks=True,#使用Attention Mesh模型
max_num_faces=5,
min_detection_confidence=0.5, #置信度閾值,越接近1越準(zhǔn)
min_tracking_confidence=0.5,#追蹤閾值
)
# 導(dǎo)入可視化函數(shù)和可視化樣式
mp_drawing=mp.solutions.drawing_utils
# mp_drawing_styles=mp.solutions.drawing_styles
draw_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[66,77,229])
landmark_drawing_spec=mp_drawing.DrawingSpec(thickness=1,circle_radius=2,color=[66,77,229])
# 輪廓可視化
connection_drawing_spec=mp_drawing.DrawingSpec(thickness=2,circle_radius=1,color=[233,155,6])
# 處理幀函數(shù)
def process_frame(img):
start_time = time.time()
scaler = 1
scaler = 1
radius = 12
lw = 2
scaler = 1
h, w = img.shape[0], img.shape[1]
img_RGB = cv.cvtColor(img, cv.COLOR_BGR2RGB)
results = model.process(img_RGB)
if results.multi_face_landmarks:
# for face_landmarks in results.multi_face_landmarks:
# 連輪廓最左側(cè)點(diǎn)
FL = results.multi_face_landmarks[0].landmark[234];
FL_X, FL_Y = int(FL.x * w), int(FL.y * h);
FL_Color = (234, 0, 255)
img = cv.circle(img, (FL_X, FL_Y), 5, FL_Color, -1)
look_img(img)
# 臉上側(cè)邊緣
FT = results.multi_face_landmarks[0].landmark[10]; # 10 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
FT_X, FT_Y = int(FT.x * w), int(FT.y * h);
FT_Color = (231, 141, 181)
img = cv.circle(img, (FT_X, FT_Y), 5, FT_Color, -1)
look_img(img)
# 下側(cè)邊緣
FB = results.multi_face_landmarks[0].landmark[152]; # 152 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
FB_X, FB_Y = int(FB.x * w), int(FB.y * h);
FB_Color = (231, 141, 181)
img = cv.circle(img, (FB_X, FB_Y), 5, FB_Color, -1)
look_img(img)
# 右側(cè)
FR = results.multi_face_landmarks[0].landmark[454]; # 454 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
FR_X, FR_Y = int(FR.x * w), int(FR.y * h);
FR_Color = (0, 255, 0)
img = cv.circle(img, (FR_X, FR_Y), 5, FR_Color, -1)
look_img(img)
# 左眼左眼角
ELL = results.multi_face_landmarks[0].landmark[33]; # 33坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ELL_X, ELL_Y = int(ELL.x * w), int(ELL.y * h);
ELL_Color = (0, 255, 0)
img = cv.circle(img, (ELL_X, ELL_Y), 5, ELL_Color, -1)
look_img(img)
# 左眼右眼角
ELR = results.multi_face_landmarks[0].landmark[133]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ELR_X, ELR_Y = int(ELR.x * w), int(ELR.y * h);
ELR_Color = (0, 255, 0)
img = cv.circle(img, (ELR_X, ELR_Y), 5, ELR_Color, -1)
look_img(img)
# 右眼左眼角362
ERL = results.multi_face_landmarks[0].landmark[362]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ERL_X, ERL_Y = int(ERL.x * w), int(ERL.y * h);
ERL_Color = (233, 255, 128)
img = cv.circle(img, (ERL_X, ERL_Y), 5, ERL_Color, -1)
look_img(img)
# 右眼右眼角263
ERR = results.multi_face_landmarks[0].landmark[263]; # 133坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ERR_X, ERR_Y = int(ERR.x * w), int(ERR.y * h);
ERR_Color = (23, 255, 128)
img = cv.circle(img, (ERR_X, ERR_Y), 5, ERR_Color, -1)
look_img(img)
# 從左往右六個(gè)點(diǎn)的橫坐標(biāo)
Six_X = np.array([FL_X, ELL_X, ELR_X, ERL_X, ERR_X, FR_X])
# 從最左到最右的距離
Left_Right = FR_X - FL_X
# 從左向右六個(gè)點(diǎn)的間隔的五個(gè)距離一并劃歸
Five_Distance = 100 * np.diff(Six_X) / Left_Right
# 兩眼寬度的平均值
Eye_Width_Mean = np.mean((Five_Distance[1], Five_Distance[3]))
# 五個(gè)距離分別與兩眼寬度均值的差
Five_Eye_Diff = Five_Distance - Eye_Width_Mean
# 求L2范數(shù),作為顏值的指標(biāo)
Five_Eye_Metrics = np.linalg.norm(Five_Eye_Diff)
cv.line(img, (FL_X, FT_Y), (FL_X, FB_Y), FL_Color, 3)
cv.line(img, (ELL_X, FT_Y), (ELL_X, FB_Y), ELL_Color, 3)
cv.line(img, (ELR_X, FT_Y), (ELR_X, FB_Y), ELR_Color, 3)
cv.line(img, (ERL_X, FT_Y), (ERL_X, FB_Y), ERL_Color, 3)
cv.line(img, (ERR_X, FT_Y), (ERR_X, FB_Y), ERR_Color, 3)
cv.line(img, (FR_X, FT_Y), (FR_X, FB_Y), FR_Color, 3)
cv.line(img, (FL_X, FT_Y), (FR_X, FT_Y), FT_Color, 3)
cv.line(img, (FL_X, FB_Y), (FR_X, FB_Y), FB_Color, 3)
# 三庭
# 眉心
MX = results.multi_face_landmarks[0].landmark[9]; # 9 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
MX_X, MX_Y = int(MX.x * w), int(MX.y * h);
MX_Color = (29, 123, 234)
img = cv.circle(img, (MX_X, MX_Y), radius, MX_Color, -1)
look_img(img)
# 鼻翼下緣 2
NB = results.multi_face_landmarks[0].landmark[2]; # 2 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
NB_X, NB_Y = int(NB.x * w), int(NB.y * h);
NB_Color = (180, 187, 28)
img = cv.circle(img, (NB_X, NB_Y), radius, NB_Color, -1)
look_img(img)
# 嘴唇中心 13
LC = results.multi_face_landmarks[0].landmark[13]; # 17 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
LC_X, LC_Y = int(LC.x * w), int(LC.y * h);
LC_Color = (0, 0, 258)
img = cv.circle(img, (LC_X, LC_Y), radius, LC_Color, -1)
look_img(img)
# 嘴唇下緣 17
LB = results.multi_face_landmarks[0].landmark[17]; # 17 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
LB_X, LB_Y = int(LB.x * w), int(LB.y * h);
LB_Color = (139, 0, 0)
img = cv.circle(img, (LB_X, LB_Y), radius, LB_Color, -1)
look_img(img)
Six_Y = np.array([FT_Y, MX_Y, NB_Y, LC_Y, LB_Y, FB_Y])
Top_Down = FB_Y - FT_Y
Three_Section_Distance = 100 * np.diff(Six_Y) / Top_Down
Three_Section_Mrtric_A = np.abs(Three_Section_Distance[1] - sum(Three_Section_Distance[2:]))
# 鼻下到唇心距離 占第三庭的三分之一
Three_Section_Mrtric_B = np.abs(Three_Section_Distance[2] - sum(Three_Section_Distance[2:]) / 3)
# 唇心到下巴尖距離 占 第三庭的二分之一
Three_Section_Mrtric_C = np.abs(sum(Three_Section_Distance[3:]) - sum(Three_Section_Distance[2:]) / 2)
# 達(dá)芬奇
# 嘴唇左角 61
LL = results.multi_face_landmarks[0].landmark[61]; # 61 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
LL_X, LL_Y = int(LL.x * w), int(LL.y * h);
LL_Color = (255, 255, 255)
img = cv.circle(img, (LL_X, LL_Y), radius, LL_Color, -1)
look_img(img)
# 嘴唇右角 291
LR = results.multi_face_landmarks[0].landmark[291]; # 291 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
LR_X, LR_Y = int(LR.x * w), int(LR.y * h);
LR_Color = (255, 255, 255)
img = cv.circle(img, (LR_X, LR_Y), radius, LR_Color, -1)
look_img(img)
# 鼻子左緣 129
NL = results.multi_face_landmarks[0].landmark[129]; # 291 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
NL_X, NL_Y = int(NL.x * w), int(NL.y * h);
NL_Color = (255, 255, 255)
img = cv.circle(img, (NL_X, NL_Y), radius, NL_Color, -1)
look_img(img)
# 鼻子右緣 358
NR = results.multi_face_landmarks[0].landmark[358]; # 358 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
NR_X, NR_Y = int(NR.x * w), int(NR.y * h);
NR_Color = (255, 255, 255)
img = cv.circle(img, (NR_X, NR_Y), radius, NR_Color, -1)
look_img(img)
# 嘴寬為鼻寬的1.5/1.6倍
Da_Vinci = (LR.x - LL.x) / (NR.x - NL.x)
# 眉毛
# 左眉毛左眉角 46
EBLL = results.multi_face_landmarks[0].landmark[46]; # 46 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBLL_X, EBLL_Y = int(EBLL.x * w), int(EBLL.y * h);
EBLL_Color = (255, 355, 155)
img = cv.circle(img, (EBLL_X, EBLL_Y), radius, EBLL_Color, -1)
look_img(img)
# 左眉毛眉峰 105
EBLT = results.multi_face_landmarks[0].landmark[105]; # 105 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBLT_X, EBLT_Y = int(EBLT.x * w), int(EBLT.y * h);
EBLT_Color = (255, 355, 155)
img = cv.circle(img, (EBLT_X, EBLT_Y), radius, EBLT_Color, -1)
look_img(img)
# 左眉毛右角 107
EBLR = results.multi_face_landmarks[0].landmark[107]; # 107 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBLR_X, EBLR_Y = int(EBLR.x * w), int(EBLR.y * h);
EBLR_Color = (255, 355, 155)
img = cv.circle(img, (EBLR_X, EBLR_Y), radius, EBLR_Color, -1)
look_img(img)
# 右眉毛左角 336
EBRL = results.multi_face_landmarks[0].landmark[336]; # 336 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBRL_X, EBRL_Y = int(EBRL.x * w), int(EBRL.y * h);
EBRL_Color = (295, 355, 105)
img = cv.circle(img, (EBRL_X, EBRL_Y), radius, EBRL_Color, -1)
look_img(img)
# 右眉毛眉峰 334
EBRT = results.multi_face_landmarks[0].landmark[334]; # 334 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBRT_X, EBRT_Y = int(EBRT.x * w), int(EBRT.y * h);
EBRT_Color = (355, 155, 155)
img = cv.circle(img, (EBRT_X, EBRT_Y), radius, EBRT_Color, -1)
look_img(img)
# 右眉毛右角 276
EBRR = results.multi_face_landmarks[0].landmark[276]; # 107 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
EBRR_X, EBRR_Y = int(EBRR.x * w), int(EBRR.y * h);
EBRR_Color = (155, 305, 195)
img = cv.circle(img, (EBRR_X, EBRR_Y), radius, EBRR_Color, -1)
look_img(img)
# 眉頭是否在眼角的正上方
EB_Metric_A = (EBLR_X - ELR_X) / Left_Right
EB_Metric_B = (EBRL_X - ERL_X) / Left_Right
EB_Metric_C = (EBLT_X - ELL_X) / Left_Right
EB_Metric_D = (EBRT_X - ERR_X) / Left_Right
EB_Metric_E = 0.5 * np.linalg.det([[EBLL_X, EBLL_Y, 1], [ELL_X, ELL_Y, 1], [NL_X, NL_Y, 1]]) / (Left_Right) ** 2
EB_Metric_F = 0.5 * np.linalg.det([[EBRR_X, EBRR_Y, 1], [ERR_X, ERR_Y, 1], [NR_X, NR_Y, 1]]) / (Left_Right) ** 2
cv.line(img, (EBLL_X, EBLL_Y), (ELL_X, ELL_Y), EBLL_Color, lw)
cv.line(img, (ELL_X, ELL_Y), (NL_X, NL_Y), EBLL_Color, lw)
cv.line(img, (EBLL_X, EBLL_Y), (NL_X, NL_Y), EBLL_Color, lw)
cv.line(img, (EBRR_X, EBRR_Y), (ERR_X, ERR_Y), EBLL_Color, lw)
cv.line(img, (EBRR_X, EBRR_Y), (NR_X, NR_Y), EBLL_Color, lw)
cv.line(img, (EBRR_X, EBRR_Y), (NR_X, NR_Y), EBLL_Color, lw)
look_img(img)
# 左內(nèi)眼角上點(diǎn) 157
ELRT = results.multi_face_landmarks[0].landmark[157]; # 157 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ELRT_X, ELRT_Y = int(ELRT.x * w), int(ELRT.y * h);
ELRT_Color = (155, 305, 195)
img = cv.circle(img, (ELRT_X, ELRT_Y), radius, ELRT_Color, -1)
look_img(img)
# 左內(nèi)眼角下點(diǎn) 154
ELRB = results.multi_face_landmarks[0].landmark[154]; # 154 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ELRB_X, ELRB_Y = int(ELRB.x * w), int(ELRB.y * h);
ELRB_Color = (155, 305, 195)
img = cv.circle(img, (ELRB_X, ELRB_Y), radius, ELRB_Color, -1)
look_img(img)
# 右內(nèi)眼角上點(diǎn) 384
ERLT = results.multi_face_landmarks[0].landmark[384]; # 384 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ERLT_X, ERLT_Y = int(ERLT.x * w), int(ERLT.y * h);
ERLT_Color = (155, 305, 195)
img = cv.circle(img, (ERLT_X, ERLT_Y), radius, ERLT_Color, -1)
look_img(img)
# 右內(nèi)眼角下點(diǎn) 381
ERRB = results.multi_face_landmarks[0].landmark[381]; # 384 坐標(biāo)為上圖中標(biāo)注的點(diǎn)的序號(hào)
ERRB_X, ERRB_Y = int(ERRB.x * w), int(ERRB.y * h);
ERRB_Color = (155, 305, 195)
img = cv.circle(img, (ERRB_X, ERRB_Y), radius, ERRB_Color, -1)
look_img(img)
# 角度
vector_a = np.array([ELRT_X - ELR_X, ELRT_Y - ELR_Y])
vector_b = np.array([ELRB_X - ELR_X, ELRB_Y - ELR_Y])
cos = vector_a.dot(vector_b) / (np.linalg.norm(vector_a) * np.linalg.norm(vector_b))
EB_Metric_G = np.degrees(np.arccos(cos))
vector_a = np.array([ERLT_X - ERL_X, ERLT_Y - ERL_Y])
vector_b = np.array([ERRB_X - ERL_X, ERRB_Y - ERL_Y])
cos = vector_a.dot(vector_b) / (np.linalg.norm(vector_a) * np.linalg.norm(vector_b))
EB_Metric_H = np.degrees(np.arccos(cos))
# 可視化
cv.line(img, (FL_X, FT_Y), (FL_X, FB_Y), FL_Color, 3)
cv.line(img, (ELL_X, FT_Y), (ELL_X, FB_Y), ELL_Color, 3)
cv.line(img, (ELR_X, FT_Y), (ELR_X, FB_Y), ELR_Color, 3)
cv.line(img, (ERL_X, FT_Y), (ERL_X, FB_Y), ERL_Color, 3)
cv.line(img, (ERR_X, FT_Y), (ERR_X, FB_Y), ERR_Color, 3)
cv.line(img, (FR_X, FT_Y), (FR_X, FB_Y), FR_Color, 3)
cv.line(img, (FL_X, FT_Y), (FR_X, FT_Y), FT_Color, 3)
cv.line(img, (FL_X, FB_Y), (FR_X, FB_Y), FB_Color, 3)
cv.line(img, (FL_X, MX_Y), (FR_X, MX_Y), MX_Color, lw)
cv.line(img, (FL_X, NB_Y), (FR_X, NB_Y), NB_Color, lw)
cv.line(img, (FL_X, LC_Y), (FR_X, LC_Y), LC_Color, lw)
cv.line(img, (FL_X, LB_Y), (FR_X, LB_Y), LB_Color, lw)
img = cv.putText(img, 'Five Eye Metrics{:.2f}'.format(Five_Eye_Metrics), (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, 'A{:.2f}'.format(Five_Eye_Diff[0]), (25, 100), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, 'B{:.2f}'.format(Five_Eye_Diff[2]), (25, 150), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, 'C{:.2f}'.format(Five_Eye_Diff[4]), (25, 200), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, 'Three Scetion{:.2f}'.format(Three_Section_Mrtric_A), (25, 300), cv.FONT_HERSHEY_SIMPLEX,
1,
(218, 112, 214), 3, 10)
img = cv.putText(img, '1/3{:.2f}'.format(Three_Section_Mrtric_B), (25, 400), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, '1/2{:.2f}'.format(Three_Section_Mrtric_C), (25, 500), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
img = cv.putText(img, 'Da Vinci{:.2f}'.format(Da_Vinci), (25, 600), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 3, 10)
look_img(img)
img = cv.putText(img, 'Five Eye Metrics{:.2f}'.format(Five_Eye_Metrics), (25, 50), cv.FONT_HERSHEY_SIMPLEX,
1,
(218, 112, 214), 2, 6)
img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[0]), (25, 100), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 2, 5)
img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[2]), (25, 150), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 2, 4)
img = cv.putText(img, 'Distance 1{:.2f}'.format(Five_Eye_Diff[4]), (25, 200), cv.FONT_HERSHEY_SIMPLEX, 1,
(218, 112, 214), 2, 4)
else:
img = cv.putText(img, 'NO FACE DELECTED', (25, 50), cv.FONT_HERSHEY_SIMPLEX, 1.25,
(218, 112, 214), 1, 8)
# 記錄該幀處理完畢的時(shí)間
end_time = time.time()
# 計(jì)算每秒處理圖像的幀數(shù)FPS
FPS = 1 / (end_time - start_time)
scaler = 1
img = cv.putText(img, 'FPS' + str(int(FPS)), (25 * scaler, 700 * scaler), cv.FONT_HERSHEY_SIMPLEX,
1.25 * scaler, (0, 0, 255), 1, 8)
return img
# 調(diào)用攝像頭
cap=cv.VideoCapture(0)
cap.open(0)
# 無(wú)限循環(huán),直到break被觸發(fā)
while cap.isOpened():
success,frame=cap.read()
# if not success:
# print('ERROR')
# break
frame=process_frame(frame)
#展示處理后的三通道圖像
cv.imshow('my_window',frame)
if cv.waitKey(1) &0xff==ord('q'):
break
cap.release()
cv.destroyAllWindows()
以上就是OpenCV MediaPipe實(shí)現(xiàn)顏值打分功能的詳細(xì)內(nèi)容,更多關(guān)于OpenCV MediaPipe顏值打分的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python+mediapipe+opencv實(shí)現(xiàn)手部關(guān)鍵點(diǎn)檢測(cè)功能(手勢(shì)識(shí)別)
- OpenCV+MediaPipe實(shí)現(xiàn)手部關(guān)鍵點(diǎn)識(shí)別
- ?Python使用Mediapipe對(duì)圖像進(jìn)行手部地標(biāo)檢測(cè)
- Python+MediaPipe實(shí)現(xiàn)檢測(cè)人臉功能詳解
- 超好玩的"隔空操物"通過(guò)Python?MediaPipe庫(kù)實(shí)現(xiàn)
- 基于Mediapipe+Opencv實(shí)現(xiàn)手勢(shì)檢測(cè)功能
- opencv+mediapipe實(shí)現(xiàn)人臉檢測(cè)及攝像頭實(shí)時(shí)示例
- MediaPipe API實(shí)現(xiàn)骨骼識(shí)別功能分步講解流程
相關(guān)文章
python使用difflib實(shí)現(xiàn)自動(dòng)查重
Python中有許多現(xiàn)成的庫(kù)和工具,可以方便地實(shí)現(xiàn)自動(dòng)查重的功能,其中,difflib庫(kù)就是一個(gè)專門(mén)用于比較文件和字符串差異的庫(kù),下面我們就來(lái)看看如何利用difflib實(shí)現(xiàn)自動(dòng)查重吧2023-11-11Python實(shí)現(xiàn)的序列化和反序列化二叉樹(shù)算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的序列化和反序列化二叉樹(shù)算法,結(jié)合實(shí)例形式分析了Python二叉樹(shù)的構(gòu)造、遍歷、序列化、反序列化等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03python利用joblib進(jìn)行并行數(shù)據(jù)處理的代碼示例
在數(shù)據(jù)量比較大的情況下,數(shù)據(jù)預(yù)處理有時(shí)候會(huì)非常耗費(fèi)時(shí)間,可以利用 joblib 中的 Parallel 和 delayed 進(jìn)行多CPU并行處理,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10python爬蟲(chóng)獲取淘寶天貓商品詳細(xì)參數(shù)
這篇文章主要為大家詳細(xì)介紹了python爬蟲(chóng)獲取淘寶天貓商品詳細(xì)參數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Python繪圖之自定義圖類型控件實(shí)現(xiàn)混合類型圖表
這篇文章主要為大家詳細(xì)介紹了Python如何新建繪圖類型控件,實(shí)現(xiàn)混合類型圖表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08