0
0
Fork 0
mirror of https://github.com/boltgolt/howdy.git synced 2024-09-19 09:51:19 +02:00
howdy/cli/add.py

127 lines
3.1 KiB
Python
Raw Normal View History

2018-01-05 16:37:00 +01:00
# Save the face of the user in encoded form
# Import required modules
2018-01-05 01:59:44 +01:00
import subprocess
import time
import os
2018-01-05 14:34:18 +01:00
import sys
import json
# Import config
import configparser
try:
import face_recognition
except ImportError as err:
print(err)
print("\nCan't import face_recognition module, check the output of")
print("pip3 show face_recognition")
sys.exit()
path = os.path.dirname(os.path.abspath(__file__))
2018-01-05 14:34:18 +01:00
# Read config from disk
config = configparser.ConfigParser()
config.read(path + "/../config.ini")
2018-01-05 14:34:18 +01:00
def captureFrame(delay):
2018-01-05 16:37:00 +01:00
"""Capture and encode 1 frame of video"""
global insert_model
2018-01-05 16:37:00 +01:00
# Call fswebcam to save a frame to /tmp with a set delay
2018-02-01 19:27:02 +01:00
exit_code = subprocess.call(["fswebcam", "-S", str(delay), "--no-banner", "-d", "/dev/video" + str(config.get("video", "device_id")), tmp_file])
2018-01-05 14:34:18 +01:00
2018-02-01 19:27:02 +01:00
# Check if fswebcam exited normally
if (exit_code != 0):
print("Webcam frame capture failed!")
print("Please make sure fswebcam is installed on this system")
sys.exit()
# Try to load the image from disk
try:
ref = face_recognition.load_image_file(tmp_file)
except FileNotFoundError:
print("No webcam frame captured, check if /dev/video" + str(config.get("video", "device_id")) + " is the right webcam")
sys.exit()
# Make a face encoding from the loaded image
2018-01-05 14:34:18 +01:00
enc = face_recognition.face_encodings(ref)
2018-01-05 16:37:00 +01:00
# If 0 faces are detected we can't continue
2018-01-05 14:34:18 +01:00
if len(enc) == 0:
print("No face detected, aborting")
sys.exit()
2018-01-05 16:37:00 +01:00
# If more than 1 faces are detected we can't know wich one belongs to the user
2018-01-05 14:34:18 +01:00
if len(enc) > 1:
print("Multiple faces detected, aborting")
sys.exit()
clean_enc = []
2018-01-05 16:37:00 +01:00
# Copy the values into a clean array so we can export it as JSON later on
2018-01-05 14:34:18 +01:00
for point in enc[0]:
clean_enc.append(point)
insert_model["data"].append(clean_enc)
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# The current user
2018-02-13 22:03:03 +01:00
user = sys.argv[1]
2018-01-05 16:37:00 +01:00
# The name of the tmp frame file to user
2018-01-05 14:34:18 +01:00
tmp_file = "/tmp/howdy_" + user + ".jpg"
2018-01-05 16:37:00 +01:00
# The permanent file to store the encoded model in
enc_file = path + "/../models/" + user + ".dat"
2018-01-05 16:37:00 +01:00
# Known encodings
2018-01-05 14:34:18 +01:00
encodings = []
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# Make the ./models folder if it doesn't already exist
if not os.path.exists(path + "/../models"):
2018-01-05 01:59:44 +01:00
print("No face model folder found, creating one")
os.makedirs(path + "/../models")
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# To try read a premade encodings file if it exists
2018-01-05 14:34:18 +01:00
try:
encodings = json.load(open(enc_file))
except FileNotFoundError:
encodings = []
2018-01-05 01:59:44 +01:00
print("Adding face model for the user account " + user)
label = "Initial model"
if len(encodings) > 0:
label = "Model #" + str(len(encodings) + 1)
label_in = input("Enter a label for this new model [" + label + "]: ")
if label_in != "":
2018-02-11 01:54:44 +01:00
label = label_in[:24]
insert_model = {
"time": int(time.time()),
"label": label,
"id": len(encodings),
"data": []
}
print("\nPlease look straight into the camera for 5 seconds")
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# Give the user time to read
2018-01-05 14:34:18 +01:00
time.sleep(2)
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# Capture with 3 different delays to simulate different camera exposures
2018-01-05 14:34:18 +01:00
for delay in [30, 6, 0]:
time.sleep(.3)
captureFrame(delay)
2018-01-05 01:59:44 +01:00
encodings.append(insert_model)
2018-01-05 16:37:00 +01:00
# Save the new encodings to disk
2018-01-05 14:34:18 +01:00
with open(enc_file, "w") as datafile:
json.dump(encodings, datafile)
2018-01-05 01:59:44 +01:00
2018-01-05 16:37:00 +01:00
# Remove any left over temp files
2018-01-05 14:34:18 +01:00
os.remove(tmp_file)
2018-01-05 01:59:44 +01:00
print("Done.")