コード例 #1
0
class Camera_reader(object):
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128

    def build_camera(self):
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
        name_list = read_name_list("C:\\Users\\jimmychen\\Desktop\\chernger\\chernger_faceRecognition\\dataset")

        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
            faces = face_cascade.detectMultiScale(gray, 1.3, 5) 
            for (x, y, w, h) in faces:
                ROI = gray[x:x + w, y:y + h]
                ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR)
                label,prob = self.model.predict(ROI)
                if prob > 0.7:   
                    show_name = name_list[label]
                    cv2.putText(frame, show_name + "{}".format(prob), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                    frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) 
                else:
                    show_name = 'Stranger'
                    cv2.putText(frame, show_name + "{}".format(prob), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
                    frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)    
            frame = cv2.flip(frame, 1)
            cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #2
0
class Camera_reader(object):

    #Load model
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128


    def build_camera(self):        
        face_cascade = cv2.CascadeClassifier('D:\proj\opencv-master\opencv-master\data\haarcascades\haarcascade_frontalface_alt.xml')
        #Read the subfolder in dataset folder
        name_list = read_name_list('D:\proj\dataset')

        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
             success, frame = cameraCapture.read()
             gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
             faces = face_cascade.detectMultiScale(gray, 1.3, 5)
             for (x, y, w, h) in faces:
                 ROI = gray[x:x + w, y:y + h]
                 ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR)
                 label,prob = self.model.predict(ROI)
                 if prob >0.7:    #Only output higher than 70% label
                     show_name = name_list[label]
                 else:
                     show_name = 'Stranger'
                 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
                 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
             cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #3
0
ファイル: read_camera.py プロジェクト: jerry1900/Practice
class Camera_reader(object):
    #在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()


    def build_camera(self):
        #
        face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml')
        name_list = read_name_list('D:\myProject\pictures\dataset')

        #打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
             success, frame = cameraCapture.read()
             gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
             faces = face_cascade.detectMultiScale(gray, 1.3, 5)
             for (x, y, w, h) in faces:
                 ROI = gray[x:x + w, y:y + h]
                 ROI = cv2.resize(ROI, (64, 64), interpolation=cv2.INTER_LINEAR)
                 label,prob = self.model.predict(ROI)  #利用模型对cv2识别出的人脸进行比对
                 if prob >0.95:    #如果模型认为概率高于95%则显示为模型中已有的label
                     show_name = name_list[label]
                 else:
                     show_name = 'Stranger'
                 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)  #显示名字
                 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  #在人脸区域画一个正方形出来
             cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #4
0
    def __init__(self):
        self.selector_rf = None
        self.selector_svm = None
        self.x_selected_rf = None
        self.x_selected_svm = None
        self.rfc = RandomForestClassifier(
            criterion='gini',
            n_estimators=191,
            max_depth=13
            # , max_features=6
            ,
            min_samples_leaf=1,
            random_state=30)
        self.svc = SVC(kernel='linear',
                       C=0.007564633275546291,
                       tol=0.0001,
                       cache_size=1000,
                       random_state=30)
        self.dh = DataHandle('data/phone_data.csv')

        self.dh.read_data('data/smote_rf_data.csv')
        self.model_rf = Model(self.dh.get_data(), self.dh.get_target())

        self.dh.read_data('data/smote_svm_data.csv')
        self.model_svm = Model(self.dh.get_data(), self.dh.get_target())
コード例 #5
0
ファイル: main.py プロジェクト: tpoljak/BERT_RESSEL
def evaluate_model(args):
    hparams = PARAMS_MAP[args.model]
    hparams["pickle_dir"] = args.eval_pickle
    hparams = collections.namedtuple("HParams",
                                     sorted(hparams.keys()))(**hparams)
    model = Model(hparams)
    print(hparams.init_checkpoint)
    model.analysis_evaluate(saved_file=args.evaluate)
コード例 #6
0
 def __init__(self):
     self.model = Model(read_save=True)
     self.model.load()
     self.img_size = 128
     #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
     self.face_cascade = cv2.CascadeClassifier(
         "haarcascade_frontalface_default.xml")
     #读取dataset数据集下的子文件夹名称
     self.name_list = read_name_list(dataset_path)
