{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":7203146,"sourceType":"datasetVersion","datasetId":4166956},{"sourceId":7205734,"sourceType":"datasetVersion","datasetId":4168622},{"sourceId":7207166,"sourceType":"datasetVersion","datasetId":4169644}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-15T09:19:54.935108Z","iopub.execute_input":"2023-12-15T09:19:54.935424Z","iopub.status.idle":"2023-12-15T09:19:56.092130Z","shell.execute_reply.started":"2023-12-15T09:19:54.935399Z","shell.execute_reply":"2023-12-15T09:19:56.091076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DeepFace try-outs\n\nDeepFace github official - https://github.com/serengil/deepface ","metadata":{}},{"cell_type":"code","source":"#Installing deepface library \n!pip install deepface --target=/kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-14T18:22:43.273042Z","iopub.execute_input":"2023-12-14T18:22:43.273464Z","iopub.status.idle":"2023-12-14T18:24:14.000678Z","shell.execute_reply.started":"2023-12-14T18:22:43.273433Z","shell.execute_reply":"2023-12-14T18:24:13.999269Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importing DeepFace\nfrom deepface import DeepFace","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:19:56.093782Z","iopub.execute_input":"2023-12-15T09:19:56.094183Z","iopub.status.idle":"2023-12-15T09:20:06.291416Z","shell.execute_reply.started":"2023-12-15T09:19:56.094156Z","shell.execute_reply":"2023-12-15T09:20:06.290376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using analyse to find the age, emotions and gender from the provided input image\nkeypoints = DeepFace.analyze(img_path = \"/kaggle/input/muskimages/musk_2.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:10.540444Z","iopub.execute_input":"2023-12-15T09:20:10.541371Z","iopub.status.idle":"2023-12-15T09:20:25.424234Z","shell.execute_reply.started":"2023-12-15T09:20:10.541336Z","shell.execute_reply":"2023-12-15T09:20:25.423251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Recognising Age, Gender, Emotion and race\nkeypoints","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:25.426135Z","iopub.execute_input":"2023-12-15T09:20:25.426827Z","iopub.status.idle":"2023-12-15T09:20:25.434986Z","shell.execute_reply.started":"2023-12-15T09:20:25.426784Z","shell.execute_reply":"2023-12-15T09:20:25.433981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Comparing two images and finding if both are same for given set of source images and target image.\nverification = DeepFace.verify(img1_path = \"/kaggle/input/bidenimages/Joseph_Biden_0002.jpg\",img2_path = \"/kaggle/input/muskimages/musk_4.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:27.916456Z","iopub.execute_input":"2023-12-15T09:20:27.916811Z","iopub.status.idle":"2023-12-15T09:20:32.642977Z","shell.execute_reply.started":"2023-12-15T09:20:27.916783Z","shell.execute_reply":"2023-12-15T09:20:32.641806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"verification","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:34.551949Z","iopub.execute_input":"2023-12-15T09:20:34.552308Z","iopub.status.idle":"2023-12-15T09:20:34.558927Z","shell.execute_reply.started":"2023-12-15T09:20:34.552277Z","shell.execute_reply":"2023-12-15T09:20:34.558071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Mediapipe - Face detection and landmarks extraction**\n\nMediapipe Official link - https://developers.google.com/mediapipe","metadata":{}},{"cell_type":"code","source":"!pip install mediapipe --target=/kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-15T02:59:45.893044Z","iopub.execute_input":"2023-12-15T02:59:45.893375Z","iopub.status.idle":"2023-12-15T03:00:11.671980Z","shell.execute_reply.started":"2023-12-15T02:59:45.893351Z","shell.execute_reply":"2023-12-15T03:00:11.670727Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport mediapipe as mp\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:38.472757Z","iopub.execute_input":"2023-12-15T09:20:38.473487Z","iopub.status.idle":"2023-12-15T09:20:38.861801Z","shell.execute_reply.started":"2023-12-15T09:20:38.473454Z","shell.execute_reply":"2023-12-15T09:20:38.861002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detect_faces_fn = mp.solutions.face_detection\ndetect_face = detect_faces_fn.FaceDetection(model_selection=0, min_detection_confidence=0.5)\ndraw_pts = mp.solutions.drawing_utils","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:40.554869Z","iopub.execute_input":"2023-12-15T09:20:40.555233Z","iopub.status.idle":"2023-12-15T09:20:40.567962Z","shell.execute_reply.started":"2023-12-15T09:20:40.555201Z","shell.execute_reply":"2023-12-15T09:20:40.566922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Detecting multiple faces**","metadata":{}},{"cell_type":"code","source":"input_img = cv2.imread('/kaggle/input/group-images/groupfaces.jpg')\nplt.figure(figsize = [10, 10])\nplt.title(\"Input Image\")\nplt.axis('off')\nplt.imshow(input_img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:42.728261Z","iopub.execute_input":"2023-12-15T09:20:42.729267Z","iopub.status.idle":"2023-12-15T09:20:42.970883Z","shell.execute_reply.started":"2023-12-15T09:20:42.729233Z","shell.execute_reply":"2023-12-15T09:20:42.969963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detect_res = detect_face.process(input_img[:,:,::-1])\nif detect_res.detections:\n    for f_no, face in enumerate(detect_res.detections):\n        print(f'Face Nos in sequence --', f_no)\n        print(f'Face confidence: {round(face.score[0], 2)}')\n        f_data = face.location_data\n        print(f'nFace BBOX:n{f_data.relative_bounding_box}')\n        for i in range(6):\n            print(f'{detect_faces_fn.FaceKeyPoint(i).name}:')\n            print(f'{f_data.relative_keypoints[detect_faces_fn.FaceKeyPoint(i).value]}')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:44.193873Z","iopub.execute_input":"2023-12-15T09:20:44.194541Z","iopub.status.idle":"2023-12-15T09:20:44.210875Z","shell.execute_reply.started":"2023-12-15T09:20:44.194504Z","shell.execute_reply":"2023-12-15T09:20:44.209816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_copy = input_img[:,:,::-1].copy()\nif detect_res.detections:\n    for f_no, face in enumerate(detect_res.detections):\n        draw_pts.draw_detection(image=img_copy, detection=face, keypoint_drawing_spec=draw_pts.DrawingSpec(color=(255, 0, 0),thickness=2,circle_radius=2))\nfig = plt.figure(figsize = [10, 10])\nplt.title(\"Final Image\")\nplt.axis('off')\nplt.imshow(img_copy)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T09:20:48.494158Z","iopub.execute_input":"2023-12-15T09:20:48.495158Z","iopub.status.idle":"2023-12-15T09:20:48.721573Z","shell.execute_reply.started":"2023-12-15T09:20:48.495088Z","shell.execute_reply":"2023-12-15T09:20:48.720528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}