{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from deepface import DeepFace\n", "import cv2\n", "from pathlib import Path\n", "import uuid\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take pictures" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Take a picture using opencv with .jpg\n", "# Then delete it after\n", "cap = cv2.VideoCapture(0)\n", "ret, frame = cap.read()\n", "cap.release()\n", "# uuid_str = str(uuid.uuid4())\n", "# uuid_path = Path(uuid_str + \".jpg\")\n", "# cv2.imwrite(str(uuid_path), frame)\n", "# dfs = DeepFace.find(img_path=str(uuid_path), db_path = \"faces\")\n", "# Don't throw an error if no face is detected (enforce_detection=False)\n", "dfs = DeepFace.find(frame, db_path = \"faces\", enforce_detection=False, silent=False, model_name=\"ArcFace\", detector_backend=\"opencv\")\n", "# Get the identity of the person\n", "for i, pd_dataframe in enumerate(dfs):\n", " # Sort the dataframe by confidence\n", " # inplace=True means that the dataframe is modified so we don't need to assign it to a new variable\n", " # pd_dataframe.sort_values(by=['model_name=\"ArcFace\", detector_backend=\"opencv\")'], inplace=True, ascending=False)\n", " print(f'On dataframe {i}')\n", " print(pd_dataframe)\n", " # Get the most likely identity\n", " # print(f'Most likely identity: {pd_dataframe.iloc[0][\"identity\"]}')\n", " # We could use Path to get the parent directory of the image to use as the identity\n", " print(f'Most likely identity: {Path(pd_dataframe.iloc[0][\"identity\"]).parent.name}')\n", " # Get the most likely identity's confidence\n", " print(f'Confidence: {pd_dataframe.iloc[0][\"ArcFace_cosine\"]}')\n", "\n", "# uuid_path.unlink()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stream" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DeepFace.stream(db_path=\"faces\", model_name=\"ArcFace\", detector_backend=\"opencv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stream normal frame by frame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from deepface import DeepFace\n", "import cv2\n", "from pathlib import Path\n", "import uuid\n", "import pandas as pd\n", "\n", "def main():\n", " cap = cv2.VideoCapture(0)\n", " while True:\n", " ret, frame = cap.read()\n", " dfs = DeepFace.find(frame, db_path = \"faces\", enforce_detection=False, silent=False, model_name=\"ArcFace\", detector_backend=\"opencv\")\n", " for i, pd_dataframe in enumerate(dfs):\n", " print(f'On dataframe {i}')\n", " print(pd_dataframe)\n", " print(f'Most likely identity: {Path(pd_dataframe.iloc[0][\"identity\"]).parent.name}')\n", " print(f'Confidence: {pd_dataframe.iloc[0][\"ArcFace_cosine\"]}')\n", " cv2.imshow(\"frame\", frame)\n", " if cv2.waitKey(1) & 0xFF == ord(\"q\"):\n", " break\n", " cap.release()\n", " cv2.destroyAllWindows()\n", " \n", "\n", "\n", "main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other functions\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" } }, "nbformat": 4, "nbformat_minor": 2 }