Serie Jetson nano combat: reconocimiento facial basado en la biblioteca face_recognition

0, orden

  face_recognition afirma ser el proyecto de reconocimiento facial más poderoso y simple de la historia. Se informa que el proyecto fue desarrollado por Adam Geitgey, un desarrollador y consultor de ingeniería de software. Su fortaleza no solo se basa en el modelo de aprendizaje profundo en la biblioteca de código abierto C ++ líder en la industria, dlib, sino también en el conjunto de datos faciales utilizados por la Universidad. of Massachusetts Amn Laked Faces in the Wild, producido por la Universidad de Manchester, contiene más de 13.000 imágenes faciales recopiladas de Internet, con una tasa de precisión del 99,38%. Además, el proyecto también está equipado con documentos de desarrollo completos y casos de aplicación, especialmente compatibles con el sistema Raspberry Pi. La simplicidad es que el operador puede usar directamente Python y herramientas de línea de comandos para extraer, reconocer y manipular rostros humanos.
El portal del proyecto github: https://github.com/ageitgey/face_recognition Los
  siguientes recursos de procesamiento de imágenes relacionados con la demostración se obtienen a través de opencv para obtener los datos de imagen en tiempo real de la cámara, y luego opencv y los requisitos relacionados relacionados para La operación opencv se ha instalado de forma predeterminada. Algunas bibliotecas y componentes dependientes.

1. Preparación ambiental

1.1. Instale la biblioteca face_recognition:

pip3 install face_recognition

1.2. Instale la biblioteca dlib:

wget http://dlib.net/files/dlib-19.17.tar.bz2
tar jxvf dlib-19.17.tar.bz2
cd dlib-19.17

修改在dlib/cuda/目录下的该 cudnn_dlibapi.cpp文件中
gedit dlib/cuda/cudnn_dlibapi.cpp
找到对应的一行代码,进行删除(可以采用注释的方式删除)
forward_algo = forward_best_algo;
//forward_algo = forward_best_algo;

编译安装
sudo python3 setup.py install

1.3. Instalar pepinillos

pip3 install pickle

2 、 Codificación

Inserte la descripción de la imagen aquí

2.1. Registro facial y datos de clasificación guardados

import face_recognition
import cv2
import os
import pickle
print(cv2.__version__)

Encodings=[]
Names=[]

image_dir='/home/colin/works/face_recognition_dlib/face_register'
for root, dirs, files in os.walk(image_dir):
    print(files)
    for file in files:
        path=os.path.join(root,file)
        print(path)
        name=os.path.splitext(file)[0]
        print(name)
        person=face_recognition.load_image_file(path)
        encoding=face_recognition.face_encodings(person)[0]
        Encodings.append(encoding)
        Names.append(name)
print(Names)

with open('train.pkl', 'wb') as f:
    pickle.dump(Names, f)
    pickle.dump(Encodings, f)

2.2, reconocimiento facial en tiempo real

import os
import face_recognition
import cv2
import time
import numpy as np
import pickle
import image_shop

save_switch = 0
catch_max = 50
Encodings=[]
Names=[]
font = cv2.FONT_HERSHEY_SIMPLEX

#640 480 320 240
def gstreamer_pipeline(
    capture_width=640,
    capture_height=480,
    display_width=640,
    display_height=480,
    framerate=30,
    flip_method=0,
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
)

def train_data_load( ):
    global Names, Encodings

    with open('train.pkl','rb') as f:
        Names=pickle.load(f)
        Encodings=pickle.load(f)


def catch_face(startX, endX, startY, endY, image):
    global save_switch
    face_pic = image[startY -50: endY + 50, startX-50 : endX + 50]
    face_name = 'Unkown'

    if face_pic.size > 0:
        face_name = face_recognition_frame(face_pic)
    return face_name


def face_recognition_frame(frame):
    global Names, Encodings, font

    frameSmall =cv2.resize(frame,(0,0),fx=.25, fy=.25)

    frameRGB=cv2.cvtColor(frameSmall,cv2.COLOR_BGR2RGB)
    facePositions=face_recognition.face_locations(frameRGB,model='cnn')
    allEncodings=face_recognition.face_encodings(frameRGB,facePositions)
    
    for (top,right,bottom,left),face_encoding in zip(facePositions,allEncodings):
        name='Unkown'
        matches=face_recognition.compare_faces(Encodings,face_encoding, tolerance=0.5)
        if True in matches:
            #first_match_index=matches.index(True)
            #name=Names[first_match_index]
            face_distance = face_recognition.face_distance(Encodings, face_encoding)
            best_match_index = np.argmin(face_distance)
            name = Names[best_match_index]

        top     = top * 4
        right   = right * 4
        bottom  = bottom * 4
        left    = left * 4

        if top - 6 > 10:
            mark_loc = (left, top-6)
        else:
            mark_loc = (left, bottom-6)

        if name == 'Unkown':
            cv2.rectangle(frame, (left, top), (right, bottom), (0,0,255), 2)
            cv2.putText(frame, name, mark_loc, font, .5, (255,0,0), 2)
        else:
            cv2.rectangle(frame, (left, top), (right, bottom), (0,255,0), 2)
            cv2.putText(frame, name, mark_loc, font, .5, (0,0,255), 2)
        
            
        frame = image_shop.mark_add(left, right, top, bottom, frame)  
          
        #return name
    resImage = frame
    return resImage


def CatchVideo(window_name, camera_idx):
	
	global save_switch, catch_switch, catch_max

	cv2.namedWindow(window_name)

	#CSI Camera for get pipeline
	cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=camera_idx), cv2.CAP_GSTREAMER)

	while cap.isOpened():
		ok, frame = cap.read() #read 1 frame
		if not ok:
			break

		resImage = face_recognition_frame(frame)

		#display
		cv2.imshow(window_name, resImage)
		c = cv2.waitKey(1)
		if c & 0xFF == ord('q'):
			break
 
	#close
	cap.release()
	cv2.destroyAllWindows()


if __name__ == '__main__':

    print('main menu:\n\t1 -reload face register\n\t2 -run\n\t3 -exit\n')
    choose = input("input your choose:")
    train_data_load()
    CatchVideo("Find face", 0)

3. Efecto de demostración

Inserte la descripción de la imagen aquí

Apéndice de referencia

1) También hay un gran hombre en Youtube que está haciendo videos de aprendizaje de hardware de código abierto relacionado como jetson nano
https://toptechboy.com/
2) El proyecto de reconocimiento facial más simple de la historia está en la lista de tendencias de GitHub

Supongo que te gusta

Origin blog.csdn.net/qq_33475105/article/details/111994247
Recomendado
Clasificación