wyzely-detect/deepface-test.ipynb

150 lines
4.1 KiB
Plaintext
Raw Normal View History

2023-10-14 02:51:16 +01:00
{
"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",
2023-10-14 21:40:36 +01:00
"# 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",
2023-10-14 02:51:16 +01:00
"# 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",
2023-10-14 02:51:16 +01:00
" print(f'On dataframe {i}')\n",
" print(pd_dataframe)\n",
" # Get the most likely identity\n",
2023-10-14 21:40:36 +01:00
" # print(f'Most likely identity: {pd_dataframe.iloc[0][\"identity\"]}')\n",
2023-10-14 02:51:16 +01:00
" # We could use Path to get the parent directory of the image to use as the identity\n",
2023-10-14 21:40:36 +01:00
" print(f'Most likely identity: {Path(pd_dataframe.iloc[0][\"identity\"]).parent.name}')\n",
2023-10-14 02:51:16 +01:00
" # Get the most likely identity's confidence\n",
" print(f'Confidence: {pd_dataframe.iloc[0][\"ArcFace_cosine\"]}')\n",
2023-10-14 02:51:16 +01:00
"\n",
2023-10-14 21:40:36 +01:00
"# uuid_path.unlink()"
2023-10-14 02:51:16 +01:00
]
},
{
"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\")"
2023-10-14 02:51:16 +01:00
]
},
{
"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"
]
2023-10-14 02:51:16 +01:00
}
],
"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
}