FaceDB


NameFaceDB JSON
Version 0.0.10 PyPI version JSON
download
home_pagehttps://github.com/shhossain/facedb
SummaryA python package for large scale face recognition.
upload_time2023-09-10 10:51:06
maintainer
docs_urlNone
authorsifat (shhossain)
requires_python>=3.7
license
keywords python face recognition
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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"
os.environ["PINECONE_ENVIRONMENT"] = "YOUR_ENVIRONMENT_NAME"

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")
```

## 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": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "python,face,recognition",
    "author": "sifat (shhossain)",
    "author_email": "",
    "download_url": "",
    "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\nos.environ[\"PINECONE_ENVIRONMENT\"] = \"YOUR_ENVIRONMENT_NAME\"\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\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": "",
    "summary": "A python package for large scale face recognition.",
    "version": "0.0.10",
    "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": "591173c9c32fe1306836ecbabae1e1e0ee89042dd1fbf87a407d8e058a91ea60",
                "md5": "4f4714e003e3fab89a978e1b39f70bb0",
                "sha256": "50589a4294710782547eb1eae973fa5d8581b1e67370e05ae149b58ed00d37f2"
            },
            "downloads": -1,
            "filename": "FaceDB-0.0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4f4714e003e3fab89a978e1b39f70bb0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 21898,
            "upload_time": "2023-09-10T10:51:06",
            "upload_time_iso_8601": "2023-09-10T10:51:06.524650Z",
            "url": "https://files.pythonhosted.org/packages/59/11/73c9c32fe1306836ecbabae1e1e0ee89042dd1fbf87a407d8e058a91ea60/FaceDB-0.0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-10 10:51:06",
    "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"
}
        
Elapsed time: 0.11520s