Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions python/object_detection_with_huggingface/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/
env/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

187 changes: 187 additions & 0 deletions python/object_detection_with_huggingface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Object Detection with Hugging Face

This function uses the Hugging Face API to detect objects. It takes an image file from Appwrite storage and sends it to the Hugging Face API for object detection. The API returns a list of labels and their scores and records it in the database. This function also supports receiving file events from Appwrite Storage.

## 🧰 Usage

### POST /

**Parameters**
| Name | Description | Location | Type | Sample Value |
|------------|-------------|----------|--------|--------------|
| imageId | Appwrite File ID of Image | Body | String | `65c6319c5f34dc9638ec` |

This function also accepts body of a file event from Appwrite Storage.

**Response**

Sample `200` Response:

Example Image of an airport on Appwrite Storage is sent as input and is recognized.

```json
[
{
"score": 0.998191773891449,
"label": "person",
"box": {
"xmin": 870,
"ymin": 463,
"xmax": 895,
"ymax": 540
}
},
{
"score": 0.9406785368919373,
"label": "person",
"box": {
"xmin": 452,
"ymin": 395,
"xmax": 470,
"ymax": 425
}
},
{
"score": 0.9969456791877747,
"label": "person",
"box": {
"xmin": 428,
"ymin": 568,
"xmax": 466,
"ymax": 667
}
},
{
"score": 0.9682687520980835,
"label": "person",
"box": {
"xmin": 443,
"ymin": 394,
"xmax": 462,
"ymax": 421
}
},
{
"score": 0.9888055324554443,
"label": "truck",
"box": {
"xmin": 0,
"ymin": 467,
"xmax": 459,
"ymax": 713
}
},
{
"score": 0.9973268508911133,
"label": "airplane",
"box": {
"xmin": 498,
"ymin": 110,
"xmax": 1409,
"ymax": 417
}
},
{
"score": 0.9892668724060059,
"label": "person",
"box": {
"xmin": 1039,
"ymin": 444,
"xmax": 1064,
"ymax": 513
}
},
{
"score": 0.9430725574493408,
"label": "person",
"box": {
"xmin": 463,
"ymin": 394,
"xmax": 480,
"ymax": 422
}
}
]
```

Sample `404` Response:

```json
{
"error": "Image not found"
}
```

## ⚙️ Configuration

| Setting | Value |
| ----------------- | ------------------------------ |
| Runtime | Node (18.0) |
| Entrypoint | `src/main.js` |
| Build Commands | `npm install && npm run setup` |
| Permissions | `any` |
| Timeout (Seconds) | 15 |
| Events | `buckets.*.files.*.create` |

## 🔒 Environment Variables

### APPWRITE_BUCKET_ID

The ID of the storage bucket where the images are stored.

| Question | Answer |
| ------------ | ------------------ |
| Required | No |
| Sample Value | `object_detection` |

### APPWRITE_DATABASE_ID

The ID of the database where the responses are stored.

| Question | Answer |
| ------------ | ------ |
| Required | No |
| Sample Value | `ai` |

### APPWRITE_COLLECTION_ID

The ID of the collection where the responses are stored.

| Question | Answer |
| ------------ | ------------------ |
| Required | No |
| Sample Value | `object_detection` |

### HUGGINGFACE_ACCESS_TOKEN

Secret for sending requests to the Hugging Face API.

| Question | Answer |
| ------------- | ----------------------------------------------------------------------------------------------------- |
| Required | Yes |
| Sample Value | `hf_x2a...` |
| Documentation | [Hugging Face: API tokens](https://huggingface.co/docs/api-inference/en/quicktour#get-your-api-token) |


Create a `.env` file in the `src` directory and add the following environment variables:


```properties
APPWRITE_ENDPOINT=http://localhost/v1
APPWRITE_API_KEY=your_appwrite_api_key
APPWRITE_PROJECT_ID=your_appwrite_project_id
HUGGINGFACE_ACCESS_TOKEN=your_huggingface_access_token
APPWRITE_DATABASE_ID=ai
APPWRITE_COLLECTION_ID=object_detection
APPWRITE_BUCKET_ID=object_detection
```

## Running the Application

To process an images for object detection, you can use the `main.py` script. Ensure your Appwrite function is set up to handle HTTP requests and call the `process_object_detection` function.

## Dependencies

- pip install appwrite
- pip install huggingface_hub
- pip install python-dotenv
Loading