|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
ao1.py
import cv2
import numpy as np
import dlib
def cv_show(neme, img):
cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
class Eigenvector(object):
def __init__(self):
# resnet模型
self.face_descriptor_extractor = dlib.face_recognition_model_v1(
'./data/data_dlib/dlib_face_recognition_resnet_model_v1.dat')
# 人脸检测模型
self.hog_face_detector = dlib.get_frontal_face_detector()
# 获取人脸的68个关键点
self.shape_detector = dlib.shape_predictor('./data/data_dlib/shape_predictor_68_face_landmarks.dat')
def fun1(self, img):
# 检测人脸
detections = self.hog_face_detector(img, 1)
# 可能识别多张人脸(但只要第一个识别到的人脸)
for face in detections:
# # 人脸框坐标
# l, t, r, b = face.left(), face.top(), face.right(), face.bottom()
# 获取68个关键点
points = self.shape_detector(img, face)
# # 绘制关键点
# for point in points.parts():
# cv2.circle(img, (point.x, point.y), 2, (0, 255, 0), 1)
#
# # 绘制矩形框
# cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 2)
# cv_show('neme', img)
# 获取特征描述符
# 功能:图像中的68个关键点转换为128D面部描述符,其中同一人的图片被映射到彼此附近,并且不同人的图片被远离地映射。
# 参数:img_rd:人脸灰度图,类型:numpy.ndarray
# shape:68个关键点位置
# 返回值:128D面部描述符
face_descriptor = self.face_descriptor_extractor.compute_face_descriptor(img, points)
# 转为numpy 格式的数组
face_descriptor = [f for f in face_descriptor]
face_descriptor = np.array(face_descriptor, dtype=np.float64)
face_descriptor = np.reshape(face_descriptor, (1, -1))
# print(face_descriptor.shape)
return face_descriptor
if __name__ == '__main__':
# 提取人脸区域
path = './data/jm/1.jpg'
img = cv2.imread(path)
# 转为RGB 为了准确率
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 返回值:128D面部描述符
p = Eigenvector().fun1(img)
print(p.shape)
ao2.py
# 批量读取数据
import os
import cv2
import numpy as np
import ao1
def cv_show(neme, img):
cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def r_path_name(path):
list_img = os.listdir(path)
# 一个人的特征表
person = []
# 标签列表
lab = []
# 标签号
lab_n = 0
for i in range(len(list_img)):
list_img_name = os.listdir(path + list_img[i])
lab_n += 1
# 当前类别的特征集合
feature_list = None
for j in list_img_name:
path_n = path + list_img[i] + "/" + j
# 读取图片
img = cv2.imread(path_n)
# 转为RGB 为了提高准确率
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv_show('neme', img)
# 人脸特征向量
feat = ao1.Eigenvector().fun1(img)
# 如果识别到了人脸
if not feat is None:
if feature_list is None:
feature_list = feat
else:
# 特征列表 拼接矩阵 竖向合并
feature_list = np.concatenate((feature_list, feat), axis=0)
person.append(feature_list)
lab.append(lab_n)
return person, lab
if __name__ == '__main__':
path = "./data/jm/"
img, lab = r_path_name(path)
print(len(img))
print(len(lab))
# 保存路径
path_s = "./data/train"
# 保存csv
for i, j in zip(img, lab):
fo1 = open(f"{path_s}{j}.txt", "w", encoding="utf-8")
# 写入
fo1.write(str(i.tolist()))
# 关闭打开的文件
fo1.close()
NotADirectoryError: [WinError 267] 目录名称无效。: './data/jm/1.jpg'
|
|