Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
isaric committed Dec 21, 2023
1 parent 4d82af6 commit 005e3c3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33,794 deletions.
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
71 changes: 38 additions & 33 deletions face_recog/detector.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,91 @@
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']}
img_stream = requests.get(image_url, headers=headers).content
return face_recognition.load_image_file(img_stream)
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):
boxes = face_recognition.face_locations(image)
return face_recognition.face_encodings(image, boxes), boxes
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:
def _send_unknown_message(self, unknown, image):
message = f"{len(unknown)} unknown faces detected"
for _, (top, right, bottom, left) in unknown:
cv2.rectangle(image, (left, top), (right, bottom),
(0, 255, 225), 2)
self.slack_client.send_image(self.channel_id, message, image)
_, bts = cv2.imencode('.png', image)
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)
y = top - 15 if top - 15 > 15 else top + 15
cv2.putText(image, 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', image)
self.slack_client.send_image_no_msg(channel_id=self.channel_id, image=bts.tostring())



Loading

0 comments on commit 005e3c3

Please sign in to comment.