# 跟面部识别开愚人玩笑？做一个时尚口罩就能实现

Haar级联分类器：Haar级联使用一个数据集和大量标记过的积极和消极示例图片进行训练。Haar级联分类器的主要缺点是只能识别正面照片。由于神经网络更为通用，所以它们无法广泛应用。

Python中的定向梯度直方图面部检测

import dlib

import cv2

frontal_face_detector = dlib.get_frontal_face_detector()

upscaling_factor = 1

detected_faces = frontal_face_detector(img, upscaling_factor)

# indexes:

# 0 % 4 = pos_x

# 1 % 4 = pos_y

# 2 % 4 = rotation

# 3 % 4 = scale

initial_state = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

def detected_max(state):

# converts the 1D state array into images

get_img_from_state(state)

fitness = mlrose.CustomFitness(detected_max)

problem = mlrose.DiscreteOpt(length=24, fitness_fn=fitness,

maximize=True, max_val=scale_factor)

schedule = mlrose.ExpDecay()

best_state, max_faces = mlrose.simulated_annealing(problem, schedule=schedule, max_attempts=10, max_iters=1000,

init_state=initial_state, random_state=1)

print('Optimal state found: ', best_state)

print('Max fitness found: ', max_faces)

# save the optimal found

get_img_from_state(best_state)

print("Number of faces in output: ", len(detect_faces(cv2.imread(OUTPUT))))

1307 篇文章122 人订阅

0 条评论