0% found this document useful (0 votes)
6 views4 pages

Codigo Visual Studio Code y Python

The document contains Python code for a face detection and recognition system using OpenCV and face_recognition libraries. It sets up a directory for storing detected faces, processes images from a specified path, and recognizes faces from a live camera feed. The code includes error handling for camera access and displays recognized faces with their names on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Codigo Visual Studio Code y Python

The document contains Python code for a face detection and recognition system using OpenCV and face_recognition libraries. It sets up a directory for storing detected faces, processes images from a specified path, and recognizes faces from a live camera feed. The code includes error handling for camera access and displays recognized faces with their names on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CODIGO VISUAL STUDIO CODE

DETECTOR DE CARA

import cv2

import os

imagesPath = "C:/Users/HP PAVILION GAMING/Desktop/face_recognition_models-master/images"

if not os.path.exists("faces"):

os.makedirs("faces")

print("Nueva carpeta: faces")

# Detector facial

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

count = 0

for imageName in os.listdir(imagesPath):

print(imageName)

image = cv2.imread(imagesPath + "/" + imageName)

faces = faceClassif.detectMultiScale(image, 1.1, 5)

for (x, y, w, h) in faces:

#cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

face = image[y:y + h, x:x + w]

face = cv2.resize(face, (150, 150))

cv2.imwrite("faces/" + str(count) + ".jpg", face)

count += 1

#cv2.imshow("face", face)

#cv2.waitKey(0)
#cv2.imshow("Image", image)

#cv2.waitKey(0)

#cv2.destroyAllWindows()

CODIGO PARA RECONOCIMIENTO FACIAL


import cv2

import os

import face_recognition

import urllib.request

import numpy as np

# URL de la cámara del ESP32

url = 'http://192.168.213.189/cam-hi.jpg'

# Ruta de imágenes con rostros conocidos

imageFacesPath = "C:/Users/HP PAVILION GAMING/Desktop/face_recognition_models-


master/faces"

facesEncodings = []

facesNames = []

# Cargar y codificar rostros conocidos

for file_name in os.listdir(imageFacesPath):

image = cv2.imread(imageFacesPath + "/" + file_name)

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Se asume que el rostro está en (0, 150, 150, 0), puede ajustarse

f_coding = face_recognition.face_encodings(image, known_face_locations=[(0, 150, 150, 0)])[0]

facesEncodings.append(f_coding)

facesNames.append(file_name.split(".")[0])
while True:

try:

# Obtener imagen desde la URL del ESP32 (formato MJPEG)

imgResponse = urllib.request.urlopen(url, timeout=2)

imgNp = np.array(bytearray(imgResponse.read()), dtype=np.uint8)

orig = cv2.imdecode(imgNp, -1) # Decodificar la imagen

except Exception as e:

print("⚠ No se pudo acceder a la cámara del ESP32. Verifica la conexión.")

continue

# Procesar la imagen

orig = cv2.flip(orig, 1)

faces = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")\

.detectMultiScale(orig, 1.1, 5)

for (x, y, w, h) in faces:

face = orig[y:y + h, x:x + w]

face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)

try:

actual_face_encoding = face_recognition.face_encodings(face, known_face_locations=[(0,


w, h, 0)])[0]

result = face_recognition.compare_faces(facesEncodings, actual_face_encoding)

if True in result:

index = result.index(True)

name = facesNames[index]

color = (125, 220, 0)

else:
name = "Desconocido"

color = (50, 50, 255)

# Dibujar resultados en pantalla

cv2.rectangle(orig, (x, y + h), (x + w, y + h + 30), color, -1)

cv2.rectangle(orig, (x, y), (x + w, y + h), color, 2)

cv2.putText(orig, name, (x, y + h + 25), 2, 1, (255, 255, 255), 2, cv2.LINE_AA)

except IndexError:

continue

# Mostrar la imagen con reconocimiento facial

cv2.imshow("ESP32 Camera", orig)

# Salir con tecla ESC

if cv2.waitKey(1) & 0xFF == 27:

break

cv2.destroyAllWindows()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy