Looking for support with face recognition

Saludes soy nuevo, estoy trabajando sobre reconocimiento de rostros y ando buscando apoyo

try again in english, please.

also, please explain, what you’ve tried, so far,
and what the current problem is

1 Like

programa
#…importar librerias
import os
import numpy as np
import cv2

#…importamos las fotos tomadas
nombre = “hola”
direccion = “C:/Users/valme/OneDrive/protocolo de escuelas/proyectos python/same/deteccion de rostro y cubre bocas/fotos”
lista = os.listdir(direccion)

etiquetas = []
rostros = []
cont = 0

for nameDir in lista:
nombre = direccion + “/” + nameDir # leer las fotos de los rostros

for fileName in os.listdir(nombre):
    etiquetas.append(cont) #asignamos las etiquetas
    rostros.append(cv2.imread(nombre + "/" + fileName, 0))

cont = cont + 1

print(etiquetas)

#…creamos el modelo
reconocimiento = cv2.face.LBPHFaceRecognizer_create()

#…Entrenamos el modelo
reconocimiento.train(rostros, np.array(etiquetas))

#…guardamos el modelo
reconocimiento.write(ModeloEntrenando.xml)
print(“Modelo Creado”)

error
Traceback (most recent call last):
File “C:\Users\valme\OneDrive\protocolo de escuelas\proyectos python\same\deteccion de rostro y cubre bocas\entrenador.py”, line 29, in
reconocimiento.train(rostros, np.array(etiquetas))
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix.cpp:250: error: (-215:Assertion failed) s >= 0 in function ‘cv::setSize’
Enviaste

Hello from Honduras, the problem I have is that I managed to take the photos but when I load to create the ´reconocimiento.train(rostros, np.array(etiquetas))´ I cannot create the model *.xml and that is why I am looking for it the support to see if they can upload my code and see if it’s a problem with my pc or a general problem and I can’t move on from there.
I put add where I’m going in the trainer and the error it gives me

i recommend you to use newer and better OpenCV: DNN-based Face Detection And Recognition

you probably tried to load an image, but it failed (it is empty / invalid)
please add a check after cv2.imread()

Thanks for the quick response and how would you go about letting the system know that there is an invalid file and move on from this

again, add a check after the imread() and only proceed if the image is not none

hello thank you very much with the guide you gave me I found the problems, but I don’t know how to write the instruction, add the code as I think it could be for verification use an imshow to see where that file was

if rostros == None: # wrong path ? not an image ?
rostros.append(cv2.imread(nombre + “/” + fileName, 0))
else:
continue

Greetings from Honduras bothering again.
I managed to make the part where I create the model work and it was what I got was a file that was not a photo.
but now I have 100 folders with photos ready when I run the file with 10 folders it works but when I load the *.xml file with the 100 folders it just doesn’t activate the camera for detection when it’s a small file it works.
it has opencv folder limits i am trying to use it for a small school to control biosecurity in children

# importar las librerias
import cv2
import mediapipe as mp
import os
import pyttsx3

engine = pyttsx3.init()
engine.setProperty("rate", 130)
engine.setProperty("voice", "spanish latin")


#  importar nomnbre de las carpetas
direccion = "C:/Users/valme/OneDrive/protocolo de escuelas/proyectos python/same/proyecto same/fotosP"

#direccion = "C:/Users/valme/OneDrive/protocolo de escuelas/proyectos python/same/deteccion de rostro y cubre bocas/fotos"
etiquetas = os.listdir(direccion)
print("Nombres", etiquetas)

# Llamas el modelo emtrenado
modelo = cv2.face.LBPHFaceRecognizer_create()

# ...............Declaracion del detector
detector=mp.solutions.face_detection  # detector
dibujo=mp.solutions.drawing_utils  # dibujo
# leer el modelo
modelo.read("modelP.xml")


# ...............realizamos la videocaptura
cap = cv2.VideoCapture(0)

# ...............Inicializamos los parametros de deteccion
with detector.FaceDetection(min_detection_confidence=0.75) as rostros:
       while True:
           # lectura de videocaptura
           ret, frame = cap.read()

           # eliminar el error de espejo
           frame = cv2.flip(frame, 1)
           copia = frame.copy()
           # eliminar el error de color
           rgb = cv2.cvtColor(copia, cv2.COLOR_BGR2RGB)
           crgb = rgb.copy()
           # deteccion de rostros
           resultado = rostros.process(crgb)

           # filtros de seguridad
           if resultado.detections is not None:
               for rostro in resultado.detections:
                   #dibujo.draw_detection(frame, rostro)

                   # print(rostro)
                   # extraemos el ancho y el alto de nuestra ventanas
                   al, an, _=frame.shape
                   # extrtaer xini, yini  ancho y alto
                   xi=rostro.location_data.relative_bounding_box.xmin
                   yi=rostro.location_data.relative_bounding_box.ymin
                   ancho=rostro.location_data.relative_bounding_box.width
                   alto=rostro.location_data.relative_bounding_box.height

                   # conversion a pixeles
                   xi=int (xi * an)
                   yi=int (yi * al)
                   ancho=int (ancho * an)
                   alto=int (alto * al)
                   xf=xi + ancho
                   yf=yi + alto

                   # extraccion de pixeles cara
                   cara=crgb[yi:yf, xi:xf]
                   # redimensionar las fotos
                   cara=cv2.resize (cara, (150, 200), interpolation=cv2.INTER_CUBIC)
                   cara=cv2.cvtColor (cara, cv2.COLOR_BGR2GRAY)
                   caraP = cara.copy()
                   # extraemos el ancho y el alto de nuestra ventanas
                   al, an, _ = copia.shape
                   # extrtaer xini, yini  ancho y alto
                   xi = rostro.location_data.relative_bounding_box.xmin
                   yi = rostro.location_data.relative_bounding_box.ymin
                   ancho = rostro.location_data.relative_bounding_box.width
                   alto = rostro.location_data.relative_bounding_box.height

                   # conversion a pixeles
                   xi = int(xi * an)
                   yi = int(yi * al)
                   ancho = int(ancho * an)
                   alto = int(alto * al)
                   xf = xi + ancho
                   yf = yi + alto
                   # realizar la prediccion
                   prediccion = modelo.predict(caraP)
                   t= 37
                   o=92
                   #mostrar resultado

                   print(prediccion)
                   print(etiquetas)
                   cv2.putText(copia, "".format(etiquetas[0]), (xi, yf+15), 1, 1.3, (20,100,255), 1, cv2.LINE_AA)
                   cv2.putText(copia, "t={} ,O={}% ".format(t,o), (xi, yf + 40), 1, 1.3, (0, 0, 255), 1,
                               cv2.LINE_AA)
                   cv2.rectangle(copia, (xi, yi), (xf, yf), (0,0,255), 2)


               #print(copia)
               # mostramos los fotogramas
               cv2.imshow("Reconocimiento Facial y CubreBocas", copia)

               # leyendo una tecla
               t = cv2.waitKey(1)
               if t == 27  :
                   break
      # engine.say("Listo Listo Listo fotos tomadas {} ahora colocate el cubreboca , presione [S] para continuar".format(etiquetas))
       #engine.runAndWait()
       #conti = input("¿Listo fotos tomadas {} ahora colocate el cubreboca , presione [S] para continuar")

cap.release()
cv2.destroyAllWindows()