コード例 #7
0
ファイル: read_camera.py プロジェクト: brave-orange/faceme
 def __init__(self):
     self.model = Model()
     self.model.load()
     self.img_size = 128
     classname = 0
     titlename = "untitled (hzy_order) - Sublime Text (UNREGISTERED)"
     #获取句柄
     self.hwnd = win32gui.FindWindow(classname, titlename)
     print("得到句柄")
コード例 #8
0
ファイル: main.py プロジェクト: shawn3298317/cv_practical
def load_model(fn):
    m = Model(filter_sizes=[3, 3, 2, 2],
              kernel_nums=[10, 10, 10, 10],
              strides=[1, 1, 1, 1],
              batch_size=50,
              training_mode=False)
    m.build_model()
    m.model.load_weights(fn)
    return m
コード例 #9
0
def test_onePicture(path):
    model = Model()
    model.load()

    color = (0, 255, 0)
    img1 = cv2.imread(path)
    img = cv2.resize(img1, (128, 128))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    classfier = cv2.CascadeClassifier(
        "C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml"
    )
    faceRects = classfier.detectMultiScale(img1,
                                           scaleFactor=1.2,
                                           minNeighbors=3,
                                           minSize=(32, 32))

    if len(faceRects) > 0:  # 大于0则检测到人脸
        for faceRect in faceRects:  # 单独框出每一张人脸
            x, y, w, h = faceRect
            cv2.rectangle(img1, (x - 10, y - 10), (x + w + 10, y + h + 10),
                          color, 3)  # 5控制绿色框的粗细
        picType, prob = model.predict(img)
        if picType != -1:
            name_list = read_name_list('dataset')
            info = name_list[picType].split('_')
            infoName = 'Name:' + info[0]
            infoAge = 'Age:' + info[1]
            infoUniversity = 'University:' + info[2]
            infoSex = 'Sex:' + info[3]
            infoDrivingLN = 'Driving number:' + info[4]
            cv2.rectangle(img1, (0, 0), (300, 255), color, 3)  # 5控制绿色框的粗细
            cv2.putText(img1, "Driver Info", (80, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2)  # 显示性别
            cv2.putText(img1, infoDrivingLN, (0, 60), cv2.FONT_HERSHEY_SIMPLEX,
                        0.8, 255, 2, 2)  # 显示驾驶证号码
            cv2.putText(img1, infoName, (0, 100), cv2.FONT_HERSHEY_SIMPLEX,
                        0.8, 255, 2, 2)  # 显示名字
            cv2.putText(img1, infoAge, (0, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                        255, 2, 2)  # 显示年龄
            cv2.putText(img1, infoUniversity, (0, 180),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2)  # 显示学历
            cv2.putText(img1, infoSex, (0, 220), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                        255, 2, 2)  # 显示性别
            cv2.imshow("Camera", img1)
            # cv2.imshow(name_list[picType],img1)
            cv2.waitKey(0)
            print(name_list[picType], prob)
        else:
            cv2.imshow(" Don't know this person", img1)
            cv2.waitKey(0)
            print(" Don't know this person")
    else:
        cv2.imshow("No Face in this picture !!!", img1)
        cv2.waitKey(0)
        print("No Face in this picture")
コード例 #10
0
ファイル: main.py プロジェクト: tpoljak/BERT_RESSEL
def train_model(args):
    hparams = PARAMS_MAP[args.model]
    timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
    root_dir = os.path.join(hparams["root_dir"], "%s/" % timestamp)
    logger = init_logger(root_dir)
    logger.info("Hyper-parameters: %s" % str(hparams))
    hparams["root_dir"] = root_dir

    hparams = collections.namedtuple("HParams",
                                     sorted(hparams.keys()))(**hparams)
    model = Model(hparams)
    model.train(pretrained_file=args.pretrained)
コード例 #11
0
ファイル: test_model.py プロジェクト: qqqqll/faceRecognition
def test_onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (64, 64))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        name_list = read_name_list('D:\myProject\pictures\dataset')
        print name_list[picType], prob
    else:
        print " Don't know this person"
コード例 #12
0
def test_onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (128, 128))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        name_list = read_name_list(r'E:\cut-face')
        print(name_list[picType], prob)
    else:
        print("Don't know this person")
