{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2023-10-03 09:26:15.259389: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2023-10-03 09:26:16.687334: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] } ], "source": [ "import os\n", "from deepface import DeepFace" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "dir_path = '/fsx/homes/mshuvi/IP-Adapter/maayan_face_aligned'\n", "img_path = 'assets/my_imgs/maayan_face.jpg'\n", "test_files = sorted(os.listdir(dir_path))\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "models = [\n", " \"VGG-Face\", \n", " \"Facenet\", \n", " \"Facenet512\", \n", " \"OpenFace\", \n", " \"DeepFace\", \n", " \"DeepID\", \n", " \"ArcFace\", \n", " \"Dlib\", \n", " \"SFace\",\n", "]" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'models' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/fsx/homes/mshuvi/IP-Adapter/face_recon.ipynb Cell 4\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m model \u001b[39m=\u001b[39m models[\u001b[39m2\u001b[39m]\n", "\u001b[0;31mNameError\u001b[0m: name 'models' is not defined" ] } ], "source": [ "model = models[2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details." ] } ], "source": [ "dict = {}\n", "for f in test_files:\n", " cur_path = os.path.join(dir_path, f)\n", " if not f in dict.keys():\n", " try:\n", " result = DeepFace.verify(img1_path = img_path, img2_path = cur_path, model_name = model)\n", " dict[f] = result\n", " print(f\"{f}: verified: {result['verified']} distance: {result['distance']}\" )\n", " except:\n", " print(f\"{f}: FACE NOT FOUND\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame.from_dict(dict, orient='index')\n", "df = df.sort_values(by=['distance'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df[:10]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df[-10:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "# open a file for writing\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "class CustomJSONizer(json.JSONEncoder):\n", " def default(self, obj):\n", " return super().encode(bool(obj)) \\\n", " if isinstance(obj, np.bool_) \\\n", " else super().default(obj)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('kobi_face_similarity.json', 'w') as f:\n", " # write the dictionary to the file in JSON format\n", " json.dump(dict, f, cls=CustomJSONizer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir_path = '/fsx/homes/mshuvi/IP-Adapter/maayan_face'\n", "img_path = '/fsx/homes/mshuvi/IP-Adapter/assets/my_imgs/maayan_face.jpg'\n", "test_files = sorted(os.listdir(dir_path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_maayan = {}\n", "for f in test_files:\n", " cur_path = os.path.join(dir_path, f)\n", " if not f in dict_maayan.keys():\n", " try:\n", " result = DeepFace.verify(img1_path = img_path, img2_path = cur_path, model_name = model, enforce_detection=False)\n", " dict_maayan[f] = result\n", " print(f\"{f}: verified: {result['verified']} distance: {result['distance']}\" )\n", " except:\n", " print(f\"{f}: FACE NOT FOUND\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_maayan = pd.DataFrame.from_dict(dict_maayan, orient='index')\n", "df_maayan = df_maayan.sort_values(by=['distance'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('maayan_face_similarity.json', 'w') as f:\n", " # write the dictionary to the file in JSON format\n", " json.dump(dict_maayan, f, cls=CustomJSONizer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_maayan[-10:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir_path = '/fsx/homes/mshuvi/IP-Adapter/maayan_face_nonplus'\n", "img_path = '/fsx/homes/mshuvi/IP-Adapter/assets/my_imgs/maayan_face.jpg'\n", "test_files = sorted(os.listdir(dir_path))\n", "\n", "dict_maayan = {}\n", "for f in test_files:\n", " cur_path = os.path.join(dir_path, f)\n", " if not f in dict_maayan.keys():\n", " try:\n", " result = DeepFace.verify(img1_path = img_path, img2_path = cur_path, model_name = model, enforce_detection=False)\n", " dict_maayan[f] = result\n", " print(f\"{f}: verified: {result['verified']} distance: {result['distance']}\" )\n", " except:\n", " print(f\"{f}: FACE NOT FOUND\")\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_maayan = pd.DataFrame.from_dict(dict_maayan, orient='index')\n", "df_maayan = df_maayan.sort_values(by=['distance'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_maayan[-10:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ms_env", "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.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }