Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import face_recognition
- import cv2
- import numpy as np
- # Obtenemos la camara
- video_capture = cv2.VideoCapture(0)
- # Cargamos una imagen y aprendemos a reconocerla
- obama_image = face_recognition.load_image_file("obama.jpg")
- obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
- # Cargamos una segunda imagen y aprendemos a reconocer
- biden_image = face_recognition.load_image_file("biden.jpg")
- biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
- # Creamos los nombres de las caras a reconcer
- known_face_encodings = [
- obama_face_encoding,
- biden_face_encoding
- ]
- known_face_names = [
- "Barack Obama",
- "Joe Biden"
- ]
- while True:
- # Grabamos un video
- ret, frame = video_capture.read()
- # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
- rgb_frame = frame[:, :, ::-1]
- # Convierta la imagen de color BGR (que utiliza OpenCV) a color RGB (que utiliza face_recognition)
- face_locations = face_recognition.face_locations(rgb_frame)
- face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
- # Recorre cada cara en este cuadro de video
- for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
- # Vea si la cara coincide con las caras conocidas
- matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
- name = "Unknown"
- # Si se encontró una coincidencia en face_encodings conocidos, solo use la primera.
- # si es cierto en los partidos:
- # first_match_index = match.index (True)
- # name = known_face_names[first_match_index]
- # O en su lugar, use la cara conocida con la menor distancia a la nueva cara
- face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
- best_match_index = np.argmin(face_distances)
- if matches[best_match_index]:
- name = known_face_names[best_match_index]
- # Dibuja un cuadro alrededor de la cara
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # Dibuja una etiqueta con un nombre debajo de la cara
- cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
- font = cv2.FONT_HERSHEY_DUPLEX
- cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
- # Mostrar la imagen resultante
- cv2.imshow('Video', frame)
- # ¡Presiona 'q' en el teclado para salir!
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Suelte el asa a la cámara web
- video_capture.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement