• 大小: 115KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-08
  • 语言: Python
  • 标签: 完整代码  

资源简介

基于卷积神经网络和OpenCV的人脸识别系统_python实现,完整代码!

资源截图

代码片段和文件信息


from read_data import read_file
#from sklearn.model_selection import train_test_split
from sklearn.cross_validation import train_test_split
from keras.utils import np_utils
import random

#建立一个用于存储和格式化读取训练数据的类
class DataSet(object):
   def __init__(selfpath):
       self.num_classes = None
       self.X_train = None
       self.X_test = None
       self.Y_train = None
       self.Y_test = None
       self.img_size = 128
       self.extract_data(path)
       #在这个类初始化的过程中读取path下的训练数据

   def extract_data(selfpath):
        #根据指定路径读取出图片、标签和类别数
        imgslabelscounter = read_file(path)
        print(“输出标记“)
        print(labels)

        #将数据集打乱随机分组
        X_trainX_testy_trainy_test = train_test_split(imgslabelstest_size=0.2random_state=random.randint(0 100))
        print(“输出训练标记和训练集长度“)
        print(y_train)
        print(len(X_train))
        print(X_train[1])
        print(“测试长度和测试集标记“)
        print(len(X_test))
        print(y_test)
        print(“输出和“)
        print(counter)

        #重新格式化和标准化
        # 本案例是基于thano的,如果基于tensorflow的backend需要进行修改
        X_train = X_train.reshape(X_train.shape[0] 1 self.img_size self.img_size)/255.0
        X_test = X_test.reshape(X_test.shape[0] 1 self.img_size self.img_size) / 255.0

        X_train = X_train.astype(‘float32‘)
        X_test = X_test.astype(‘float32‘)
        print(X_train[1])

        #将labels转成 binary class matrices
        Y_train = np_utils.to_categorical(y_train num_classes=counter)
        Y_test = np_utils.to_categorical(y_test num_classes=counter)
        print(Y_train)
        #将格式化后的数据赋值给类的属性上
        self.X_train = X_train
        self.X_test = X_test
        self.Y_train = Y_train
        self.Y_test = Y_test
        self.num_classes = counter

   def check(self):
       print(‘num of dim:‘ self.X_test.ndim)
       print(‘shape:‘ self.X_test.shape)
       print(‘size:‘ self.X_test.size)

       print(‘num of dim:‘ self.X_train.ndim)
       print(‘shape:‘ self.X_train.shape)
       print(‘size:‘ self.X_train.size)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-03-07 09:33  faceRecognition\
     目录           0  2018-03-02 15:24  faceRecognition\.idea\
     目录           0  2018-03-02 15:24  faceRecognition\.idea\inspectionProfiles\
     文件           0  2018-01-22 15:25  faceRecognition\.idea\inspectionProfiles\profiles_settings.xml
     文件           0  2018-01-22 15:25  faceRecognition\.idea\workspace.xml
     文件        2393  2018-03-07 09:33  faceRecognition\dataSet.py
     文件      676709  2018-01-20 22:42  faceRecognition\haarcascade_frontalface_alt.xml
     文件        1466  2018-03-06 14:57  faceRecognition\pick_face.py
     文件          56  2018-03-06 14:57  faceRecognition\README.md
     文件        2043  2018-03-06 14:48  faceRecognition\read_camera.py
     文件        1580  2018-03-06 14:21  faceRecognition\read_data.py
     文件        1159  2018-03-06 14:22  faceRecognition\read_img.py
     文件        1207  2018-03-06 14:57  faceRecognition\test_model.py
     文件        3754  2018-03-06 14:21  faceRecognition\train_model.py
     目录           0  2018-03-07 09:33  faceRecognition\__pycache__\
     文件        1869  2018-01-22 16:01  faceRecognition\__pycache__\dataSet.cpython-35.pyc
     文件        1812  2018-03-07 09:33  faceRecognition\__pycache__\dataSet.cpython-36.pyc
     文件        1255  2018-01-22 15:40  faceRecognition\__pycache__\read_data.cpython-35.pyc
     文件        1094  2018-03-06 14:21  faceRecognition\__pycache__\read_data.cpython-36.pyc
     文件         985  2018-01-20 22:47  faceRecognition\__pycache__\read_img.cpython-35.pyc
     文件         868  2018-03-06 14:25  faceRecognition\__pycache__\read_img.cpython-36.pyc
     文件        3335  2018-01-28 12:06  faceRecognition\__pycache__\train_model.cpython-35.pyc
     文件        3017  2018-03-06 14:25  faceRecognition\__pycache__\train_model.cpython-36.pyc

评论

共有 条评论