martes, 2 de junio de 2020

RECONOCIMIENTO FACIAL EN WINDOWS_MATERIAL COMPLEMENTARIO


RECONOCIMIENTO FACIAL EN WINDOWS
ESTEBAN QUISPE CHURATA
Aprovechando el momento, les envió saludos cordiales a sus familias.

En este documento adjunto material complementario, que se utiliza en el videotutorial didáctica, donde podrás realizar tu propio reconocimiento facial con mi ayuda.

Adjunto link de descarga para el archivo ZIP.

PRIMER PASO:  CAPTURAR ROSTROS CON NUESTRA CAMARA
CODIGO

#ESTEBAN QUISPE CHURATA
#importamos estas librerias necesarias para el codigo
import cv2
import os
import imutils

personName = 'ESTEBAN' #cambiar por el nombre que tengan almacenado en data
dataPath = 'C:/Users/esteb/Documents/reconocimiento facial/Data' #Cambia a la ruta donde hayas almacenado Data
personPath = dataPath + '/' + personName

if not os.path.exists(personPath):
print('Carpeta creada: ',personPath)
os.makedirs(personPath)

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) #para probar con webcam

faceClassif = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
count = 1200

while True:

ret, frame = cap.read()
if ret == False: break
frame =  imutils.resize(frame, width=640)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
auxFrame = frame.copy()

faces = faceClassif.detectMultiScale(gray,1.3,5)

for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),2)
rostro = auxFrame[y:y+h,x:x+w]
rostro = cv2.resize(rostro,(150,150),interpolation=cv2.INTER_CUBIC)
cv2.imwrite(personPath + '/rotro_{}.jpg'.format(count),rostro)
count = count + 1
cv2.imshow('frame',frame)

k =  cv2.waitKey(1)
if k == 27 or count >= 1300:
break

cap.release()
cv2.destroyAllWindows()

SEGUNDO PASO: ENTRENANDO NUESTRO PROPIO RECONOCIMIENTO DE ROSTROS
CODIGO

#ESTEBAN QUISPE CHURATA
#importamos estas librerias necesarias para el codigo
import cv2
import os
import numpy as np

dataPath = 'C:/Users/esteb/Documents/reconocimiento facial/Data' #Cambia a la ruta donde hayas almacenado Data
peopleList = os.listdir(dataPath)
print('Lista de personas: ', peopleList)

labels = []
facesData = []
label = 0

for nameDir in peopleList:
personPath = dataPath + '/' + nameDir
print('Leyendo las imágenes')

for fileName in os.listdir(personPath):
print('Rostros: ', nameDir + '/' + fileName)
labels.append(label)
facesData.append(cv2.imread(personPath+'/'+fileName,0))
image = cv2.imread(personPath+'/'+fileName,0)
cv2.imshow('image',image)
cv2.waitKey(10)
label = label + 1
#metodo de entrenamiento utilizado
face_recognizer = cv2.face.LBPHFaceRecognizer_create()

# Entrenando el reconocedor de rostros
print("Entrenando...")
face_recognizer.train(facesData, np.array(labels))

# Almacenando el modelo obtenido
face_recognizer.write('modeloLBPHFace.xml')
print("Modelo almacenado...")

TERCER PASO: PROBANDO NUESTRO RECONOCIMIENTO DE ROSTROS
CODIGO

#ESTEBAN QUISPE CHURATA
#importamos estas librerias necesarias para el codigo
import cv2
import os

dataPath = 'C:/Users/esteb/Documents/reconocimiento facial/Data' #Cambia a la ruta donde hayas almacenado Data
imagePaths = os.listdir(dataPath)
print('imagePaths=',imagePaths)

face_recognizer = cv2.face.LBPHFaceRecognizer_create()

# Leyendo el modelo

face_recognizer.read('modeloLBPHFace.xml')

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) #encendemos nuestra camara

faceClassif = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')

while True:
ret,frame = cap.read()
if ret == False: break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
auxFrame = gray.copy()

faces = faceClassif.detectMultiScale(gray,1.3,5)

for (x,y,w,h) in faces:
rostro = auxFrame[y:y+h,x:x+w]
rostro = cv2.resize(rostro,(150,150),interpolation= cv2.INTER_CUBIC)
result = face_recognizer.predict(rostro)

cv2.putText(frame,'{}'.format(result),(x,y-5),1,1.3,(255,255,0),1,cv2.LINE_AA)
# LBPHFace
if result[1] < 70:
cv2.putText(frame,'{}'.format(imagePaths[result[0]]),(x,y-25),2,1.1,(0,255,0),1,cv2.LINE_AA)
cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),2)
else:
cv2.putText(frame,'Desconocido',(x,y-20),2,0.8,(0,0,255),1,cv2.LINE_AA)
cv2.rectangle(frame, (x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('frame',frame)
k = cv2.waitKey(1)
if k == 27:
break

cap.release()
cv2.destroyAllWindows()




LINK DE DESCARGA PARA EL ARCHIVO ZIP:

0 comentarios :

Publicar un comentario

Social Profiles

Twitter Facebook Google Plus

Popular Posts

Con la tecnología de Blogger.

BTemplates.com

Blogroll

About

About

Copyright © Esteban Quispe Churata | Powered by Blogger
Design by Lizard Themes | Blogger Theme by Lasantha - PremiumBloggerTemplates.com