教程:使用深度學習和OpenCV的自拍過濾器(面部標記檢測)

# Just load the recent model using load_my_CNN_model and use it to predict keypoints on any face data

my_model = load_my_CNN_model('my_model')

'''

第2步:初始化Stuff

我們現在創建一個新的python文件 --shades.py,我們在其中編寫代碼來讀取網絡攝像頭輸入,檢測面部,使用我們構建的CNN模型等。

from my_CNN_model import *

import cv2

import numpy as np

# Load the model built in the previous step

my_model = load_my_CNN_model('my_model')

# Face cascade to detect faces

face_cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml')

# Define the upper and lower boundaries for a color to be considered "Blue"

blueLower = np.array([100, 60, 60])

blueUpper = np.array([140, 255, 255])

# Define a 5x5 kernel for erosion and dilation

kernel = np.ones((5, 5), np.uint8)

# Define filters

filters = ['images/sunglasses.png', 'images/sunglasses_2.png', 'images/sunglasses_3.jpg', 'images/sunglasses_4.png', 'images/sunglasses_5.jpg', 'images/sunglasses_6.png']

filterIndex = 0

# Load the video - O for webcam input

camera = cv2.VideoCapture(0)

第3步:檢測輸入中的面部

一旦開始從網絡攝像頭讀取輸入,使用我們之前初始化的Cascade Classifier對象檢測輸入中的臉。

# Keep reading the input

while True:

(grabbed, frame) = camera.read()

frame = cv2.flip(frame, 1)

frame2 = np.copy(frame)

# Convert to HSV and GRAY for convenience

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces using the haar cascade object

faces = face_cascade.detectMultiScale(gray, 1.25, 6)

第4步:創建過濾器切換觸發器

在這裡,我使用藍色瓶蓋作為過濾開關。為了檢測藍色瓶蓋,我們編寫代碼以找到圖像中的藍色輪廓。我們傳遞了在步驟2中定義的blueLower和blueUpper hsv範圍。

# Add the 'Next Filter' button to the frame

frame = cv2.rectangle(frame, (500,10), (620,65), (235,50,50), -1)

cv2.putText(frame, "NEXT FILTER", (512, 37), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)

# Determine which pixels fall within the blue boundaries

# and then blur the binary image to remove noise

blueMask = cv2.inRange(hsv, blueLower, blueUpper)

blueMask = cv2.erode(blueMask, kernel, iterations=2)

blueMask = cv2.morphologyEx(blueMask, cv2.MORPH_OPEN, kernel)

blueMask = cv2.dilate(blueMask, kernel, iterations=1)

一旦我們創建了blueMask,它應該在視頻中找到藍色的東西,我們使用OpenCV的cv2.findContours()方法來查找輪廓。

# Find contours (bottle cap in my case) in the image

(_, cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

center = None

# Check to see if any contours were found

if len(cnts) > 0:

# Sort the contours and find the largest one -- we

# will assume this contour correspondes to the area of the bottle cap

cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]

# Get the radius of the enclosing circle around the found contour

((x, y), radius) = cv2.minEnclosingCircle(cnt)

# Draw the circle around the contour

cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)

# Get the moments to calculate the center of the contour (in this case Circle)

M = cv2.moments(cnt)

center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

# Once

if center[1] <= 65:

if 500 <= center[0] <= 620: # Next Filter

filterIndex += 1

filterIndex %= 6

continue

步驟5:使用模型檢測面部關鍵點

我們使用我們之前初始化的面部級聯來定位框架中的面。然後,我們遍歷每個面部以預測面部關鍵點。

# Loop over all the faces found in the frame

for (x, y, w, h) in faces:

# Make the faces ready for the model (normalize, resize and stuff)

gray_face = gray[y:y+h, x:x+w]

color_face = frame[y:y+h, x:x+w]

# Normalize to match the input format of the model - Range of pixel to [0, 1]

gray_normalized = gray_face / 255

# Resize it to 96x96 to match the input format of the model

original_shape = gray_face.shape # A Copy for future reference

face_resized = cv2.resize(gray_normalized, (96, 96), interpolation = cv2.INTER_AREA)

face_resized_copy = face_resized.copy()

face_resized = face_resized.reshape(1, 96, 96, 1)

# Predict the keypoints using the model

keypoints = my_model.predict(face_resized)

# De-Normalize the keypoints values

keypoints = keypoints * 48 + 48

# Map the Keypoints back to the original image

face_resized_color = cv2.resize(color_face, (96, 96), interpolation = cv2.INTER_AREA)

face_resized_color2 = np.copy(face_resized_color)

# Pair the keypoints together - (x1, y1)

points = []

for i, co in enumerate(keypoints[0][0::2]):

points.append((co, keypoints[0][1::2][i]))

在我們將檢測到的面部傳遞給模型之前,我們必須對輸入進行歸一化,因為歸一化圖像是我們模型訓練的圖像,將其大小調整為96 x 96圖像,因為這是我們的模型所期望的。上面的代碼也是一樣的。

第6步:使用面部關鍵點將陰影置於面部

一旦我們檢測到面部關鍵點,我們就可以使用它們來做各種很酷的事情。例如,您可以使用鼻子關鍵點添加鬍子,嘴唇關鍵點,以便為它們添加顏色等。

# Add FILTER to the frame

sunglasses = cv2.imread(filters[filterIndex], cv2.IMREAD_UNCHANGED)

sunglass_width = int((points[7][0]-points[9][0])*1.1)

sunglass_height = int((points[10][1]-points[8][1])/1.1)

sunglass_resized = cv2.resize(sunglasses, (sunglass_width, sunglass_height), interpolation = cv2.INTER_CUBIC)

transparent_region = sunglass_resized[:,:,:3] != 0

face_resized_color[int(points[9][1]):int(points[9][1])+sunglass_height, int(points[9][0]):int(points[9][0])+sunglass_width,:][transparent_region] = sunglass_resized[:,:,:3][transparent_region]

# Map the face with shades back to its original shape

frame[y:y+h, x:x+w] = cv2.resize(face_resized_color, original_shape, interpolation = cv2.INTER_CUBIC)

# Add KEYPOINTS to the frame2

for keypoint in points:

cv2.circle(face_resized_color2, keypoint, 1, (0,255,0), 1)

# Map the face with keypoints back to the original image (a separate one)

frame2[y:y+h, x:x+w] = cv2.resize(face_resized_color2, original_shape, interpolation = cv2.INTER_CUBIC)

在這裡,我們使用了4個關鍵點來配置臉部陰影的寬度和高度。並且,face_resized_color有陰影,並且映射回frame,face_resized_color2有關鍵點,並映射回frame2。然後我們將它們顯示出來。

# Show the frame and the frame2

cv2.imshow("Selfie Filters", frame)

cv2.imshow("Facial Keypoints", frame2)

# If the 'q' key is pressed, stop the loop

if cv2.waitKey(1) & 0xFF == ord("q"):

break

# Cleanup the camera and close any open windows

camera.release()

cv2.destroyAllWindows()

最後,我們只是清理stuff。

執行

1.下載數據

從此處下載數據(https://www.kaggle.com/c/facial-keypoints-detection/data)並將其放在項目目錄中名為“data”的文件夾中。確保'data'文件夾包含 - training.csv和test.csv

2.構建CNN模型

> python model_builder.py

這會調用my_CNN_model.py,因此請確保您的項目目錄中包含該文件。

4.運行引擎文件

> python shades.py

5.拿一個藍色瓶蓋測試

結論

在本教程中,我們構建了一個深度卷積神經網絡模型,對面部關鍵點數據進行了訓練。然後,我們使用該模型來預測在輸入網絡攝像頭數據中檢測到的面部的面部關鍵點。我們還使用輪廓創建了一個開關,它允許我們使用手勢迭代其他過濾器。我鼓勵您調整模型架構並親自了解它如何影響關鍵點檢測。

這個數據只有15個關鍵點,其他幾個數據集在面上標有超過30個關鍵點。

教程:使用深度學習和OpenCV的自拍過濾器(面部標記檢測)

面部關鍵點檢測的應用:

  • 人臉特徵檢測提高了人臉識別能力
  • 男/女區分
  • 面部表情區別
  • 頭部姿勢估計
  • 面對變形
  • 虛擬化妝
  • 面部更換

您可以在此處訪問完整的項目代碼:

akshaychandra21 / Selfie_Filters_OpenCV (https://github.com/akshaychandra21/Selfie_Filters_OpenCV/)


分享到:


相關文章: