第1关:人脸检测
'''****************BEGIN****************'''
import face_recognition
image_path = './step1/image/children.jpg'
image = face_recognition.load_image_file("./step1/image/children.jpg")
face_locations = face_recognition.face_locations(image)
print(face_locations)
'''**************** END ****************'''
import cv2
for face_location in face_locations:
'''****************BEGIN****************'''
top, right, bottom, left = face_location
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
'''**************** END ****************'''
# 保存图片
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite("./step1/out/children.jpg", image_rgb)
第2关:人脸特征点获取
import face_recognition
'''****************BEGIN****************'''
# 获取人脸特征点
image = face_recognition.load_image_file("./step2/image/laugh.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
print(face_landmarks_list)
'''**************** END ****************'''
import cv2
# 绘制人脸特征点
for face_landmarks in face_landmarks_list:
'''****************BEGIN****************'''
for facial_feature in face_landmarks.keys():
for pt_pos in face_landmarks[facial_feature]:
cv2.circle(image, pt_pos, 1, (255, 0, 0), 2)
'''**************** END ****************'''
# 保存图片
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite("./step2/out/laugh.jpg", image_rgb)
第3关:人脸识别
import face_recognition
def recognition():
'''****************BEGIN****************'''
# 导入图片
known_image_path = "./step3/known_image/cyx1.jpg"
known_image_cyz = face_recognition.load_image_file("./step3/known_image/cyx1.jpg")
unknown_image_1_path = "./step3/unknown_image/cyx2.jpg"
unknown_image_2_path = "./step3/unknown_image/wlh.jpg"
unknown_image_1 = face_recognition.load_image_file("./step3/unknown_image/cyx2.jpg")
unknown_image_2 = face_recognition.load_image_file("./step3/unknown_image/wlh.jpg")
'''**************** END ****************'''
'''****************BEGIN****************'''
# 编码获取128维特征向量
cyz_encoding = face_recognition.face_encodings(known_image_cyz)[0]
unknown_encoding_1 = face_recognition.face_encodings(unknown_image_1)[0]
unknown_encoding_2 = face_recognition.face_encodings(unknown_image_2)[0]
'''**************** END ****************'''
'''****************BEGIN****************'''
# 比较特征向量值,识别人脸
face1_result = face_recognition.compare_faces([cyz_encoding], unknown_encoding_1, tolerance=0.5)
face2_result = face_recognition.compare_faces([cyz_encoding], unknown_encoding_2, tolerance=0.5)
'''**************** END ****************'''
return face1_result, face2_result
第4关:人脸识别绘制并展示
import face_recognition
import cv2
'''****************BEGIN****************'''
# 加载已知图片
known_image_cc_path = "./step4/known_image/Caocao.jpg"
known_image_xy_path = "./step4/known_image/XunYu.jpg"
known_image_smy_path = "./step4/known_image/SiMayi.jpg"
known_image_zch_path = "./step4/known_image/ZhangChunhua.jpg"
known_image_cc = face_recognition.load_image_file("./step4/known_image/Caocao.jpg")
known_image_xy = face_recognition.load_image_file("./step4/known_image/XunYu.jpg")
known_image_smy = face_recognition.load_image_file("./step4/known_image/SiMayi.jpg")
known_image_zch= face_recognition.load_image_file("./step4/known_image/ZhangChunhua.jpg")
'''**************** END ****************'''
'''****************BEGIN****************'''
# 对图片进行编码,获取128维特征向量
caocao_encoding = face_recognition.face_encodings(known_image_cc)[0]
xy_encoding = face_recognition.face_encodings(known_image_xy)[0]
zys_encoding = face_recognition.face_encodings(known_image_smy)[0]
cyz_encoding = face_recognition.face_encodings(known_image_zch)[0]
'''**************** END ****************'''
'''****************BEGIN****************'''
# 存为数组以便之后识别
known_faces = [
caocao_encoding,
xy_encoding,
zys_encoding,
cyz_encoding
]
'''**************** END ****************'''
'''****************BEGIN****************'''
# 加载待识别图片
unknown_image_1_path = "./step4/unknown_image/Caocao.jpg"
unknown_image_2_path = "./step4/unknown_image/Cuple.jpg"
unknown_image_3_path = "./step4/unknown_image/ZhangChunhua.jpg"
unknown_image_4_path = "./step4/unknown_image/XunYu.jpg"
unknown_image_5_path = './step4/unknown_image/A.jpg'
unknown_image_1 = face_recognition.load_image_file("./step4/unknown_image/Caocao.jpg")
unknown_image_2 = face_recognition.load_image_file("./step4/unknown_image/Cuple.jpg")
unknown_image_3 = face_recognition.load_image_file("./step4/unknown_image/ZhangChunhua.jpg")
unknown_image_4 = face_recognition.load_image_file("./step4/unknown_image/XunYu.jpg"
)
unknown_image_5 = face_recognition.load_image_file('./step4/unknown_image/A.jpg')
'''**************** END ****************'''
'''****************BEGIN****************'''
# 存为数组以遍历识别
unknown_faces = [
unknown_image_1,
unknown_image_2,
unknown_image_3,
unknown_image_4,
unknown_image_5,
]
'''**************** END ****************'''
# 初始化一些变量
face_locations = []
face_encodings = []
face_names = []
frame_number = 0
for frame in unknown_faces:
face_names = []
'''****************BEGIN****************'''
# 获取人脸区域位置
face_locations = face_recognition.face_locations(frame)
# 对图片进行编码,获取128维特征向量
face_encodings = face_recognition.face_encodings(frame, face_locations)
# 对图片进行编码,获取128维特征向量
'''**************** END ****************'''
for face_encoding in face_encodings:
'''****************BEGIN****************'''
# 识别图片中人脸是否匹配已知图片
match = face_recognition.compare_faces(known_faces, face_encoding,tolerance=0.5)
'''**************** END ****************'''
'''****************BEGIN****************'''
name = None
if match[0]:
name = "Caocao"
elif match[1]:
name = "XunYu"
elif match[2]:
name = "SiMayi"
elif match[3]:
name = 'ZhangChunhua'
else:
name = 'Unknown'
'''**************** END ****************'''
face_names.append(name)
# 结果打上标签
for (top, right, bottom, left), name in zip(face_locations, face_names):
if not name:
continue
'''****************BEGIN****************'''
# 绘制脸部区域框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 在脸部区域下面绘制人名
cv2.rectangle(frame, (left, bottom - 25),(right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6),font, 0.5, (255, 255, 255), 1)
'''**************** END ****************'''
print(frame[left+6, bottom-6])
print(frame[left, bottom])
print(face_locations)
print(face_names)
# 保存图片
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
path = './step4/out/' + name + str(face_locations[0][0]) + '.jpg'
cv2.imwrite(path, image_rgb)
图像生成
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as func
import torchvision
import matplotlib.pylab as plt
import numpy as np
# 转换
def denomalize(x):
##########Begin##########
out = (x+1) / 2
out = out.view(32, 28, 28).unsqueeze(1) # 添加channel
return out.clamp(0,1)
###########End###########
# 保存并显示
def imshow(img,epoch):
##########Begin##########
# torchvision.utils.make_grid用来连接一组图, img为一个tensor(batch, channel, height, weight)
# .detach()消除梯度
im = torchvision.utils.make_grid(img, nrow=8).detach().numpy()
# print(np.shape(im))
plt.title("Generated IMG {}".format(epoch))
plt.imshow(im.transpose(1, 2, 0)) # 调整图形标签, plt的图片格式为(height, weight, channel)
plt.savefig('./img_out/generate{}.jpg'.format(str(epoch)))
plt.show()
###########End###########
# 判别器模型
class DNet(nn.Module):
##########Begin##########
def __init__(self):
super(DNet, self).__init__()
self.l1 = nn.Linear(28*28, 256)
self.a = nn.ReLU()
self.l2 = nn.Linear(256, 128)
self.l3 = nn.Linear(128, 1)
self.s = nn.Sigmoid()
def forward(self, x):
x = self.l1(x)
x = self.a(x)
x = self.l2(x)
x = self.a(x)
x = self.l3(x)
x = self.s(x)
return x
###########End###########
# 生成器模型
class GNet(nn.Module):
##########Begin##########
def __init__(self):
super(GNet, self).__init__()
self.l1 = nn.Linear(10, 128)
self.a = nn.ReLU()
self.l2 = nn.Linear(128, 256)
self.l3 = nn.Linear(256, 28*28)
self.tanh = nn.Tanh()
def forward(self, x):
x = self.l1(x)
x = self.a(x)
x = self.l2(x)
x = self.a(x)
x = self.l3(x)
x = self.tanh(x)
return x
###########End###########
device = torch.device("cpu")
# 构建模型
D = DNet().to(device)
G = GNet().to(device)
print(D)
print(G)
z = torch.tensor([[ 1.1489e+00, 7.1681e-01, 5.8007e-02, -1.2308e-01, -8.6097e-01,
1.2392e+00, -1.1096e+00, 2.4585e-01, -7.0626e-01, -2.8230e-01],
[-2.1945e-01, 4.6672e-01, 7.7213e-01, -9.8824e-01, 1.3838e+00,
2.2511e-01, -1.1168e+00, 2.2530e+00, 7.3158e-01, 1.7082e-01],
[-2.6517e+00, 1.3954e+00, 1.5612e+00, -3.5644e-01, 1.3853e+00,
-3.6608e-01, 2.3234e-03, -4.7733e-01, -1.7758e-01, 3.4617e-01],
[ 4.7174e-01, -7.3149e-01, 1.1419e-01, -2.6241e-01, -1.1660e-01,
-1.3477e-01, 1.1245e+00, -1.6306e+00, 6.1722e-01, -1.3473e-01],
[ 1.0552e+00, -1.4014e-01, -5.8463e-01, 1.2349e+00, -6.5313e-01,
-2.3425e-01, 1.0186e-01, -6.0410e-01, -7.4244e-01, -8.8516e-01],
[-5.3014e-01, 6.2547e-01, -1.5977e-01, -2.3066e-01, 9.8950e-02,
-4.8964e-01, -1.0218e+00, -5.9025e-01, -1.0131e+00, -6.5106e-01],
[-1.5537e+00, -6.9103e-01, -7.3599e-01, -4.3914e-01, -2.0448e-01,
-1.4190e+00, -1.2123e+00, -1.6747e-01, -1.1395e+00, 5.5171e-01],
[-1.6244e+00, -7.4496e-01, -1.2768e+00, -4.2177e-01, -6.1283e-01,
-8.8188e-01, 4.1788e-01, 9.2558e-01, -1.5659e+00, -1.5211e-01],
[ 9.3092e-02, -1.5520e-01, 1.6417e+00, 6.8507e-01, 2.4547e+00,
-8.3659e-02, 3.2725e+00, 4.3044e-01, 1.3569e-01, 1.4817e+00],
[-1.4958e+00, -5.2917e-02, -3.6961e-01, -2.2025e+00, -1.6436e-01,
1.2136e+00, -1.3152e-02, -1.2154e+00, 2.0911e-03, 2.9080e-01],
[-1.0469e+00, 2.8222e+00, -3.9115e-01, 2.2041e-01, -7.8101e-01,
1.2563e+00, 1.1753e+00, -1.3332e+00, 2.4884e+00, -4.4259e-01],
[-7.0369e-01, -1.4290e+00, 6.3865e-01, -5.8341e-01, -1.3592e+00,
-5.8469e-01, 1.8228e+00, 1.0016e+00, 5.4477e-01, -2.2182e+00],
[-3.4230e-01, 9.9979e-01, 1.2068e-01, -2.5346e-01, -1.1366e+00,
-1.3778e+00, -1.3563e+00, 9.4610e-01, -9.4018e-01, -9.2094e-01],
[ 8.2746e-01, -6.2931e-01, -3.8875e-01, 2.1943e-01, 6.4780e-01,
-1.0844e+00, -8.8125e-01, 2.8869e-01, -1.0784e-01, -6.1752e-01],
[ 7.3338e-01, -8.2371e-01, 8.7851e-01, 9.9646e-01, -1.7378e+00,
4.4679e-01, -4.5057e-01, 8.8582e-01, 1.0759e+00, 7.1904e-01],
[ 1.3013e+00, 2.7334e-01, 4.3593e-01, -1.2887e+00, -1.2346e+00,
1.5697e+00, 3.1392e-01, -1.8793e+00, 7.4765e-01, -1.5952e+00],
[-1.7635e-01, -5.8611e-02, -3.1754e-01, 5.3886e-01, 4.2619e-01,
9.4467e-01, 2.0849e-01, -3.3167e-01, -4.1872e-01, -7.3638e-02],
[ 6.3905e-01, 6.8031e-02, 1.8931e+00, 6.8102e-01, 6.8116e-01,
-6.4648e-02, 4.3161e-01, 1.8226e+00, 2.1950e+00, -4.8745e-01],
[-1.6209e+00, 7.3165e-01, 1.1026e-01, 1.1185e+00, 2.7662e+00,
-5.5616e-01, 3.9975e-01, -5.4667e-01, 8.1839e-01, 2.9950e-01],
[-4.0914e-01, 5.0958e-01, 5.3542e-01, -6.3667e-01, -5.9792e-01,
-1.3693e+00, 7.9520e-02, 1.9240e-01, -7.1213e-01, -3.1667e-01],
[ 2.8481e-01, -1.1645e+00, 1.0669e-01, 8.8317e-01, -1.1061e+00,
1.1247e+00, -1.8601e-01, 3.4027e+00, 1.1318e+00, -2.4114e+00],
[ 2.7305e-01, -3.0332e-01, -4.0622e-01, 2.2628e-01, 1.8857e+00,
-3.0286e-01, -6.2789e-01, -2.3954e-01, -7.5774e-01, -6.8736e-01],
[ 3.8802e-01, 1.6416e+00, 5.8121e-01, 1.9790e+00, -3.7603e-02,
-3.2679e-01, -4.7280e-01, -1.4476e-01, 1.0581e+00, -9.8440e-01],
[-3.9233e-01, 3.2649e-02, 7.7994e-01, -1.3475e+00, -2.7656e-01,
1.8111e+00, -1.3303e-01, 1.8950e+00, 1.2445e+00, 8.5697e-01],
[-1.0138e+00, -3.4321e-01, 9.0624e-01, -1.5395e+00, -3.9321e-01,
-4.8072e-01, -9.0556e-01, 1.5222e+00, -8.9406e-01, -8.8321e-01],
[ 5.2679e-02, 4.2842e-01, 1.1687e+00, 3.4735e-01, -2.9261e-01,
-3.1606e-01, -5.9302e-01, -3.0689e-01, 1.0589e+00, -7.3568e-01],
[ 5.9457e-01, -5.2930e-01, 6.7054e-01, 1.0220e+00, -1.4623e-02,
-1.7065e-01, -1.6068e+00, 8.9959e-01, -1.1968e+00, 2.4110e-01],
[ 8.9343e-02, 1.4014e+00, 6.8921e-01, 4.5150e-01, -2.6966e+00,
1.8082e+00, 7.1977e-01, -1.2223e+00, 1.3240e-01, -3.1817e-03],
[-6.7737e-01, 1.1342e+00, -3.7651e-01, 9.1442e-01, 3.4748e-01,
-1.2479e+00, -6.5175e-01, 2.9314e-01, 1.0561e-01, 4.2260e-01],
[ 1.2196e+00, 1.5686e-01, 3.7240e-01, 1.3012e+00, 2.5194e-01,
-6.7221e-01, 1.3126e+00, 8.6740e-01, -2.5076e-01, 7.7630e-01],
[ 5.8054e-01, 1.6174e+00, -3.9814e-01, -5.9296e-01, -6.1294e-01,
1.0387e+00, -2.5145e+00, 7.9484e-01, -7.6897e-01, -1.1325e+00],
[ 3.6981e-01, 3.0270e-01, 1.7068e-01, 8.7460e-01, 2.0930e-01,
-3.2083e-01, 6.9222e-01, -1.2070e-01, -1.2727e+00, 6.8880e-01]]).to(device)
# 加载预训练模型并生成图片
##########Begin##########
for epoch in [10,50,100,200,400]:
G.load_state_dict(torch.load("./G{}.pth".format(epoch),map_location=device))
G.to(device)
img = G(z)
img = denomalize(img.to("cpu"))
imshow(img,epoch)
###########End###########
if epoch ==200:
print(img)