wyzely-detect/deepface-test.ipynb

94 lines
2.3 KiB
Plaintext

{
"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 <uuid>.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",
"\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=['VGG-Face_cosine'], inplace=True, ascending=False)\n",
" print(f'On dataframe {i}')\n",
" print(pd_dataframe)\n",
" # Get the most likely 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: {pd_dataframe.iloc[0][\"identity\"]}')\n",
" # Get the most likely identity's confidence\n",
" print(f'Confidence: {pd_dataframe.iloc[0][\"VGG-Face_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\")"
]
}
],
"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
}