Face Recognition in Real Time
https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
installing_dlib_on_macos_for_python.md
GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
https://github.com/shantnu/Webcam-Face-Detect
shantnu/Webcam-Face-Detect
Contribute to shantnu/Webcam-Face-Detect development by creating an account on GitHub.
github.com
import cv2
import sys, os
import logging as log
import datetime as dt
import face_recognition
import numpy as np
from time import sleep
from datetime import datetime
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
known_face_encodings = []
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(100, 100),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
face_encodings = face_recognition.face_encodings(frame, {(y, x+w, y+h, x)})
if face_encodings is not None:
distances = face_recognition.face_distance(known_face_encodings, face_encodings[0])
# if distances is not None and len(distances) > 0:
# print(min(distances), max(distances))
# min(distances) == 0.6
if distances is None or len(distances) == 0 or (len(distances) > 0 and min(distances) > 0.6):
pathname = 'images/face_%s.jpg' % datetime.now()
cv2.imwrite(pathname, frame[y:y+h, x:x+w])
# img = face_recognition.load_image_file(pathname)
# face_encoding = face_recognition.face_encodings(img)
known_face_encodings.append(face_encodings[0])
# cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# if anterior != len(faces):
# anterior = len(faces)
# log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()