人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Python實戰(zhàn)之基于OpenCV的美顏掛件制作

發(fā)布日期:2021-12-09 15:59 | 文章來源:腳本之家

基于 Snapchat 的增強現(xiàn)實

胡子掛件融合

第一個項目中,我們將在檢測到的臉上覆蓋了一個小胡子。我們可以使用從攝像頭捕獲的連續(xù)視頻幀,也可以使用單張測試圖像。在進行實際講解程序的關(guān)鍵步驟前,首先查看應用程序預期輸出的結(jié)果圖像:


項目的第一步是檢測圖像中的人臉。如上圖所示,使用青色矩形繪制圖像中檢測到的人臉的位置和大小;接下來迭代圖像中所有檢測到的人臉,在其區(qū)域內(nèi)搜索鼻子,粉紅色矩形表示圖像中檢測到的鼻子;檢測到鼻子之后,就要根據(jù)之前計算出的鼻子的位置和大小來調(diào)整我們想要覆蓋“胡子”掛件的區(qū)域,藍色矩形表示計算獲得的胡須將被覆蓋的區(qū)域位置。如果處理的目標是連續(xù)視頻幀,在處理完成所有檢測到的人臉后,將繼續(xù)分析下一幀。

根據(jù)上述描述,程序應當首先檢測圖像中的人臉和鼻子。為了檢測這些對象,創(chuàng)建了兩個分類器,一個用于檢測人臉,另一個用于檢測鼻子:

# 用于人臉和鼻子檢測的級聯(lián)分類器
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
nose_cascade = cv2.CascadeClassifier("haarcascade_mcs_nose.xml")

一旦創(chuàng)建了分類器,下一步就是使用 cv2.detectMultiScale() 函數(shù)檢測圖像中的這些對象。cv2.detectMultiScale() 函數(shù)檢測輸入灰度圖像中不同大小的對象,并將檢測到的對象作為矩形列表返回。例如,檢測人臉時使用以下代碼:

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

接下來,遍歷檢測到的人臉,嘗試檢測鼻子:

# 遍歷檢測到的人臉
for (x, y, w, h) in faces:
 # 根據(jù)檢測到的面大小創(chuàng)建感興趣區(qū)域(ROI)
 roi_gray = gray[y:y + h, x:x + w]
 roi_color = frame[y:y + h, x:x + w]
 # 在檢測到的人臉內(nèi)檢測鼻子
 noses = nose_cascade.detectMultiScale(roi_gray)

檢測到鼻子后,遍歷所有檢測到的鼻子,并計算將被“胡子”掛件覆蓋的區(qū)域。過濾掉錯誤的鼻子位置后,將“胡子”掛件根據(jù)先前計算的區(qū)域覆蓋在圖像上:

for (nx, ny, nw, nh) in noses:
 # 計算將被“胡子”掛件覆蓋的區(qū)域坐標
 x1 = int(nx - nw / 2)
 x2 = int(nx + nw / 2 + nw)
 y1 = int(ny + nh / 2 + nh / 8)
 y2 = int(ny + nh + nh / 4 + nh / 6)
 if x1 < 0 or x2 < 0 or x2 > w or y2 > h:
  continue
 # 計算將被“胡子”掛件覆蓋的區(qū)域尺寸
 img_moustache_res_width = int(x2 - x1)
 img_moustache_res_height = int(y2 - y1)
 # 調(diào)整掩膜大小,使其與放置“胡子”掛件的區(qū)域相等
 mask = cv2.resize(img_moustache_mask, (img_moustache_res_width, img_moustache_res_height))
 # 翻轉(zhuǎn)掩膜
 mask_inv = cv2.bitwise_not(mask)
 # 將“胡子”掛件調(diào)整為所需區(qū)域
 img = cv2.resize(img_moustache, (img_moustache_res_width, img_moustache_res_height))
 # 獲取原始圖像的ROI
 roi = roi_color[y1:y2, x1:x2]
 # 創(chuàng)建ROI背景和ROI前景
 roi_bakground = cv2.bitwise_and(roi, roi, mask=mask_inv)
 roi_foreground = cv2.bitwise_and(img, img, mask=mask)
 # 獲取結(jié)果
 res = cv2.add(roi_bakground, roi_foreground)
 # 將res置于原始圖像中
 roi_color[y1:y2, x1:x2] = res
 break

上述程序的關(guān)鍵在于 img_mustache_mask 圖像。此圖像是使用要融合的“胡子”圖像的 Alpha 通道創(chuàng)建的,使用此圖像,將可以只繪制疊加圖像的前景,基于融合圖像的 alpha 通道創(chuàng)建的“胡子”蒙版如下:

img_moustache = cv2.imread('moustache.png', -1)
img_moustache_mask = img_moustache[:, :, 3]
cv2.imshow("img moustache mask", img_moustache_mask)

獲得的胡子蒙版,如下所示:

增強現(xiàn)實融合“胡子”掛件后的結(jié)果如下所示:

這里不再針對視頻幀的處理進行講解,因為其方法與單個圖像相同,在接下來的完整代碼中,給出對連續(xù)視頻幀進行增強現(xiàn)實的代碼。

完整代碼

