# FaceDB - A Face Recognition Database
FaceDB is a Python library that provides an easy-to-use interface for face recognition and face database management. It allows you to perform face recognition tasks, such as face matching and face searching, and manage a database of faces efficiently. FaceDB supports two popular face recognition frameworks: DeepFace and face_recognition.
## Links
[Pypi](https://pypi.org/project/facedb/)
[Github](https://github.com/shhossain/facedb)
## Installation
FaceDB can be installed using pip:
```bash
pip install facedb
```
You can use face_recognition or DeepFace for face recognition. If you want to use DeepFace, you need to install the following dependencies:
for face_recognition:
```bash
pip install face_recognition
```
for DeepFace:
```bash
pip install deepface
```
## Simple Usage
This will create a chromadb database in the current directory.
```python
# Import the FaceDB library
from facedb import FaceDB
# Create a FaceDB instance
db = FaceDB(
path="facedata",
)
# Add a new face to the database
face_id = db.add("John Doe", img="john_doe.jpg")
# Recognize a face
result = db.recognize(img="new_face.jpg")
# Check if the recognized face is similar to the one in the database
if result and result["id"] == face_id:
print("Recognized as John Doe")
else:
print("Unknown face")
```
## Advanced Usage
You need to install pinecone first to use pinecone as the database backend.
```bash
pip install pinecone
```
```python
import os
os.environ["PINECONE_API_KEY"] = "YOUR_API_KEY"
db = FaceDB(
path="facedata",
metric='euclidean',
database_backend='pinecone',
index_name='faces',
embedding_dim=128,
module='face_recognition',
)
# This will create a pinecone index with name 'faces' in your environment if it doesn't exist
# add multiple faces
from glob import glob
from pathlib import Path
files = glob("faces/*.jpg") # Suppose you have a folder with imgs with names as filenames
imgs = []
names = []
for file in files:
imgs.append(file)
names.append(Path(file).name)
ids, failed_indexes = db.add_many(
imgs=imgs,
names=names,
)
unknown_face = "unknown_face.jpg"
result = db.recognize(img=unknown_face, include=['name'])
if result:
print(f"Recognized as {result['name']}")
else:
print("Unknown face")
# Include img in the result
result = db.recognize(img=unknown_face, include=['img'])
if result:
result.show_img()
# # Use can also use show_img() for multiple results
results = db.all(include='name')
results.show_img() # make sure you have matplotlib installed
# or
img = result['img'] # cv2 image (numpy array)
# Include embedding in the result
result = db.recognize(img=unknown_face, include=['embedding'])
if result:
print(result['embedding'])
# Search for similar faces
results = db.search(img=unknown_face, top_k=5, include=['name'])[0]
for result in results:
print(f"Found {result['name']} with distance {result['distance']}")
# or search for multiple faces
multi_results = db.search(img=[img1, img2], top_k=5, include=['name'])
for results in multi_results:
for result in results:
print(f"Found {result['name']} with distance {result['distance']}")
# get all faces
faces = db.get_all(include=['name', 'img'])
# Update a face
db.update(face_id, name="John Doe", img="john_doe.jpg", metadata1="metadata1", metadata2="metadata2")
# Delete a face
db.delete(face_id)
# Count the number of faces in the database
count = db.count()
# Get pandas dataframe of all faces
df = db.all().df
```
## Simple Querying
```python
# First add some faces to the database
db.add("Nelson Mandela", img="mandela.jpg", profession="Politician", country="South Africa")
db.add("Barack Obama", img="obama.jpg", profession="Politician", country="USA")
db.add("Einstein", img="einstein.jpg", profession="Scientist", country="Germany")
# Query the database by name
results = db.query(name="Nelson Mandela")
# Query the database by profession
results = db.query(profession="Politician")
```
## If you don't have an API key
You can follow the official pinecone tutorial : https://docs.pinecone.io/docs/new-api
It's easy to use and to understand, don't worry.
## Advanced Querying
You can use following operators in queries:
- $eq - Equal to (number, string, boolean)
- $ne - Not equal to (number, string, boolean)
- $gt - Greater than (number)
- $lt - Less than (number)
- $in - In array (string or number)
- $regex - Regex match (string)
```python
results = db.query(
profession={"$eq": "Politician"},
country={"$in": ["USA", "South Africa"]},
)
# or write in a single json
results = db.query(
where={
"profession": {"$eq": "Politician"},
"country": {"$in": ["USA", "South Africa"]},
}
)
# you can use show_img(), df, query to further filter the results
results.show_img()
results.df
results.query(name="Nelson Mandela")
```
Raw data
{
"_id": null,
"home_page": "https://github.com/shhossain/facedb",
"name": "FaceDB",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "python, face, recognition",
"author": "sifat (shhossain)",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/5b/b8/06bc1bf996a5959cf32f35cd9e3649ec09c2883ed6097fe0181fe983f10f/FaceDB-0.0.13.tar.gz",
"platform": null,
"description": "# FaceDB - A Face Recognition Database\r\n\r\nFaceDB is a Python library that provides an easy-to-use interface for face recognition and face database management. It allows you to perform face recognition tasks, such as face matching and face searching, and manage a database of faces efficiently. FaceDB supports two popular face recognition frameworks: DeepFace and face_recognition.\r\n\r\n## Links\r\n[Pypi](https://pypi.org/project/facedb/)\r\n[Github](https://github.com/shhossain/facedb)\r\n\r\n## Installation\r\n\r\nFaceDB can be installed using pip:\r\n\r\n```bash\r\npip install facedb\r\n```\r\n\r\nYou can use face_recognition or DeepFace for face recognition. If you want to use DeepFace, you need to install the following dependencies:\r\nfor face_recognition:\r\n\r\n```bash\r\npip install face_recognition\r\n```\r\n\r\nfor DeepFace:\r\n\r\n```bash\r\npip install deepface\r\n```\r\n\r\n## Simple Usage\r\n\r\nThis will create a chromadb database in the current directory.\r\n\r\n```python\r\n# Import the FaceDB library\r\nfrom facedb import FaceDB\r\n\r\n# Create a FaceDB instance\r\ndb = FaceDB(\r\n path=\"facedata\",\r\n)\r\n\r\n# Add a new face to the database\r\nface_id = db.add(\"John Doe\", img=\"john_doe.jpg\")\r\n\r\n# Recognize a face\r\nresult = db.recognize(img=\"new_face.jpg\")\r\n\r\n# Check if the recognized face is similar to the one in the database\r\nif result and result[\"id\"] == face_id:\r\n print(\"Recognized as John Doe\")\r\nelse:\r\n print(\"Unknown face\")\r\n```\r\n\r\n## Advanced Usage\r\n\r\nYou need to install pinecone first to use pinecone as the database backend.\r\n\r\n```bash\r\npip install pinecone\r\n```\r\n\r\n```python\r\nimport os\r\n\r\nos.environ[\"PINECONE_API_KEY\"] = \"YOUR_API_KEY\"\r\n\r\ndb = FaceDB(\r\n path=\"facedata\",\r\n metric='euclidean',\r\n database_backend='pinecone',\r\n index_name='faces',\r\n embedding_dim=128,\r\n module='face_recognition',\r\n)\r\n\r\n# This will create a pinecone index with name 'faces' in your environment if it doesn't exist\r\n\r\n# add multiple faces\r\nfrom glob import glob\r\nfrom pathlib import Path\r\n\r\nfiles = glob(\"faces/*.jpg\") # Suppose you have a folder with imgs with names as filenames\r\nimgs = []\r\nnames = []\r\nfor file in files:\r\n imgs.append(file)\r\n names.append(Path(file).name)\r\n\r\nids, failed_indexes = db.add_many(\r\n imgs=imgs,\r\n names=names,\r\n)\r\n\r\nunknown_face = \"unknown_face.jpg\"\r\nresult = db.recognize(img=unknown_face, include=['name'])\r\nif result:\r\n print(f\"Recognized as {result['name']}\")\r\nelse:\r\n print(\"Unknown face\")\r\n\r\n\r\n# Include img in the result\r\nresult = db.recognize(img=unknown_face, include=['img'])\r\nif result:\r\n result.show_img()\r\n\r\n# # Use can also use show_img() for multiple results\r\nresults = db.all(include='name')\r\nresults.show_img() # make sure you have matplotlib installed\r\n\r\n# or\r\nimg = result['img'] # cv2 image (numpy array)\r\n\r\n# Include embedding in the result\r\nresult = db.recognize(img=unknown_face, include=['embedding'])\r\nif result:\r\n print(result['embedding'])\r\n\r\n\r\n# Search for similar faces\r\nresults = db.search(img=unknown_face, top_k=5, include=['name'])[0]\r\n\r\nfor result in results:\r\n print(f\"Found {result['name']} with distance {result['distance']}\")\r\n\r\n# or search for multiple faces\r\nmulti_results = db.search(img=[img1, img2], top_k=5, include=['name'])\r\n\r\nfor results in multi_results:\r\n for result in results:\r\n print(f\"Found {result['name']} with distance {result['distance']}\")\r\n\r\n# get all faces\r\nfaces = db.get_all(include=['name', 'img']) \r\n\r\n# Update a face\r\ndb.update(face_id, name=\"John Doe\", img=\"john_doe.jpg\", metadata1=\"metadata1\", metadata2=\"metadata2\")\r\n\r\n# Delete a face\r\ndb.delete(face_id)\r\n\r\n# Count the number of faces in the database\r\ncount = db.count()\r\n\r\n# Get pandas dataframe of all faces\r\ndf = db.all().df\r\n```\r\n\r\n## Simple Querying\r\n\r\n```python\r\n\r\n# First add some faces to the database\r\ndb.add(\"Nelson Mandela\", img=\"mandela.jpg\", profession=\"Politician\", country=\"South Africa\")\r\ndb.add(\"Barack Obama\", img=\"obama.jpg\", profession=\"Politician\", country=\"USA\")\r\ndb.add(\"Einstein\", img=\"einstein.jpg\", profession=\"Scientist\", country=\"Germany\")\r\n\r\n# Query the database by name\r\nresults = db.query(name=\"Nelson Mandela\")\r\n\r\n# Query the database by profession\r\nresults = db.query(profession=\"Politician\")\r\n```\r\n## If you don't have an API key\r\n\r\nYou can follow the official pinecone tutorial : https://docs.pinecone.io/docs/new-api\r\nIt's easy to use and to understand, don't worry.\r\n\r\n## Advanced Querying\r\n\r\nYou can use following operators in queries:\r\n\r\n- $eq - Equal to (number, string, boolean)\r\n- $ne - Not equal to (number, string, boolean)\r\n- $gt - Greater than (number)\r\n- $lt - Less than (number)\r\n- $in - In array (string or number)\r\n- $regex - Regex match (string)\r\n\r\n```python\r\nresults = db.query(\r\n profession={\"$eq\": \"Politician\"},\r\n country={\"$in\": [\"USA\", \"South Africa\"]},\r\n)\r\n# or write in a single json\r\nresults = db.query(\r\n where={\r\n \"profession\": {\"$eq\": \"Politician\"},\r\n \"country\": {\"$in\": [\"USA\", \"South Africa\"]},\r\n }\r\n)\r\n\r\n# you can use show_img(), df, query to further filter the results\r\nresults.show_img()\r\nresults.df\r\nresults.query(name=\"Nelson Mandela\")\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A python package for face database management",
"version": "0.0.13",
"project_urls": {
"Bug Tracker": "https://github.com/shhossain/facedb/issues",
"Documentation": "https://github.com/shhossain/facedb",
"Homepage": "https://github.com/shhossain/facedb",
"Source": "https://github.com/shhossain/facedb"
},
"split_keywords": [
"python",
" face",
" recognition"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5bb806bc1bf996a5959cf32f35cd9e3649ec09c2883ed6097fe0181fe983f10f",
"md5": "b6a39d23ac486e6f6007680ce95a1266",
"sha256": "beadac318c328b3595b911b698430cce3defb794f876b8f9d2f64b2a37a16263"
},
"downloads": -1,
"filename": "FaceDB-0.0.13.tar.gz",
"has_sig": false,
"md5_digest": "b6a39d23ac486e6f6007680ce95a1266",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 26206,
"upload_time": "2024-07-10T14:01:14",
"upload_time_iso_8601": "2024-07-10T14:01:14.443353Z",
"url": "https://files.pythonhosted.org/packages/5b/b8/06bc1bf996a5959cf32f35cd9e3649ec09c2883ed6097fe0181fe983f10f/FaceDB-0.0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-10 14:01:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "shhossain",
"github_project": "facedb",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "facedb"
}