-
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
Showing
7 changed files
with
43 additions
and
33,794 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
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
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 |
---|---|---|
@@ -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()) | ||
|
||
|
||
|
Oops, something went wrong.