Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Martlgap committed Mar 11, 2021
0 parents commit ca62dc7
Show file tree
Hide file tree
Showing 12 changed files with 1,232 additions and 0 deletions.
137 changes: 137 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# IDEs
.idea
.vscode
.toml

# MacOS
.DS_Store
Empty file added FaceIDLight/__init__.py
Empty file.
75 changes: 75 additions & 0 deletions FaceIDLight/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-

# MIT License
#
# Copyright (c) 2021 Martin Knoche
#
# 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.


import threading
import cv2
import time


class Camera:
def __init__(self, stream_id=0):
self.stream_id = stream_id
self.currentFrame = None
self.ret = False
self.capture = cv2.VideoCapture(stream_id)
self.pill2kill = threading.Event()
self.thread = threading.Thread(target=self.update_frame, args=())
self.thread.start() # TODO check if really threading necessary

# Continually updates the frame
def update_frame(self):
while True:
self.ret, self.currentFrame = self.capture.read()

while self.currentFrame is None: # Continually grab frames until we get a good one
self.capture.read()

# Get current frame
def get_frame(self):
return self.ret, self.currentFrame

def screen(self, function):
window_name = "Streaming from {}".format(self.stream_id)
cv2.namedWindow(window_name)
last = 0
while True:
ret, frame = self.get_frame()
if ret:
frame = function(frame)
frame = cv2.putText(
frame,
"FPS{:5.1f}".format(1 / (time.time() - last)),
(frame.shape[1]-80, 30),
cv2.FONT_HERSHEY_PLAIN,
1,
(0, 255, 0),
)
last = time.time()
cv2.imshow(window_name, frame)
if cv2.waitKey(1) & 0xFF == ord("q"): # TODO MacOS window freezes after exiting here
break
self.pill2kill.set()
self.thread.join()
cv2.destroyWindow(window_name)
107 changes: 107 additions & 0 deletions FaceIDLight/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# -*- coding: utf-8 -*-

# MIT License
#
# Copyright (c) 2021 Martin Knoche
#
# 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.


import cv2
from FaceIDLight.camera import Camera
from FaceIDLight.tools import FaceID


# TODO Why fps is so low if multi face? Should be parallel!


class Demonstrator:
def __init__(self, gal_dir=None):
# Initialization
self.cam = Camera()
self.FaceID = FaceID(gal_dir=gal_dir)

# Set OpenCV defaults
self.color = (0, 0, 255)
self.font = cv2.QT_FONT_NORMAL

def annotate(self, img, results):
num = 0
for result in results:
face, detections, ids = result
(
bbox,
points,
conf,
) = detections
name, gal_face, dist, id_conf = ids

# Add BoundingBox
img = cv2.rectangle(img, tuple(bbox[0]), tuple(bbox[1]), self.color)

# Add LandmarkPoints
for point in points:
img = cv2.circle(img, tuple(point), 5, self.color)

# Add Confidence Value
img = cv2.putText(
img,
"Detect-Conf: {:0.2f}%".format(conf * 100),
(int(bbox[0, 0]), int(bbox[0, 1]) - 20),
self.font,
0.7,
(0, 0, 255),
)

# Add aligned face onto img
img[0 : face.shape[1], num : num + face.shape[0]] = face
# Subject
img = cv2.putText(
img, "Subject", (num, 10), self.font, 0.4, (255, 255, 255)
)

# Add Prediction, Distance and Confidence
img = cv2.putText(
img, "{}".format(name), (int(bbox[0, 0]), int(bbox[0, 1]) - 80), self.font, 0.7, (255, 255, 0)
)
img = cv2.putText(
img, "Emb-Dist: {:0.2f}".format(dist), (int(bbox[0, 0]), int(bbox[0, 1] - 60)), self.font, 0.7, self.color
)
img = cv2.putText(
img, "ID-Conf: {:0.2f} %".format(id_conf * 100), (int(bbox[0, 0]), int(bbox[0, 1] - 40)), self.font, 0.7, self.color
)

# Add gal face onto img
if name != "Other":
img[112 : 112 + gal_face.shape[1], num : num + gal_face.shape[0]] = gal_face
# Match
img = cv2.putText(
img, "GalleryMatch", (num, 112+10), self.font, 0.4, (255, 255, 255)
)

num += 112
return img

def identification(self, frame):
results = self.FaceID.recognize_faces(frame)
frame = self.annotate(frame, results)
return frame

def run(self):
self.cam.screen(self.identification)
77 changes: 77 additions & 0 deletions FaceIDLight/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-

# MIT License
#
# Copyright (c) 2021 Martin Knoche
#
# 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.


import tempfile
import os
import hashlib
from tqdm import tqdm
from zipfile import ZipFile
from urllib.request import urlopen


def get_file(origin, file_hash, is_zip=False):
tmp_file = os.path.join(tempfile.gettempdir(), "FaceIDLight", origin.split("/")[-1])
os.makedirs(os.path.dirname(tmp_file), exist_ok=True)
if not os.path.exists(tmp_file):
download = True
else:
hasher = hashlib.sha256()
with open(tmp_file, "rb") as file:
for chunk in iter(lambda: file.read(65535), b""):
hasher.update(chunk)
if not hasher.hexdigest() == file_hash:
print(
"A local file was found, but it seems to be incomplete or outdated because the file hash does not "
"match the original value of " + file_hash + " so data will be downloaded."
)
download = True
else:
download = False

if download:
response = urlopen(origin)
with tqdm.wrapattr(
open(tmp_file, "wb"),
"write",
miniters=1,
desc="Downloading " + origin.split("/")[-1] + " to: " + tmp_file,
total=getattr(response, "length", None),
) as file:
for chunk in response:
file.write(chunk)
file.close()
if is_zip:
with ZipFile(tmp_file, "r") as zipObj:
zipObj.extractall(tmp_file.split(".")[0])
tmp_file = os.path.join(tmp_file.split(".")[0])
return tmp_file


def get_hash(filepath):
hasher = hashlib.sha256()
with open(filepath, "rb") as file:
for chunk in iter(lambda: file.read(65535), b""):
hasher.update(chunk)
return hasher.hexdigest()
Loading

0 comments on commit ca62dc7

Please sign in to comment.