python face recognition verification

First, the code

Directly on the code, this case is modified in accordance with https://github.com/caibojian/face_login, the recognition rate is not very good, sometimes blocked half of the face or success

# -*- coding: utf-8 -*-
# __author__="maple"
"""
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃ animal bless ┣┓
                ┃ never BUG! ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
import base64
import cv2
import time
from io import BytesIO
from tensorflow import keras
from PIL import Image
from pymongo import MongoClient
import tensorflow as tf
import face_recognition
import numpy as np
#mongodb连接
conn = MongoClient('mongodb://root:123@localhost:27017/')
db = conn.myface   # connection mydb database, it does not automatically create 
user_face = db.user_face # use test_set collection, not automatically create 
face_images = db.face_images


lables = []
dates = []
INPUT_NODE = 128
LATER1_NODE = 200
OUTPUT_NODE = 0
TRAIN_DATA_SIZE = 0
TEST_DATA_SIZE = 0


def generateds():
    get_out_put_node()
    train_x, train_y, test_x, test_y = np.array(datas),np.array(lables),np.array(datas),np.array(lables)
    return train_x, train_y, test_x, test_y

def get_out_put_node():
    for item in face_images.find():
        lables.append(item['user_id'])
        datas.append(item['face_encoding'])
    OUTPUT_NODE = len(set(lables))
    TRAIN_DATA_SIZE = len(lables)
    TEST_DATA_SIZE = len(lables)
    return OUTPUT_NODE, TRAIN_DATA_SIZE, TEST_DATA_SIZE

# Verify the face information 
DEF predict_image (Image):
    model = tf.keras.models.load_model('face_model.h5',compile=False)
    face_encode = face_recognition.face_encodings(image)
    result = []
    for j in range(len(face_encode)):
        predictions1 = model.predict(np.array(face_encode[j]).reshape(1, 128))
        print(predictions1)
        if np.max(predictions1[0]) > 0.90:
            print(np.argmax(predictions1[0]).dtype)
            pred_user = user_face.find_one({'id': int(np.argmax(predictions1[0]))})
            print('第%d张脸是%s' % (j+1, pred_user['user_name']))
            result.append(pred_user['user_name'])
    return result

# Save the face information 
DEF save_face (PIC_PATH, uid):
    image = face_recognition.load_image_file(pic_path)
    face_encode = face_recognition.face_encodings(image)
    print(face_encode[0].shape)
    if(len(face_encode) == 1):
        face_image = {
            'user_id': uid,
            'face_encoding':face_encode[0].tolist()
        }
        face_images.insert_one(face_image)

# Training face information 
DEF train_face ():
    train_x, train_y, test_x, test_y = generateds()
    dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y))
    dataset = dataset.batch(32)
    dataset = dataset.repeat()
    OUTPUT_NODE, TRAIN_DATA_SIZE, TEST_DATA_SIZE = get_out_put_node()
    model = keras.Sequential([
        keras.layers.Dense(128, activation=tf.nn.relu),
        keras.layers.Dense(128, activation=tf.nn.relu),
        keras.layers.Dense(OUTPUT_NODE, activation=tf.nn.softmax)
    ])

    model.compile(optimizer=tf.compat.v1.train.AdamOptimizer(),
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
    steps_per_epoch  = 30
    if steps_per_epoch > len(train_x):
        steps_per_epoch = len(train_x)
    model.fit(dataset, epochs=10, steps_per_epoch=steps_per_epoch)

    model.save('face_model.h5')



DEF register_face (User):
     IF user_face.find ({ " USER_NAME " : User}) COUNT ()>. 0:
         Print ( " User already exists " )
         return 
    video_capture = cv2.VideoCapture (0)
     # used in MongoDB sort ( ) the method of sorting data, Sort () method may specify the sort fields parameter, and -1 is specified using the sort of way 1, wherein 1 is ascending, descending -1. 
    = user_face.find Finds (). Sort ([( " ID " , -1)]). limit (. 1 )
    uid = 0
    if finds.count() > 0:
        uid = finds[0]['id'] + 1
    print(uid)
    user_info = {
        'id': uid,
        'user_name': user,
        'create_time': time.time(),
        'update_time': time.time()
    }
    user_face.insert_one(user_info)

    the while . 1 :
         # acquires a video 
        RET, Frame = video_capture.read ()
         # window 
        cv2.imshow ( ' Connections Video ' , Frame)
         # adjusted angle shot five consecutive images 
        IF cv2.waitKey (. 1) == 0xFF & the ord ( ' Q ' ):
             for I in Range (1,6 ):
                cv2.imwrite('Myface{}.jpg'.format(i), frame)
                with open('Myface{}.jpg'.format(i),"rb")as f:
                    img=f.read()
                    img_data = BytesIO(img)
                    im = Image.open(img_data)
                    im = im.convert('RGB')
                    imgArray = np.array (im)
                    faces = face_recognition.face_locations(imgArray)
                    save_face('Myface{}.jpg'.format(i),uid)
            break

    train_face()
    video_capture.release()
    cv2.destroyAllWindows()


def rec_face():
    video_capture = cv2.VideoCapture (0)
     the while . 1 :
         # acquires a video 
        RET, Frame = video_capture.read ()
         # window 
        cv2.imshow ( ' Connections Video ' , Frame)
         # verification face photograph 5 
        IF cv2.waitKey ( . 1) == 0xFF & the ord ( ' Q ' ):
             for I in Range (1,6 ):
                cv2.imwrite('recface{}.jpg'.format(i), frame)
            break

    res = []
    for i in range(1, 6):
        with open('recface{}.jpg'.format(i),"rb")as f:
            img=f.read()
            img_data = BytesIO(img)
            im = Image.open(img_data)
            im = im.convert('RGB')
            imgArray = np.array (im)
            predict = predict_image(imgArray)
            if predict:
                res.extend(predict)

    B = SET (RES)   # {2,. 3} 
    IF len (B) ==. 1 and len (RES)> =. 3 :
         Print ( " authentication successful " )
     the else :
         Print ( " authentication failure " )

if __name__ == '__main__':
    register_face("maple")
    rec_face()

Guess you like

Origin www.cnblogs.com/angelyan/p/12113773.html