Skip to content
Run error
# import numpy as np
# import urllib.request
# import sys
# import subprocess

# # implement pip as a subprocess:
# subprocess.check_call([sys.executable, '-m', 'pip', 'install', 
# 'opencv-python==4.5.5.62'])

# import cv2
# # multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
# faceCascade = cv2.CascadeClassifier('FaceDetection/Cascades/haarcascade_frontalface_default.xml')
# # url='https://media.istockphoto.com/id/1368965646/photo/multi-ethnic-guys-and-girls-taking-selfie-outdoors-with-backlight-happy-life-style-friendship.jpg?b=1&s=170667a&w=0&k=20&c=oFtP564Ykvak2VIyM1OUb29daY5S4uqsmT3j3_8QgfQ='
# # response = requests.get(url)
# # imga = Image.open(BytesIO(response.content))

# req = urllib.request.urlopen('https://media.istockphoto.com/id/1368965646/photo/multi-ethnic-guys-and-girls-taking-selfie-outdoors-with-backlight-happy-life-style-friendship.jpg?b=1&s=170667a&w=0&k=20&c=oFtP564Ykvak2VIyM1OUb29daY5S4uqsmT3j3_8QgfQ=')
# arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
# imga = cv2.imdecode(arr, -1) # 'Load it as it is'

# cap = cv2.imshow('lalala', imga)
# cap.set(3,640) # set Width
# cap.set(4,480) # set Height

# while True:
#     ret, img = cap.read()
#     img = cv2.flip(img, -1)
#     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     faces = faceCascade.detectMultiScale(
#         gray,
        
#         scaleFactor=1.2,
#         minNeighbors=5
#         ,     
#         minSize=(20, 20)
#     )

#     for (x,y,w,h) in faces:
#         cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#         roi_gray = gray[y:y+h, x:x+w]
#         roi_color = img[y:y+h, x:x+w]
        

#     cv2.imshow('video',img)

#     k = cv2.waitKey(30) & 0xff
#     if k == 27: # press 'ESC' to quit
#         break

# cap.release()
# cv2.destroyAllWindows()

import sys
import subprocess

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 
'opencv-python==4.5.5.62'])

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 
'dlib'])

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 
'face_recognition'])

# pip install numpy opencv-python
# pip install dlib
# pip install face_recognition

import face_recognition as fr
import cv2
import numpy as np
import os

path = "./train/"

known_names = []
known_name_encodings = []

images = os.listdir(path)
for _ in images:
    image = fr.load_image_file(path + _)
    image_path = path + _
    encoding = fr.face_encodings(image)[0]

    known_name_encodings.append(encoding)
    known_names.append(os.path.splitext(os.path.basename(image_path))[0].capitalize())

print(known_names)

test_image = "./test/test.jpg"
image = cv2.imread(test_image)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

face_locations = fr.face_locations(image)
face_encodings = fr.face_encodings(image, face_locations)

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    matches = fr.compare_faces(known_name_encodings, face_encoding)
    name = ""

    face_distances = fr.face_distance(known_name_encodings, face_encoding)
    best_match = np.argmin(face_distances)

    if matches[best_match]:
        name = known_names[best_match]

    cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
    cv2.rectangle(image, (left, bottom - 15), (right, bottom), (0, 0, 255), cv2.FILLED)
    font = cv2.FONT_HERSHEY_DUPLEX
    cv2.putText(image, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)


cv2.imshow("Result", image)
cv2.imwrite("./output.jpg", image)
cv2.waitKey(0)
cv2.destroyAllWindows()