最近做LoRA模型訓(xùn)練時(shí)需要對(duì)一批圖片進(jìn)行人臉識(shí)別,并進(jìn)行裁剪,然后設(shè)置特定的分辨率。
首先要導(dǎo)入cv庫(kù)import cv2
如果沒有opencv庫(kù)的話要用pip先安裝一個(gè)pip install opencv-python
1、識(shí)別出圖片面部,并截取原圖片靠近面部的最大正方形部位,同時(shí)將截取的圖片分辨率改為512*512
import numpy as np
import cv2
import os
def crop_face(input_folder_path, output_folder_path):
# 加載面部識(shí)別模型
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
images = os.listdir(input_folder_path)
for image in images:
image_path = os.path.join(input_folder_path, image)
img = cv2.imread(image_path)
height, width, channels = img.shape
# 將圖像轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 檢測(cè)面部
faces = face_detector.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
# 無(wú)法識(shí)別面部的圖片
if len(faces)==0:
print(f"No face found in {image_path}")
return
if len(faces) > 0:
# 取第一個(gè)臉部位置,這里假設(shè)一張圖片只有一個(gè)臉部特征
# x、y 為人臉的像素位置,w、h 為人臉的寬度和高度。
x, y, w, h = faces[0]
# 確定最大正方形的位置
# 原圖片豎方向長(zhǎng),截取正方形長(zhǎng)度為原圖橫方向長(zhǎng),square_size截取正方形的長(zhǎng)度
if height>width:
square_size = width
x1=0
x2=square_size
# 原圖面部靠上
if y<square_size/2:
y1=0
y2 =square_size
# 原圖面部靠下
else:
y1=int(square_size/2)
y2 =height
# 原圖片是橫方向長(zhǎng),截取正方形長(zhǎng)度為原圖豎方向長(zhǎng)度
else:
square_size = height
y1=0
y2=square_size
# 原圖面部靠右
if x<square_size/2:
x1=0
x2 =square_size
# 原圖面部靠左
else:
x1=int(square_size/2)
x2 =square_size
# 根據(jù)最大正方形位置裁剪圖片并保存
cropped_img = img[y1:y2, x1:x2]
# 調(diào)整圖像大小為512x512
resized = cv2.resize(cropped_img, (512, 512), interpolation=cv2.INTER_AREA)
output_path = os.path.join(output_folder_path,image)
cv2.imwrite(output_path, resized)
if __name__ == "__main__":
input_folder = r"輸入圖片所在文件夾路徑"
output_folder = r"輸出圖片所在文件夾路徑"
# 創(chuàng)建輸出目錄
if not os.path.exists(output_folder):
os.makedirs(output_folder)
crop_face(input_folder, output_folder)
print('Done!')
可以看到a1(7)這張圖片,模型無(wú)法識(shí)別,大家做的時(shí)候根據(jù)實(shí)際情況,調(diào)整這個(gè)位置的參數(shù),以提高識(shí)別的準(zhǔn)確度。
faces = face_detector.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
scaleFactor:表示在前后兩次相繼的掃描中,搜索窗口的比例系數(shù)。默認(rèn)為1.1即每次搜索窗口依次擴(kuò)大10%。可以根據(jù)圖像的像素值來(lái)設(shè)置此參數(shù),像素大縮小的速度就可以快一點(diǎn),通常在1~1.5之間。
minNeighbors:表示構(gòu)成檢測(cè)目標(biāo)的相鄰矩形的最小個(gè)數(shù)(默認(rèn)為3個(gè))。是在人臉附近進(jìn)行指定次數(shù)的檢測(cè),獲取最準(zhǔn)確的范圍,設(shè)置越高,誤檢率越低,但是對(duì)于迷糊圖片,設(shè)置越高,越不易檢測(cè)出來(lái),要適當(dāng)降低。
2、以面部為中心,截取的最大正方形
import numpy as np
import cv2
import os
def crop_face(input_folder_path, output_folder_path):
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
images = os.listdir(input_folder_path)
for image in images:
image_path = os.path.join(input_folder_path, image)
img = cv2.imread(image_path)
height, width, channels = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
# 無(wú)法識(shí)別面部的圖片
if len(faces)==0:
print(f"No face found in {image_path}")
return
if len(faces) > 0:
# 取第一個(gè)臉部位置,這里假設(shè)一張圖片只有一個(gè)臉部特征
x, y, w, h = faces[0]
# 確定最大正方形的位置
square_size=min(width-x,x,y,height-y)
# 根據(jù)最大正方形位置裁剪圖片并保存
cropped_img = img[y-square_size:y+h+square_size, x-square_size:x+w+square_size] #img[y1:y2, x1:x2]
# 調(diào)整圖像大小為512x512
resized = cv2.resize(cropped_img, (512, 512), interpolation=cv2.INTER_AREA)
output_path = os.path.join(output_folder_path, image)
cv2.imwrite(output_path, resized)
if __name__ == "__main__":
input_folder = r"輸入圖片所在文件夾路徑"
output_folder = r"輸出圖片所在文件夾路徑"
# 創(chuàng)建輸出目錄
if not os.path.exists(output_folder):
os.makedirs(output_folder)
crop_face(input_folder, output_folder)
print('Done!')
3、只截取人臉部分文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-519302.html
import numpy as np
import cv2
import os
def crop_face(input_folder_path, output_folder_path):
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
images = os.listdir(input_folder_path)
for image in images:
image_path = os.path.join(input_folder_path, image)
img = cv2.imread(image_path)
height, width, channels = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=7, minSize=(30, 30))
# 無(wú)法識(shí)別面部的圖片
if len(faces)==0:
print(f"No face found in {image_path}")
return
for (x,y,w,h) in faces:
cropped_img = img[y:y+h, x:x+w]
# 調(diào)整圖像大小為512x512
resized = cv2.resize(cropped_img, (512, 512), interpolation=cv2.INTER_AREA)
# 將圖像保存到輸出目錄
output_path = os.path.join(output_folder_path, image)
cv2.imwrite(output_path, resized)
if __name__ == "__main__":
input_folder = r"輸入圖片所在文件夾路徑"
output_folder = r"輸出圖片所在文件夾路徑"
# 創(chuàng)建輸出目錄
if not os.path.exists(output_folder):
os.makedirs(output_folder)
crop_face(input_folder, output_folder)
print('Done!')
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-519302.html
到了這里,關(guān)于使用opencv批量人臉識(shí)別+裁圖+設(shè)置分辨率(Python代碼分享)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!