Skip to content

Commit

Permalink
Merge pull request #1 from escoand/docker
Browse files Browse the repository at this point in the history
add Dockerfile
  • Loading branch information
matiasdelellis authored May 11, 2022
2 parents 7e80e7f + 3c66367 commit 324b9eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM python:slim AS builder

COPY Makefile /app/

RUN apt update -yq \
&& apt install -yq bzip2 cmake g++ make wget \
&& pip wheel -w /app/ dlib \
&& make -C /app/ download-models

FROM python:slim

COPY --from=builder /app/dlib*.whl /tmp/
COPY --from=builder /app/vendor/ /app/vendor/
COPY facerecognition-external-model.py /app/

RUN pip install flask numpy \
&& pip install --no-index -f /tmp/ dlib \
&& rm /tmp/dlib*.whl

WORKDIR /app/

EXPOSE 5000

ENV API_KEY=some-super-secret-api-key
ENV FLASK_APP=facerecognition-external-model.py

CMD flask run -h 0.0.0.0
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ The images are sent via POST, and are immediately deleted after being analyzed.

So, please. Think seriously about data security before running this service outside of your local network.

## Dependencies
## Docker
The fastest way to get this up and running without manual installation and configuration is a docker image. You only have to define the api key and the exposed port:
```sh
docker build -t facerecognition https://github.com/matiasdelellis/facerecognition-external-model.git
docker run --rm -i -p 8080:5000 -e API_KEY="super-secret" facerecognition
# possible is also api key as file
docker run --rm -i -p 8080:5000 -v /path/to/api.key:/app/api.key facerecognition
```

## Manual
### Dependencies
* python3-flask
* python3-dlib
* python3-numpy

## Install
Just clone this repo and install the models. Use FACE_MODEL to specify the desired model.
### Install
Just clone this repo and install the resnet models. Use FACE_MODEL to specify the desired model.
```
[matias@laptop ~]$ git clone https://github.com/matiasdelellis/facerecognition-external-model.git
Clonando en 'facerecognition-external-model'...
Expand All @@ -36,10 +46,8 @@ bzip2 -d vendor/models/1/shape_predictor_5_face_landmarks.dat.bz2
[matias@laptop facerecognition-external-model]$
```

## Usage

### Configure Service

### Usage
#### Configure Service
You must generate a shared api key and save it in the file `api.key`.
```
[matias@laptop facerecognition-external-model]$ openssl rand -base64 32
Expand Down Expand Up @@ -71,7 +79,7 @@ Or you can just run: `make FACE_MODEL=1 serve`.

Note that this service is running on `http://192.168.1.103:8080/`

### Test
## Test
You can test if the service is running using curl.
```
[matias@laptop ~]$ curl http://192.168.1.103:8080/welcome
Expand All @@ -90,7 +98,7 @@ If curl responds something like the following, surely you have a firewall with p
curl: (7) Failed to connect to 192.168.1.103 port 8080: No route to host
```

### Use
## Use
If the service is accessible, you can now configure Nextcloud, indicating that you have an external model at this address, and the shared api key. So, you must add these lines to your `config/config.php` file.
```
[matias@nube ~]$ cat nextcloud/config/config.php
Expand Down
12 changes: 6 additions & 6 deletions facerecognition-external-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
def require_appkey(view_function):
@wraps(view_function)
def decorated_function(*args, **kwargs):
with open("api.key", "r") as apikey:
key = apikey.read().replace("\n", "")
if (
request.headers.get("x-api-key")
and request.headers.get("x-api-key") == key
):
if 'API_KEY' in os.environ:
key = os.environ.get('API_KEY')
else:
with open('api.key', 'r') as apikey:
key = apikey.read().replace('\n', '')
if request.headers.get('x-api-key') and request.headers.get('x-api-key') == key:
return view_function(*args, **kwargs)
else:
abort(401)
Expand Down

0 comments on commit 324b9eb

Please sign in to comment.