<jupyter_start><jupyter_text>Face Recognition<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!pip install face_recognition
pwd
cd /content/drive/MyDrive/Colab Notebooks/FaceRecognition
pwd
ls
import face_recognition
import cv2
import os
from google.colab.patches import cv2_imshow

def read_img(path):
    img = cv2.imread(path)
    (h, w) = img.shape[:2]
    width = 200
    ratio = width/float(w)
    height = int(h*ratio)
    return cv2.resize(img, (height, width))

known_encodings = []
known_names = []
known_dir = 'known'

for file in os.listdir(known_dir):
    img = read_img(known_dir + '/' + file)
    img_enc = face_recognition.face_encodings(img)[0]
    known_encodings.append(img_enc)
    known_names.append(file.split('.')[0])

known_encodings
unknown_dir = 'unknown'
results_list = []

for file in os.listdir(unknown_dir):
    img_enc = []
    file_path = unknown_dir + '/' + file
    print("\nProcessing", file)
    img = read_img(file_path)
    img_enc = face_recognition.face_encodings(img)[0]
    
    results = face_recognition.compare_faces(known_encodings, img_enc)
    results_list.append(results)

    for j in range(len(results)):
        if results[j]:
            name = known_names[j]

            (top, right, bottom, left) = face_recognition.face_locations(img)[0]
            cv2.rectangle(img, (left, top), (right, bottom), (0,255,0), 2)

            font                   = cv2.FONT_HERSHEY_SIMPLEX
            bottomLeftCornerOfText = (round(left*1.1),round(bottom*1.15))
            fontScale              = 0.5
            fontColor              = (0,255,0)
            lineThickness          = 1

            image = cv2.putText(img, name, bottomLeftCornerOfText, font, fontScale, fontColor, lineThickness)
            cv2_imshow(image)<jupyter_output><empty_output>