コード例 #13
0
def test_onePicture(path):
    model= Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (128, 128))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType,prob = model.predict(img)
    if picType != -1:
        name_list = read_name_list('/Users/gaoxingyun/Documents/uw/courses/Sp19/EE576_CV/project/realtime_emotion_recognition/dataset')
        print (name_list[picType],prob)
    else:
        print (" Don't know this person")
コード例 #14
0
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128
        print("init")
        # 摄像头初始化  加快使用速度
        self.face_cascade = cv2.CascadeClassifier(
            './haarcascade_frontalface_alt.xml')
        # 读取dataset数据集下的子文件夹名称
        self.name_list = read_name_list('res')

        # 打开摄像头并开始读取画面
        self.cameraCapture = cv2.VideoCapture(0)
コード例 #15
0
class Camera_reader(object):
    # 在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128

    def build_camera(self):
        # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                             'haarcascade_frontalface_alt.xml')
        # 读取dataset数据集下的子文件夹名称,即标签名
        name_list = read_name_list('./dataset')

        # 打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture('recognize_faces2.mp4')
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            # 图像灰度化
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # 检测人脸:scaleFactor是放大比例(此参数必须大于1);minNeighbors是重复识别次数(此参数用来调整精确度,越大则越精确)
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.3,
                                                  minNeighbors=8)
            for (x, y, w, h) in faces:
                origin = gray[x:x + w, y:y + h]
                # origin是原图,self.img_size是输出图像的尺寸,interpolation是插值方法
                origin = cv2.resize(origin, (self.img_size, self.img_size),
                                    interpolation=cv2.INTER_LINEAR)
                # 利用模型对cv2识别出的人脸进行比对
                label, prob = self.model.predict(origin)
                # 如果模型认为概率高于70%则显示为模型中已有的label
                if prob > 0.7:
                    show_name = name_list[label]
                else:
                    show_name = 'unknown'

                # 框出人脸
                frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (255, 0, 0), 1)
                # 显示人名
                cv2.putText(frame, show_name, (x, y - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)

            cv2.imshow("Recognizing...", frame)
        # 释放摄像头
        cameraCapture.release()
        # 关闭所有窗口
        cv2.destroyAllWindows()
コード例 #16
0
def test_onBatch(path):
    model = Model()
    model.load()
    index = 0
    img_list, label_lsit, counter = read_file(path)
    for img in img_list:
        picType, prob = model.predict(img)
        if picType != -1:
            index += 1
            print(labbel[picType], prob)
        else:
            print("invaild person")

    return index
コード例 #17
0
def test_onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (64, 64))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        name_list = read_name_list(
            'C:\\Users\\jimmychen\\Desktop\\chernger\\chernger_faceRecognition\\dataset'
        )
        print(name_list[picType], prob)
    else:
        print(" Don't know this person")
コード例 #18
0
def test_onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (128, 128))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        # name_list = read_name_list('D:\myProject\pictures\dataset')
        name_list = read_name_list(
            r'D:/my_laboratory/face_detection20180516/dataset')
        print(name_list[picType], prob)
    else:
        print(" Don't know this person")
コード例 #19
0
    def __init__(self):
        self.model = Model()
        #self.model.load()
        self.img_size = 128
        if os.path.exists("images\\yjd_sc"):
            shutil.rmtree("images\\yjd_sc")
        if os.path.exists("images\\yjd_deal"):
            shutil.rmtree("images\\yjd_deal")
        if os.path.exists("dataset\\yjd_21_NEU_boy_001231"):
            shutil.rmtree("dataset\\yjd_21_NEU_boy_001231")

        os.mkdir("images\\yjd_sc")
        os.mkdir("images\\yjd_deal")
        os.mkdir("dataset\\yjd_21_NEU_boy_001231")
コード例 #20
0
def test_onBatch(path):
    model = Model()
    model.load()
    index = 0
    img_list, label_lsit, counter = read_file(path)
    for img in img_list:
        picType, prob = model.predict(img)
        if picType != -1:
            index += 1
            name_list = read_name_list('./image/trainfaces')
            print(name_list[picType])
        else:
            print(" Don't know this person")
    return index
