-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1775038
Showing
12 changed files
with
469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
name: Docker | ||
|
||
# This workflow uses actions that are not certified by GitHub. | ||
# They are provided by a third-party and are governed by | ||
# separate terms of service, privacy policy, and support | ||
# documentation. | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
# Publish semver tags as releases. | ||
tags: [ 'v*.*.*' ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
# Use docker.io for Docker Hub if empty | ||
REGISTRY: ghcr.io | ||
# github.repository as <account>/<repo> | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
TAG_ARM64: path-variable/python-slack-fr:${{ github.ref_name }}-arm64 | ||
|
||
TAG_AMD64: path-variable/python-slack-fr:${{ github.ref_name }}-amd64 | ||
|
||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
# This is used to complete the identity challenge | ||
# with sigstore/fulcio when running outside of PRs. | ||
id-token: write | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
# Install the cosign tool except on PR | ||
# https://github.com/sigstore/cosign-installer | ||
- name: Install cosign | ||
if: github.event_name != 'pull_request' | ||
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1 | ||
with: | ||
cosign-release: 'v2.1.1' | ||
|
||
# Set up BuildKit Docker container builder to be able to build | ||
# multi-platform images and export cache | ||
# https://github.com/docker/setup-buildx-action | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 | ||
|
||
# Login against a Docker registry except on PR | ||
# https://github.com/docker/login-action | ||
- name: Log into registry ${{ env.REGISTRY }} | ||
if: github.event_name != 'pull_request' | ||
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Extract metadata (tags, labels) for Docker | ||
# https://github.com/docker/metadata-action | ||
- name: Extract Docker metadata | ||
id: meta-arm64 | ||
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_ARM64 }} | ||
|
||
- name: Extract Docker metadata | ||
id: meta-amd64 | ||
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_AMD64 }} | ||
|
||
# Build and push Docker image with Buildx (don't push on PR) | ||
# https://github.com/docker/build-push-action | ||
- name: Build and push Docker image | ||
id: build-and-push-arm64 | ||
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | ||
with: | ||
context: . | ||
file: dockerfile-arm64 | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: ${{ env.REGISTRY }}/${{ env.TAG_ARM64 }} | ||
labels: ${{ steps.meta-arm64.outputs.labels }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max | ||
platforms: linux/arm64 | ||
|
||
- name: Build and push Docker image | ||
id: build-and-push-amd64 | ||
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | ||
with: | ||
context: . | ||
file: dockerfile-amd64 | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: ${{ env.REGISTRY }}/${{ env.TAG_AMD64 }} | ||
labels: ${{ steps.meta-amd64.outputs.labels }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max | ||
platforms: linux/amd64 | ||
|
||
# Sign the resulting Docker image digest except on PRs. | ||
# This will only write to the public Rekor transparency log when the Docker | ||
# repository is public to avoid leaking data. If you would like to publish | ||
# transparency data even for private images, pass --force to cosign below. | ||
# https://github.com/sigstore/cosign | ||
- name: Sign the published Docker image | ||
if: ${{ github.event_name != 'pull_request' }} | ||
env: | ||
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable | ||
TAGS: ${{ steps.meta.outputs.tags }} | ||
DIGEST: ${{ steps.build-and-push-arm64.outputs.digest }} | ||
# This step uses the identity token to provision an ephemeral certificate | ||
# against the sigstore community Fulcio instance. | ||
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} | ||
- name: Sign the published Docker image | ||
if: ${{ github.event_name != 'pull_request' }} | ||
env: | ||
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable | ||
TAGS: ${{ steps.meta.outputs.tags }} | ||
DIGEST: ${{ steps.build-and-push-amd64.outputs.digest }} | ||
# This step uses the identity token to provision an ephemeral certificate | ||
# against the sigstore community Fulcio instance. | ||
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.venv | ||
.idea | ||
*.iml | ||
__pycache__ | ||
debug.log | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Remote Attach", | ||
"type": "python", | ||
"request": "attach", | ||
"connect": { | ||
"host": "localhost", | ||
"port": 5678 | ||
}, | ||
"pathMappings": [ | ||
{ | ||
"localRoot": "${workspaceFolder}", | ||
"remoteRoot": "." | ||
} | ||
], | ||
"justMyCode": true | ||
}, | ||
{ | ||
"name": "Python: Flask", | ||
"type": "python", | ||
"request": "launch", | ||
"module": "flask", | ||
"env": { | ||
"FLASK_APP": "main.py", | ||
"FLASK_DEBUG": "1", | ||
"SLACK_API_TOKEN": "", | ||
"MONGO_CONNECTION_STRING": "mongodb://localhost:27017", | ||
"WRITE_SLACK_CHANNEL_ID": "", | ||
"READ_SLACK_CHANNEL_ID": "", | ||
"SLACK_VERIFICATION_TOKEN": "" | ||
}, | ||
"args": [ | ||
"run", | ||
"--no-debugger", | ||
"--no-reload", | ||
"--host=0.0.0.0" | ||
], | ||
"jinja": true, | ||
"justMyCode": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Ivan Šarić | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: '3.0' | ||
services: | ||
api: | ||
build: | ||
context: . | ||
dockerfile: dockerfile-amd64 | ||
ports: | ||
- "5000:5000" | ||
- "5678:5678" | ||
environment: | ||
- SLACK_API_TOKEN= | ||
- MONGO_CONNECTION_STRING= | ||
- READ_SLACK_CHANNEL_ID= | ||
- WRITE_SLACK_CHANNEL_ID= | ||
- SLACK_VERIFICATION_TOKEN= | ||
depends_on: | ||
- mongo | ||
command: 'python3 -m debugpy --listen 0.0.0.0:5678 -m flask --app main run --host=0.0.0.0' | ||
mongo: | ||
image: mongo | ||
ports: | ||
- "27017:27017" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM ghcr.io/isaric/docker-dlib:python-opencv-4.8.0-dlib-19.24.2-amd64 | ||
|
||
WORKDIR /python-docker | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip3 install -r requirements.txt | ||
RUN pip install debugpy | ||
|
||
COPY . . | ||
|
||
EXPOSE 5000 | ||
|
||
CMD [ "python3", "-m" , "flask", "--app", "main", "run", "--host=0.0.0.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM ghcr.io/isaric/docker-dlib:python-opencv-4.8.0-dlib-19.24.2-arm64 | ||
|
||
WORKDIR /python-docker | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip3 install -r requirements.txt | ||
RUN pip install debugpy | ||
|
||
COPY . . | ||
|
||
EXPOSE 5000 | ||
|
||
CMD [ "python3", "-m" , "flask", "--app", "main", "run", "--host=0.0.0.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import cv2 | ||
import face_recognition | ||
from datetime import datetime | ||
import logging | ||
import os | ||
import urllib.request as request | ||
|
||
class Detector: | ||
|
||
def __init__(self, channel_id, slack_client, repository): | ||
self.channel_id = channel_id | ||
self.slack_client = slack_client | ||
self.repository = repository | ||
|
||
def detect(self, image_url): | ||
image = self._get_image(image_url) | ||
detected_embeddings, faces = self._get_embeddings(image) | ||
if len(detected_embeddings) == 0: | ||
logging.info("No faces detected") | ||
return | ||
|
||
all_embeddings = self.repository.get_all_embeddings() | ||
recognized, unknown = self._compare_with_existing(all_embeddings, detected_embeddings, faces) | ||
|
||
if len(recognized) > 0: | ||
logging.info("Sending message with recognized faces") | ||
self._send_recognized_message(image,recognized) | ||
|
||
if len(unknown) > 0: | ||
logging.info("Sending message with unknown faces") | ||
self._send_unknown_message(unknown, image) | ||
self._save_embeddings(unknown) | ||
|
||
def _get_image(self, image_url): | ||
logging.info("Downloading image from %s", image_url) | ||
req = request.Request(image_url) | ||
req.add_header('Authorization', 'Bearer ' + os.environ['SLACK_API_TOKEN']) | ||
return face_recognition.load_image_file(request.urlopen(req)) | ||
|
||
def _get_embeddings(self, image): | ||
faces = face_recognition.face_locations(image) | ||
return face_recognition.face_encodings(image, faces), faces | ||
|
||
def _compare_with_existing(self, existing, detected, faces): | ||
if len(existing) == 0: | ||
logging.info("No existing embeddings found") | ||
return [], tuple(zip(detected, faces)) | ||
|
||
recognized = [] | ||
unknown = [] | ||
|
||
for d in detected: | ||
box = faces[detected.index(d)] | ||
e, m = self._is_match(existing, d) | ||
if m: | ||
name = e["name"] | ||
logging.info(f"Found match - {name}") | ||
recognized.append((name, box)) | ||
else: | ||
unknown.append((d,box)) | ||
|
||
return recognized, unknown | ||
|
||
|
||
def _is_match(self, existing, detected): | ||
match = face_recognition.compare_faces([e["embedding"] for e in existing], detected) | ||
for i,m in enumerate(match): | ||
if m: | ||
return existing[i], True | ||
return None, False | ||
|
||
def _save_embeddings(self, embeddings): | ||
for embedding, _ in embeddings: | ||
self.repository.save_embedding({"name": "unknown", "embedding": embedding.tolist(), "created_date": datetime.now()}) | ||
|
||
def _send_unknown_message(self, unknown, image): | ||
message = f"{len(unknown)} unknown faces detected" | ||
colorim = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | ||
for _, (top, right, bottom, left) in unknown: | ||
cv2.rectangle(colorim, (left, top), (right, bottom), | ||
(0, 255, 225), 2) | ||
_, bts = cv2.imencode('.png', colorim) | ||
self.slack_client.send_image(self.channel_id, message, bts.tostring()) | ||
|
||
def _send_recognized_message(self, image, recognized): | ||
for (name,(top, right, bottom, left)) in recognized: | ||
colorim = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | ||
cv2.rectangle(colorim, (left, top), (right, bottom),(0, 255, 225), 2) | ||
y = top - 15 if top - 15 > 15 else top + 15 | ||
cv2.putText(colorim, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, | ||
.8, (0, 255, 255), 2) | ||
_, bts = cv2.imencode('.png', colorim) | ||
self.slack_client.send_image_no_msg(channel_id=self.channel_id, image=bts.tostring()) | ||
|
||
|
||
|
Oops, something went wrong.