Skip to content

Commit

Permalink
add image registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry-Loi committed Apr 22, 2024
1 parent d907f25 commit 63c1242
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 224 deletions.
224 changes: 0 additions & 224 deletions faceAI.ipynb

This file was deleted.

181 changes: 181 additions & 0 deletions facial_similarity_checker.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dependencies\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# %pip install -q tf_keras\n",
"# %pip install opencv-contrib-python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Library\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from deepface import DeepFace\n",
"\n",
"import cv2\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom Class\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class facial_area():\n",
" def __init__(self, tar=None):\n",
" if (tar == None):\n",
" self.x = 0\n",
" self.y = 0\n",
" self.width = 0\n",
" self.height = 0\n",
" else:\n",
" self.set(tar)\n",
" \n",
" def __str__(self):\n",
" return f\"x: {self.x}\\ty: {self.y}\\tSize: ({self.width},{self.height})\"\n",
"\n",
" def set(self, tar):\n",
" self.x = tar[\"x\"]\n",
" self.y = tar[\"y\"]\n",
" self.width = tar[\"w\"]\n",
" self.height = tar[\"h\"]\n",
" print(self)\n",
" \n",
" def show_result(self, img, with_size=False):\n",
" img = cv2.imread(img)\n",
" img = cv2.rectangle(img, (self.x, self.y), (self.x + self.width, self.y + self.height), (0, 255, 0), 2)\n",
" if with_size:\n",
" cv2.putText(img, f\"({self.width},{self.height})\", (self.x, self.y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
" return img\n",
" \n",
"class facial_similarity_checker():\n",
" def __init__(self):\n",
" self.img1 = None\n",
" self.img2 = None\n",
" self.img1_area = facial_area()\n",
" self.img2_area = facial_area()\n",
" \n",
" def load_image(self, img1, img2):\n",
" self.img1 = img1\n",
" self.img2 = img2\n",
" \n",
" def check_similarity(self, img1=None, img2=None, show_detail=False):\n",
" if img1 == None or img2 == None:\n",
" img1 = self.img1\n",
" img2 = self.img2\n",
" else:\n",
" self.load_image(img1, img2)\n",
"\n",
" try :\n",
" img1 = cv2.imread(img1)\n",
" img2 = cv2.imread(img2)\n",
" except:\n",
" print(\"The image path is not correct\")\n",
" return -1\n",
"\n",
" result = DeepFace.verify(img1, img2)\n",
" self.img1_area.set(result[\"facial_areas\"][\"img1\"])\n",
" self.img2_area.set(result[\"facial_areas\"][\"img2\"])\n",
" \n",
" if show_detail:\n",
" print(result)\n",
"\n",
" return result[\"verified\"], result[\"distance\"]\n",
"\n",
" def show_image(self, img):\n",
" if (self.img1 == img):\n",
" img = self.img1_area.show_result(img, with_size=True)\n",
" else:\n",
" img = self.img2_area.show_result(img, with_size=True)\n",
" \n",
" plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n",
" plt.show()\n",
"\n",
" def show_result(self, show_detail=False):\n",
" result, dis = self.check_similarity(self.img1, self.img2, show_detail)\n",
" print(\"The similarity between the images is: \", dis)\n",
" print(\"The images are the same person: \", result)\n",
" self.show_image(self.img1)\n",
" self.show_image(self.img2)\n",
" \n",
" def get_full_result(self):\n",
" return DeepFace.verify(cv2.imread(self.img1),cv2.imread(self.img2))\n",
" \n",
" def get_facial_area(self):\n",
" return self.img1_area, self.img2_area\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# checker = facial_similarity_checker()\n",
"\n",
"# image1_path = \"img/mymask_cup.jpg\"\n",
"# image2_path = \"img/me_without_mask.jpg\"\n",
"\n",
"# checker.load_image(image1_path, image2_path)\n",
"# checker.show_result(show_detail=True)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
348 changes: 348 additions & 0 deletions image_registration.ipynb

Large diffs are not rendered by default.

File renamed without changes
File renamed without changes
Binary file added img/me_without_mask2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/me_without_mask3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/me_without_mask4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 63c1242

Please sign in to comment.