import cv2
# 用于人臉和鼻子檢測的級聯(lián)分類器
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
nose_cascade = cv2.CascadeClassifier("haarcascade_mcs_nose.xml")
# 加載胡子圖像
img_moustache = cv2.imread('moustache.png', -1)
# 創(chuàng)建胡子蒙版
img_moustache_mask = img_moustache[:, :, 3]
# 將胡子圖像轉(zhuǎn)換為 BGR 圖像
img_moustache = img_moustache[:, :, 0:3]
# 創(chuàng)建 VideoCapture 對象
video_capture = cv2.VideoCapture(0)
while True:
 # 從 VideoCapture 對象捕獲幀
 ret, frame = video_capture.read()
 # 將 frame 轉(zhuǎn)換為灰度圖像
 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 # 檢測人臉
 faces = face_cascade.detectMultiScale(gray, 1.3, 5)
 # 迭代檢測到的人臉
 for (x, y, w, h) in faces:
  # 根據(jù)檢測到的人臉大小創(chuàng)建ROI
  roi_gray = gray[y:y + h, x:x + w]
  roi_color = frame[y:y + h, x:x + w]
  # 在檢測到的人臉中檢測鼻子
  noses = nose_cascade.detectMultiScale(roi_gray)
  for (nx, ny, nw, nh) in noses:
# 計算將放置 “胡子” 掛件的坐標
x1 = int(nx - nw / 2)
x2 = int(nx + nw / 2 + nw)
y1 = int(ny + nh / 2 + nh / 8)
y2 = int(ny + nh + nh / 4 + nh / 6)
if x1 < 0 or x2 < 0 or x2 > w or y2 > h:
 continue
# 計算“胡子”掛件區(qū)域的尺寸
img_moustache_res_width = int(x2 - x1)
img_moustache_res_height = int(y2 - y1)
# 根據(jù)掛件區(qū)域縮放“胡子”蒙版
mask = cv2.resize(img_moustache_mask, (img_moustache_res_width, img_moustache_res_height))
# 翻轉(zhuǎn)蒙版
mask_inv = cv2.bitwise_not(mask)
# 縮放“胡子”掛件
img = cv2.resize(img_moustache, (img_moustache_res_width, img_moustache_res_height))
# 從原始圖像中獲取ROI
roi = roi_color[y1:y2, x1:x2]
# 創(chuàng)建ROI前景和背景
roi_bakground = cv2.bitwise_and(roi, roi, mask=mask_inv)
roi_foreground = cv2.bitwise_and(img, img, mask=mask)
# roi_bakground 與 roi_foreground 加和獲取結(jié)果
res = cv2.add(roi_bakground, roi_foreground)
roi_color[y1:y2, x1:x2] = res
break
 # 顯示結(jié)果
 cv2.imshow('Snapchat-based OpenCV moustache overlay', frame)
 # 按下 “q” 鍵退出
 if cv2.waitKey(1) & 0xFF == ord('q'):
  break
# 釋放資源
video_capture.release()
cv2.destroyAllWindows()

眼鏡掛件融合

在這一實戰(zhàn)程序中,我們將學習在檢測到的面部眼睛區(qū)域上融合眼鏡掛件。我們通過下圖首次查看程序預期結(jié)果:


同樣為了實現(xiàn)眼鏡掛件融合,需要首先使用眼睛檢測器檢測圖像中的眼睛:

eyepair_cascade= cv2.CascadeClassifier("haarcascade_mcs_eyepair_big.xml")

上圖中青色矩形表示檢測到的人臉在圖像中的位置和大小;粉紅色矩形表示圖像中檢測到的眼睛;黃色矩形表示眼鏡將被覆蓋的位置,其根據(jù)眼睛所在區(qū)域的位置和大小進行計算。

同時,為融合的眼鏡眼鏡掛件圖像增加一些透明度,以使它們更逼真,眼鏡圖像蒙版如下所示:

融合后的結(jié)果圖像如下圖所示:

完整代碼

基本代碼與上例相同,因此不再進行贅述,需要注意的是“眼鏡”掛件的融合區(qū)域計算。

import cv2
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyepair_cascade = cv2.CascadeClassifier("haarcascade_mcs_eyepair_big.xml")
img_glasses = cv2.imread('glasses.png', -1)
img_glasses_mask = img_glasses[:, :, 3]
img_glasses = img_glasses[:, :, 0:3]
video_capture = cv2.VideoCapture(0)
while True:
 ret, frame = video_capture.read()
 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 # 檢測人臉
 faces = face_cascade.detectMultiScale(gray, 1.3, 5)
 for (x, y, w, h) in faces:
  roi_gray = gray[y:y + h, x:x + w]
  roi_color = frame[y:y + h, x:x + w]
  # 在檢測到的人臉中檢測眼睛
  eyepairs = eyepair_cascade.detectMultiScale(roi_gray)
  for (ex, ey, ew, eh) in eyepairs:
# 計算“眼睛”掛件放置的坐標
x1 = int(ex - ew / 10)
x2 = int((ex + ew) + ew / 10)
y1 = int(ey)
y2 = int(ey + eh + eh / 2)
if x1 < 0 or x2 < 0 or x2 > w or y2 > h:
 continue
# 計算“眼睛”掛件放置區(qū)域大小
img_glasses_res_width = int(x2 - x1)
img_glasses_res_height = int(y2 - y1)

mask = cv2.resize(img_glasses_mask, (img_glasses_res_width, img_glasses_res_height))
mask_inv = cv2.bitwise_not(mask)
img = cv2.resize(img_glasses, (img_glasses_res_width, img_glasses_res_height))
roi = roi_color[y1:y2, x1:x2]
roi_bakground = cv2.bitwise_and(roi, roi, mask=mask_inv)
roi_foreground = cv2.bitwise_and(img, img, mask=mask)
res = cv2.add(roi_bakground, roi_foreground):
roi_color[y1:y2, x1:x2] = res
break
 # 顯示結(jié)果畫面
 cv2.imshow('Snapchat-based OpenCV glasses filter', frame)
 if cv2.waitKey(1) & 0xFF == ord('q'):
  break
video_capture.release()
cv2.destroyAllWindows()

以上就是Python實戰(zhàn)之基于OpenCV的美顏掛件制作的詳細內(nèi)容,更多關(guān)于Python OpenCV的內(nèi)容請關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部