Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #4

Merged
merged 4 commits into from
Dec 24, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: Docker

on:
push:
branches: [ "main" ]
branches: [ ".*" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/isaric/docker-dlib:python-opencv-4.8.0-dlib-19.24
FROM ghcr.io/isaric/docker-dlib:python-opencv-4.8.0-dlib-19.24.2

WORKDIR /python-docker

Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ services:
build: .
ports:
- "5000:5000"
- "5678:5678"
environment:
- SLACK_API_TOKEN=xoxb-241070135347-1373084066946-RKPQf2uO6s7Oi0F8InCgwgkx
- MONGO_CONNECTION_STRING=mongodb://mongo:27017
- READ_SLACK_CHANNEL_ID=C0692LSN85R
- WRITE_SLACK_CHANNEL_ID=G01ASA2SG22
- READ_SLACK_CHANNEL_ID=G01ASA2SG22
- WRITE_SLACK_CHANNEL_ID=C0692LSN85R
- SLACK_VERIFICATION_TOKEN=u99mYutBGu9ckh4JnOk8YU3P
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:
Expand Down
86 changes: 43 additions & 43 deletions face_recog/detector.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
import requests
import cv2
import face_recognition
from datetime import datetime
import logging
import os
import tempfile
import urllib.request as request

class Detector:

haar_cascade = "haarcascade_frontalface_default.xml"

def __init__(self, channel_id, slack_client, repository):
self.channel_id = channel_id
self.slack_client = slack_client
self.repository = repository
self.detector = cv2.CascadeClassifier(self.haar_cascade)


def detect(self, image_url):
image = self._get_image(image_url)
detected_embeddings, boxes = self._get_embeddings(image)
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 = []
for existing in all_embeddings:
for d_embedding in detected_embeddings:
box = boxes[detected_embeddings.index(d_embedding)]
if self._is_match(existing, d_embedding):
name = existing["name"]
logging.info(f"Found match - {name}")
recognized.append((name, box))
else:
unknown.append(box)
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.tobytes())
self._send_unknown_message(unknown, image)
self._save_embeddings(unknown)

def _get_image(self, image_url):
logging.info("Downloading image from %s", image_url)
headers = {'Authorization': 'Bearer ' + os.environ['SLACK_API_TOKEN']}
with tempfile.NamedTemporaryFile() as tmpfile:
img_stream = requests.get(image_url, headers=headers).content
tmpfile.write(img_stream)
return cv2.imread(tmpfile.name, cv2.IMREAD_COLOR)
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):
rectangles = self.detector.detectMultiScale(image, scaleFactor=1.1,
minNeighbors=5, minSize=(50, 50),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(rectangles) > 0:
boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rectangles]
return face_recognition.face_encodings(image, boxes), boxes
return [], []
faces = face_recognition.face_locations(image)
return face_recognition.face_encodings(image, faces), faces

def _compare_with_existing(self, existing, detected, faces):
if existing.collection.count_documents({}) == 0:
logging.info("No existing embeddings found")
return [], tuple(zip(detected, faces))

recognized = []
unknown = []

for e in existing:
for d in detected:
box = faces[detected.index(d)]
if self._is_match(e, d):
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([existing['embedding']], detected)
return True in match

def _save_embeddings(self, embeddings):
for embedding in embeddings:
for embedding, _ in embeddings:
self.repository.save_embedding({"name": "unknown", "embedding": embedding.tolist(), "created_date": datetime.now()})

def _send_unknown_message(self, boxes, image):
message = f"{len(boxes)} unknown faces detected"
for (top, right, bottom, left) in boxes:
cv2.rectangle(image, (left, top), (right, bottom),
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)
self.slack_client.send_image(self.channel_id, message, image)
_, 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:
cv2.rectangle(image, (left, top), (right, bottom),(0, 255, 225), 2)
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(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
cv2.putText(colorim, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
.8, (0, 255, 255), 2)
with tempfile.NamedTemporaryFile(suffix=".png") as tmpfile:
cv2.imwrite(tmpfile.name, image)
f1 = open(tmpfile.name, 'rb')
self.slack_client.send_image_no_msg(channel_id=self.channel_id, image=f1.read())
_, bts = cv2.imencode('.png', colorim)
self.slack_client.send_image_no_msg(channel_id=self.channel_id, image=bts.tostring())



Loading
Loading