├── .gitignore ├── dataset ├── another_woman.jpg ├── regina_1.jpg ├── regina_2.jpg ├── regina_3.jpg ├── regina_4.jpg ├── regina_5.jpg ├── regina_6.jpg └── regina_7.jpg ├── dataset_from_video ├── 0.jpg ├── 1.jpg ├── 2.jpg ├── 3.jpg └── 4_extra_scr.jpg ├── img ├── gal1.jpg ├── gal2.jpg ├── jason_statham.jpg ├── justice_league_actors.jpg └── mr_robot_actors.jpg ├── main.py ├── requirements.txt ├── training_model.py └── video.mp4 /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.pyc 3 | /dlib/ 4 | /venvFR/ -------------------------------------------------------------------------------- /dataset/another_woman.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/another_woman.jpg -------------------------------------------------------------------------------- /dataset/regina_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_1.jpg -------------------------------------------------------------------------------- /dataset/regina_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_2.jpg -------------------------------------------------------------------------------- /dataset/regina_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_3.jpg -------------------------------------------------------------------------------- /dataset/regina_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_4.jpg -------------------------------------------------------------------------------- /dataset/regina_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_5.jpg -------------------------------------------------------------------------------- /dataset/regina_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_6.jpg -------------------------------------------------------------------------------- /dataset/regina_7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset/regina_7.jpg -------------------------------------------------------------------------------- /dataset_from_video/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset_from_video/0.jpg -------------------------------------------------------------------------------- /dataset_from_video/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset_from_video/1.jpg -------------------------------------------------------------------------------- /dataset_from_video/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset_from_video/2.jpg -------------------------------------------------------------------------------- /dataset_from_video/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset_from_video/3.jpg -------------------------------------------------------------------------------- /dataset_from_video/4_extra_scr.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/dataset_from_video/4_extra_scr.jpg -------------------------------------------------------------------------------- /img/gal1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/img/gal1.jpg -------------------------------------------------------------------------------- /img/gal2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/img/gal2.jpg -------------------------------------------------------------------------------- /img/jason_statham.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/img/jason_statham.jpg -------------------------------------------------------------------------------- /img/justice_league_actors.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/img/justice_league_actors.jpg -------------------------------------------------------------------------------- /img/mr_robot_actors.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/img/mr_robot_actors.jpg -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import face_recognition 2 | from PIL import Image, ImageDraw 3 | import pickle 4 | from cv2 import cv2 5 | 6 | 7 | def face_rec(): 8 | gal_face_img = face_recognition.load_image_file("img/gal1.jpg") 9 | gal_face_location = face_recognition.face_locations(gal_face_img) 10 | 11 | justice_league_img = face_recognition.load_image_file("img/justice_league_actors.jpg") 12 | justice_league_faces_locations = face_recognition.face_locations(justice_league_img) 13 | 14 | # print(gal_face_location) 15 | # print(justice_league_faces_locations) 16 | # print(f"Found {len(gal_face_location)} face(s) in this image") 17 | # print(f"Found {len(justice_league_faces_locations)} face(s) in this image") 18 | 19 | pil_img1 = Image.fromarray(gal_face_img) 20 | draw1 = ImageDraw.Draw(pil_img1) 21 | 22 | for(top, right, bottom, left) in gal_face_location: 23 | draw1.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=4) 24 | 25 | del draw1 26 | pil_img1.save("img/new_gal1.jpg") 27 | 28 | pil_img2 = Image.fromarray(justice_league_img) 29 | draw2 = ImageDraw.Draw(pil_img2) 30 | 31 | for(top, right, bottom, left) in justice_league_faces_locations: 32 | draw2.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=4) 33 | 34 | del draw2 35 | pil_img2.save("img/new_justice_league.jpg") 36 | 37 | 38 | def extracting_faces(img_path): 39 | count = 0 40 | faces = face_recognition.load_image_file(img_path) 41 | faces_locations = face_recognition.face_locations(faces) 42 | 43 | for face_location in faces_locations: 44 | top, right, bottom, left = face_location 45 | 46 | face_img = faces[top:bottom, left:right] 47 | pil_img = Image.fromarray(face_img) 48 | pil_img.save(f"img/{count}_face_img.jpg") 49 | count += 1 50 | 51 | return f"Found {count} face(s) in this photo" 52 | 53 | 54 | def compare_faces(img1_path, img2_path): 55 | img1 = face_recognition.load_image_file(img1_path) 56 | img1_encodings = face_recognition.face_encodings(img1)[0] 57 | # print(img1_encodings) 58 | 59 | img2 = face_recognition.load_image_file(img2_path) 60 | img2_encodings = face_recognition.face_encodings(img2)[0] 61 | 62 | result = face_recognition.compare_faces([img1_encodings], img2_encodings) 63 | # print(result) 64 | 65 | if result[0]: 66 | print("Welcome to the club! :*") 67 | else: 68 | print("Sorry, not today... Next!") 69 | 70 | 71 | def detect_person_in_video(): 72 | data = pickle.loads(open("Person_name_encodings.pickle", "rb").read()) 73 | video = cv2.VideoCapture("video.mp4") 74 | 75 | while True: 76 | ret, image = video.read() 77 | 78 | locations = face_recognition.face_locations(image, model="cnn") 79 | encodings = face_recognition.face_encodings(image, locations) 80 | 81 | for face_encoding, face_location in zip(encodings, locations): 82 | result = face_recognition.compare_faces(data["encodings"], face_encoding) 83 | match = None 84 | 85 | if True in result: 86 | match = data["name"] 87 | print(f"Match found! {match}") 88 | else: 89 | print("ACHTUNG! ALARM!") 90 | 91 | left_top = (face_location[3], face_location[0]) 92 | right_bottom = (face_location[1], face_location[2]) 93 | color = [0, 255, 0] 94 | cv2.rectangle(image, left_top, right_bottom, color, 4) 95 | 96 | left_bottom = (face_location[3], face_location[2]) 97 | right_bottom = (face_location[1], face_location[2] + 20) 98 | cv2.rectangle(image, left_bottom, right_bottom, color, cv2.FILLED) 99 | cv2.putText( 100 | image, 101 | match, 102 | (face_location[3] + 10, face_location[2] + 15), 103 | cv2.FONT_HERSHEY_SIMPLEX, 104 | 1, 105 | (255, 255, 255), 106 | 4 107 | ) 108 | 109 | cv2.imshow("detect_person_in_video is running", image) 110 | 111 | k = cv2.waitKey(20) 112 | if k == ord("q"): 113 | print("Q pressed, closing the app") 114 | break 115 | 116 | 117 | def main(): 118 | # face_rec() 119 | # print(extracting_faces("img/justice_league_actors.jpg")) 120 | # compare_faces("img/gal1.jpg", "img/gal2.jpg") 121 | detect_person_in_video() 122 | 123 | 124 | if __name__ == '__main__': 125 | main() 126 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | click==7.1.2 2 | dlib==19.22.99 3 | face-recognition==1.3.0 4 | face-recognition-models==0.3.0 5 | imutils==0.5.4 6 | numpy==1.20.2 7 | opencv-python==4.5.1.48 8 | Pillow==8.2.0 9 | -------------------------------------------------------------------------------- /training_model.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pickle 3 | import sys 4 | import face_recognition 5 | from cv2 import cv2 6 | 7 | 8 | def train_model_by_img(name): 9 | 10 | if not os.path.exists("dataset"): 11 | print("[ERROR] there is no directory 'dataset'") 12 | sys.exit() 13 | 14 | known_encodings = [] 15 | images = os.listdir("dataset") 16 | 17 | # print(images) 18 | 19 | for(i, image) in enumerate(images): 20 | print(f"[+] processing img {i + 1}/{len(images)}") 21 | # print(image) 22 | 23 | face_img = face_recognition.load_image_file(f"dataset/{image}") 24 | face_enc = face_recognition.face_encodings(face_img)[0] 25 | 26 | # print(face_enc) 27 | 28 | if len(known_encodings) == 0: 29 | known_encodings.append(face_enc) 30 | else: 31 | for item in range(0, len(known_encodings)): 32 | result = face_recognition.compare_faces([face_enc], known_encodings[item]) 33 | # print(result) 34 | 35 | if result[0]: 36 | known_encodings.append(face_enc) 37 | # print("Same person!") 38 | break 39 | else: 40 | # print("Another person!") 41 | break 42 | 43 | # print(known_encodings) 44 | # print(f"Length {len(known_encodings)}") 45 | 46 | data = { 47 | "name": name, 48 | "encodings": known_encodings 49 | } 50 | 51 | with open(f"{name}_encodings.pickle", "wb") as file: 52 | file.write(pickle.dumps(data)) 53 | 54 | return f"[INFO] File {name}_encodings.pickle successfully created" 55 | 56 | 57 | def take_screenshot_from_video(): 58 | cap = cv2.VideoCapture("video.mp4") 59 | count = 0 60 | 61 | if not os.path.exists("dataset_from_video"): 62 | os.mkdir("dataset_from_video") 63 | 64 | while True: 65 | ret, frame = cap.read() 66 | fps = cap.get(cv2.CAP_PROP_FPS) 67 | multiplier = fps * 3 68 | # print(fps) 69 | 70 | if ret: 71 | frame_id = int(round(cap.get(1))) 72 | # print(frame_id) 73 | cv2.imshow("frame", frame) 74 | k = cv2.waitKey(20) 75 | 76 | if frame_id % multiplier == 0: 77 | cv2.imwrite(f"dataset_from_video/{count}.jpg", frame) 78 | print(f"Take a screenshot {count}") 79 | count += 1 80 | 81 | if k == ord(" "): 82 | cv2.imwrite(f"dataset_from_video/{count}_extra_scr.jpg", frame) 83 | print(f"Take an extra screenshot {count}") 84 | count += 1 85 | elif k == ord("q"): 86 | print("Q pressed, closing the app") 87 | break 88 | 89 | else: 90 | print("[Error] Can't get the frame...") 91 | break 92 | 93 | cap.release() 94 | cv2.destroyAllWindows() 95 | 96 | 97 | def main(): 98 | print(train_model_by_img("Person_name")) 99 | # take_screenshot_from_video() 100 | 101 | 102 | if __name__ == '__main__': 103 | main() 104 | -------------------------------------------------------------------------------- /video.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pythontoday/face_recognition_python/a5179289085ae5c36269e59711c7f4dbfd11269d/video.mp4 --------------------------------------------------------------------------------