Created
August 3, 2023 20:20
-
-
Save ryf123/3b0fa30e0b50272fa11dfca5025b5103 to your computer and use it in GitHub Desktop.
Face Similarity.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNLtnKbAS2cMI7PjdNkuHSt", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ryf123/3b0fa30e0b50272fa11dfca5025b5103/face-similarity.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "auRTfckTSHNp" | |
}, | |
"outputs": [], | |
"source": [ | |
"!pip install insightface\n", | |
"!pip install onnxruntime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from insightface.app import FaceAnalysis\n", | |
"import cv2\n", | |
"import numpy as np" | |
], | |
"metadata": { | |
"id": "slDnIwBuThuZ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def face_similarity(face_analyser, img1, img2):\n", | |
" f1 = face_analyser.get(img1)[0]\n", | |
" f2 = face_analyser.get(img2)[0]\n", | |
" return np.sum(np.square(f1.normed_embedding - f2.normed_embedding))" | |
], | |
"metadata": { | |
"id": "__-ZsblqSS5h" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# how to use\n", | |
"providers = ['CPUExecutionProvider']\n", | |
"app = FaceAnalysis(providers=providers, name='buffalo_l')\n", | |
"app.prepare(ctx_id=0, det_size=(640, 640))\n", | |
"app.prepare(ctx_id=0)\n", | |
"\n", | |
"child_image_path = '/content/child.png' # @param {type:\"string\"}\n", | |
"father_image_path = '/content/father.png' # @param {type:\"string\"}\n", | |
"mother_image_path = '/content/mother.png' # @param {type:\"string\"}\n", | |
"similarity_1 = face_similarity(\n", | |
" app,\n", | |
" cv2.imread(child_image_path),\n", | |
" cv2.imread(father_image_path)\n", | |
")\n", | |
"similarity_2 = face_similarity(\n", | |
" app,\n", | |
" cv2.imread(child_image_path),\n", | |
" cv2.imread(mother_image_path)\n", | |
")\n", | |
"print(f'father similarity {similarity_1}, mother similarity {similarity_2}')" | |
], | |
"metadata": { | |
"id": "dzxHN89AT5-P" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment