效果
上一篇文章无法做到侧脸的识别
cv-profileface可以识别侧脸
# 法1 opencv profileface 准确率不高 参考https://github.com/jb892/FaceDetection-HaarCascade
import cv2
import matplotlib.pyplot as plt
face_detector = cv2.CascadeClassifier('haarcascade_profileface.xml')
img = cv2.imread('2.jpg')
wid = 10
pts = 400
img=cv2.resize(img,(pts,pts))
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
face1 = face_detector.detectMultiScale(img1, scaleFactor=1.15,minNeighbors=5,minSize=(34, 35), flags=cv2.CASCADE_SCALE_IMAGE)
print(face1)
for (x, y, w, h) in face1: # 在img1上绘制一个矩形框
cv2.rectangle(img1, (x-wid, y-wid), (x+w + wid, y+h + wid), (255, 0, 0), 2)
plt.imshow(img1)
dlib-mmod_human_face_detector 侧脸识别准确率更高
# 法2 dlib mmod_human_face_detector
#参考https://blog.csdn.net/hongbin_xu/article/details/78359520
#参考https://blog.csdn.net/LJX_ahut/article/details/124969818
import dlib
import matplotlib.pyplot as plt
# step 1. make sure you have downloaded the correct model file
face_detector_model_path = './mmod_human_face_detector.dat'
# step 2. load this model and create a cnn face detector
face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path) # dlib.cnn_face_detection_model_v1
# step 3. read an image using dlib or cv2
# note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2
image_path = '1.jpg'
img = dlib.load_rgb_image(image_path)
pts = 300
wid = 10
img = cv2.resize(img,(pts,pts))
# img = cv2.imread(image_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# step 4. predict and detect
detections = face_detector(img, 1) # dlib.mmod_rectangles
# step 5. get just one of the rectangle instead all of them ,the type is mmod_rectangle
detection = detections[0] # dlib.mmod_rectangle
# the mmod_rectangle contains two parts : confidence and rect
print(detection.confidence, detection.rect)
# step 6.get face coordinates for just one as sample
l, t, r, b = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()
cv2.rectangle(img, (l-wid, t-wid), (r+wid, b+wid), (255, 0, 0), 2)
plt.imshow(img)
分割
# In[]
import os
import warnings
import cv2
import matplotlib.pyplot as plt
import dlib
from PIL import Image
warnings.filterwarnings("error", category=UserWarning)
PATH1 = "./data/"
outpts = 120
wid = 10
def is_read_successfully(file):
try:
imgFile = Image.open(file)
return True
except Exception:
return False
def clipface(parent,file):
in_path=os.path.join(parent, file)
img = dlib.load_rgb_image(in_path)
# plt.imshow(img);
face_detector = dlib.cnn_face_detection_model_v1('./mmod_human_face_detector.dat') # dlib.cnn_face_detection_model_v1
detections = face_detector(img)
# 人脸数
if len(detections)> 0:
detection = detections[0] # dlib.mmod_rectangle
# print(detection.confidence, detection.rect)
l, t, r, b = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()
#获取宽度和高度
height=len(img)
width=len(img[0])
img1=img[t-wid:b+wid,l-wid:r+wid]
height=len(img1)
width=len(img1[0])
print('图片大小%d X %d'%(width,height))
plt.imshow(img1)
dir=os.path.join('face/',parent)
if not os.path.exists(dir):
os.makedirs(dir)
cv2.imwrite(os.path.join(dir,file), cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
if __name__=="__main__":
#子文件夹
for childlist in os.listdir(PATH1):
#子文件夹路径
childPATH = PATH1 + '/'+ str(childlist)
for cpath, dirs, files in os.walk(childPATH):
if childlist== '14' :
sum=0
print (childlist)
for file in files:
in_file = os.path.join(cpath, file)
if not is_read_successfully(in_file):
print('cant open'+ in_file)
else:
sum+=1
print(str(sum) + 'read ' + in_file)
clipface(cpath,file)
# %%