コード例 #21
0
class Camera_reader(object):
    #在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128

    def build_camera(self):
        #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
        face_cascade = cv2.CascadeClassifier(
            '/usr/local/lib/python2.7/site-packages/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_alt.xml'
        )
        #读取dataset数据集下的子文件夹名称
        name_list = read_name_list(
            '/home/hezhiqiang/PycharmProjects/pictures/dataset')

        #打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture(0)
        nextCaptureTime = time.time()
        faces = []
        if not cameraCapture.isOpened():
            print('Capture failed because of camera')
        success, frame = cameraCapture.read()
        while 1:
            # while success and cv2.waitKey(1) == -1:
            #     print '1'
            success, frame = cameraCapture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  #图像灰化
            # if nextCaptureTime < time.time():
            #     nextCaptureTime = time.time() + 0.1
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)  #识别人脸
            for (x, y, w, h) in faces:

                ROI = gray[x:x + w, y:y + h]
                ROI = cv2.resize(ROI, (self.img_size, self.img_size),
                                 interpolation=cv2.INTER_LINEAR)
                label, prob = self.model.predict(ROI)  #利用模型对cv2识别出的人脸进行比对
                if prob > 0.7:  #如果模型认为概率高于70%则显示为模型中已有的label
                    show_name = name_list[label]
                else:
                    show_name = 'Stranger'
                cv2.putText(frame, show_name, (x, y - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)  #显示名字
                frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (255, 0, 0), 2)  #在人脸区域画一个正方形出来
            cv2.imshow("MyWindow", frame)
            if cv2.waitKey(1) & 0xFF == 27: break
        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #22
0
ファイル: test_model.py プロジェクト: qqqqll/faceRecognition
def test_onBatch(path):
    model = Model()
    model.load()
    index = 0
    img_list, label_lsit, counter = read_file(path)
    for img in img_list:
        picType, prob = model.predict(img)
        if picType != -1:
            index += 1
            name_list = read_name_list('D:\myProject\pictures\dataset')
            print name_list[picType]
        else:
            print " Don't know this person"

    return index
コード例 #23
0
def test_onBatch(path):
    model= Model()
    model.load()
    index = 0
    img_list, label_lsit, counter = read_file(path)
    for img in img_list:
        picType,prob = model.predict(img)
        if picType != -1:
            index += 1
            name_list = read_name_list('/Users/gaoxingyun/Documents/uw/courses/Sp19/EE576_CV/project/faceRecognition/dataset')
            print (name_list[picType])
        else:
            print (" Don't know this person")

    return index
コード例 #24
0
def test_onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    #print(img)
    img = cv2.resize(img, (128, 128))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        #print(picType,prob)
        #name_list = read_name_list('../myAIlab/pictures/dataset')
        #print(name_list[picType],prob)
        print(labbel[picType], prob)
    else:
        print("invaild person")
コード例 #25
0
def onePicture(path):
    model = Model()
    model.load()
    img = cv2.imread(path)
    img = cv2.resize(img, (92, 112))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    picType, prob = model.predict(img)
    if picType != -1:
        name_list = read_name_list('res')
        print(name_list)
        print(picType)
        print(name_list[picType], prob)
        print("done")
    else:
        print(" Don't know this person")
コード例 #26
0
def test_Pictures(path):
    model = Model()
    model.load()
    for child_dir in os.listdir(path):
        if endwith(child_dir, 'jpg'):  # 找到以jpg结尾的文件路径
            str = os.path.join(path, child_dir)  # 具体的图片文件的路径
            img = cv2.imread(str)
            resized_img = cv2.resize(img, (128, 128),
                                     interpolation=cv2.INTER_CUBIC)
            img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY)
            picType, prob = model.predict(img)
            if picType != -1:
                name_list = read_name_list('pictures/dataset')
                print(name_list[picType], prob)
            else:
                print(" Don't know this person")
コード例 #27
0
class Camera_reader(object):
    # 在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128

    def build_camera(self):
        # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
        face_cascade = cv2.CascadeClassifier(
            r'C:\Users\01436179\PycharmProjects\pyhton_face\haarcascade_frontalface_alt.xml'
        )
        # 读取dataset数据集下的子文件夹名称
        name_list = read_name_list(
            r'C:\Users\01436179\PycharmProjects\pyhton_face\dataset')

        # 打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # 图像灰化
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            # 识别人脸
            for (x, y, w, h) in faces:
                ROI = gray[x:x + w, y:y + h]
                ROI = cv2.resize(ROI, (self.img_size, self.img_size),
                                 interpolation=cv2.INTER_LINEAR)
                label, prob = self.model.predict(ROI)
                # 利用模型对cv2识别出的人脸进行比对
                if prob > 0.7:
                    # 如果模型认为概率高于70%则显示为模型中已有的label
                    show_name = name_list[label]
                else:
                    show_name = 'Stranger'
                cv2.putText(frame, show_name, (x, y - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
                #  显示名字
                frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (255, 0, 0), 2)
                # 在人脸区域画一个正方形出来
            cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #28
0
ファイル: read_camera.py プロジェクト: brave-orange/faceme
class Camera_reader(object):
    #在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128
        classname = 0
        titlename = "untitled (hzy_order) - Sublime Text (UNREGISTERED)"
        #获取句柄
        self.hwnd = win32gui.FindWindow(classname, titlename)
        print("得到句柄")

    def build_camera(self):
        #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸C:\\Users\\jan\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\
        face_cascade = cv2.CascadeClassifier(
            'C:\\Users\\jan\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml'
        )
        #读取dataset数据集下的子文件夹名称
        name_list = read_name_list('E:\\WWW\\faceRecognition\\dataset')

        #打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  #图像灰化
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)  #识别人脸
            for (x, y, w, h) in faces:
                ROI = gray[x:x + w, y:y + h]
                ROI = cv2.resize(ROI, (self.img_size, self.img_size),
                                 interpolation=cv2.INTER_LINEAR)
                label, prob = self.model.predict(ROI)  #利用模型对cv2识别出的人脸进行比对
                if prob > 0.7:  #如果模型认为概率高于70%则显示为模型中已有的label
                    show_name = name_list[label]
                    win32gui.SetForegroundWindow(self.hwnd)
                    print("got you %s " % show_name)
                    win32gui.ShowWindow(self.hwnd, win32con.SW_MAXIMIZE)
                else:
                    show_name = 'Stranger'
                #cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)  #显示名字
                #frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  #在人脸区域画一个正方形出来
                time.sleep(3)
            #cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()
コード例 #29
0
class Camera_reader(object):

    #在初始化camera的时候建立模型,并加载已经训练好的模型
    def __init__(self):
        self.model = Model()
        self.model.load()
        self.img_size = 128

    def build_camera(self):

        #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
        face_cascade = cv2.CascadeClassifier(
            'C:\\Face recognition\\classifier\\haarcascade_frontalface_alt2.xml'
        )
        #读取dataset数据集下的子文件夹名称
        name_list = read_name_list('C:\\Face recognition\\data')

        #打开摄像头并开始读取画面
        cap = cv2.VideoCapture(0)
        ret, frame = cap.read()  #读取一帧视频
        while ret and cv2.waitKey(10) == -1:
            ret, frame = cap.read()
            try:
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  #图像灰化
                faces = face_cascade.detectMultiScale(
                    gray, scaleFactor=1.2, minNeighbors=3,
                    minSize=(32, 32))  #识别人脸,******

                for (x, y, w, h) in faces:
                    ROI = gray[y - 10:y + h + 10, x - 10:x + w + 10]
                    ROI = cv2.resize(ROI, (self.img_size, self.img_size),
                                     interpolation=cv2.INTER_LINEAR)
                    label, prob = self.model.predict(ROI)  #利用模型对cv2识别出的人脸进行比对
                    if prob > 0.90:  #如果模型认为概率高于95%则显示为模型中已有的label
                        show_name = name_list[label]
                    else:
                        show_name = 'Stranger'
                    cv2.putText(frame, show_name, (x, y - 20),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)  #显示名字
                    frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                          (255, 0, 0), 2)  #在人脸区域画一个正方形
                cv2.imshow("Camera", frame)
            except:
                continue
        cap.release()
        cv2.destroyAllWindows()
        return show_name
コード例 #30
0
def test_onePicture(path):
    # 加载模型
    model = Model()
    model.load()
    # 读取图片
    img = cv2.imread(path)
    # 重置图片尺寸为:128*128
    img = cv2.resize(img, (128, 128))
    # 图片灰度化
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # labelIndex为概率最高的label的索引号, prob为对应概率
    labelIndex, prob = model.predict(img)
    if labelIndex != -1:
        name_list = read_name_list('./dataset')
        print(name_list[labelIndex], prob)
    else:
        print("Don't know this person.")