Moved learn.py to the CLI and added a help page
This commit is contained in:
parent
22c72432a7
commit
494e414949
4 changed files with 62 additions and 44 deletions
8
cli.py
8
cli.py
|
|
@ -1,7 +1,15 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
if (len(sys.argv) == 1):
|
||||||
|
import cli.help
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
cmd = sys.argv[1]
|
cmd = sys.argv[1]
|
||||||
|
|
||||||
if cmd == "list":
|
if cmd == "list":
|
||||||
import cli.list
|
import cli.list
|
||||||
|
elif cmd == "help":
|
||||||
|
import cli.help
|
||||||
|
elif cmd == "add":
|
||||||
|
import cli.add
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,33 @@
|
||||||
# Save the face of the user in encoded form
|
# Save the face of the user in encoded form
|
||||||
|
|
||||||
# Import required modules
|
# Import required modules
|
||||||
import face_recognition
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Import config and extra functions
|
# Import config
|
||||||
import configparser
|
import configparser
|
||||||
import utils
|
|
||||||
|
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__))
|
||||||
|
|
||||||
# Read config from disk
|
# Read config from disk
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(os.path.dirname(os.path.abspath(__file__)) + "/config.ini")
|
config.read(path + "/../config.ini")
|
||||||
|
|
||||||
def captureFrame(delay):
|
def captureFrame(delay):
|
||||||
"""Capture and encode 1 frame of video"""
|
"""Capture and encode 1 frame of video"""
|
||||||
global encodings
|
global insert_model
|
||||||
|
|
||||||
# Call fswebcam to save a frame to /tmp with a set delay
|
# Call fswebcam to save a frame to /tmp with a set delay
|
||||||
exit_code = subprocess.call(["fswebcam", "-S", str(delay), "--no-banner", "-d", "/dev/video" + str(config.get("video", "device_id")), tmp_file])
|
exit_code = subprocess.call(["fswebcam", "-S", str(delay), "--no-banner", "-d", "/dev/video" + str(config.get("video", "device_id")), tmp_file])
|
||||||
|
|
@ -54,36 +63,48 @@ def captureFrame(delay):
|
||||||
for point in enc[0]:
|
for point in enc[0]:
|
||||||
clean_enc.append(point)
|
clean_enc.append(point)
|
||||||
|
|
||||||
encodings.append(clean_enc)
|
insert_model["data"].append(clean_enc)
|
||||||
|
|
||||||
# The current user
|
# The current user
|
||||||
user = os.environ.get("USER")
|
user = os.environ.get("USER")
|
||||||
# The name of the tmp frame file to user
|
# The name of the tmp frame file to user
|
||||||
tmp_file = "/tmp/howdy_" + user + ".jpg"
|
tmp_file = "/tmp/howdy_" + user + ".jpg"
|
||||||
# The permanent file to store the encoded model in
|
# The permanent file to store the encoded model in
|
||||||
enc_file = "./models/" + user + ".dat"
|
enc_file = path + "/../models/" + user + ".dat"
|
||||||
# Known encodings
|
# Known encodings
|
||||||
encodings = []
|
encodings = []
|
||||||
|
|
||||||
# Make the ./models folder if it doesn't already exist
|
# Make the ./models folder if it doesn't already exist
|
||||||
if not os.path.exists("models"):
|
if not os.path.exists(path + "/../models"):
|
||||||
print("No face model folder found, creating one")
|
print("No face model folder found, creating one")
|
||||||
os.makedirs("models")
|
os.makedirs(path + "/../models")
|
||||||
|
|
||||||
# To try read a premade encodings file if it exists
|
# To try read a premade encodings file if it exists
|
||||||
try:
|
try:
|
||||||
encodings = json.load(open(enc_file))
|
encodings = json.load(open(enc_file))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
encodings = False
|
|
||||||
|
|
||||||
# If a file does exist, ask the user what needs to be done
|
|
||||||
if encodings != False:
|
|
||||||
encodings = utils.print_menu(encodings)
|
|
||||||
else:
|
|
||||||
encodings = []
|
encodings = []
|
||||||
|
|
||||||
print("\nLearning face for the user account " + user)
|
print("Adding face model for the user account " + user)
|
||||||
print("Please look straight into the camera for 5 seconds")
|
|
||||||
|
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 != "":
|
||||||
|
label = label_in
|
||||||
|
|
||||||
|
insert_model = {
|
||||||
|
"time": int(time.time()),
|
||||||
|
"label": label,
|
||||||
|
"id": len(encodings),
|
||||||
|
"data": []
|
||||||
|
}
|
||||||
|
|
||||||
|
print("\nPlease look straight into the camera for 5 seconds")
|
||||||
|
|
||||||
# Give the user time to read
|
# Give the user time to read
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
@ -93,6 +114,8 @@ for delay in [30, 6, 0]:
|
||||||
time.sleep(.3)
|
time.sleep(.3)
|
||||||
captureFrame(delay)
|
captureFrame(delay)
|
||||||
|
|
||||||
|
encodings.append(insert_model)
|
||||||
|
|
||||||
# Save the new encodings to disk
|
# Save the new encodings to disk
|
||||||
with open(enc_file, "w") as datafile:
|
with open(enc_file, "w") as datafile:
|
||||||
json.dump(encodings, datafile)
|
json.dump(encodings, datafile)
|
||||||
14
cli/help.py
Normal file
14
cli/help.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
print("Howdy IR face recognition\n")
|
||||||
|
|
||||||
|
print("Usage:")
|
||||||
|
print(" howdy <command> [argument]\n")
|
||||||
|
|
||||||
|
print("Commands:")
|
||||||
|
print(" help Show this help page")
|
||||||
|
print(" list List all saved face models for the current user")
|
||||||
|
print(" add Add a new face model for the current user")
|
||||||
|
print(" remove [id] Remove a specific model")
|
||||||
|
print(" clear Remove all face models for the current user")
|
||||||
|
|
||||||
|
print("\nFor support please visit")
|
||||||
|
print("https://github.com/Boltgolt/howdy")
|
||||||
27
utils.py
27
utils.py
|
|
@ -1,27 +0,0 @@
|
||||||
# Useful support functions
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
def print_menu(encodings):
|
|
||||||
"""Show a menu asking the user what he wants to do"""
|
|
||||||
if len(encodings) == 3:
|
|
||||||
print("There is 1 existing face model for this user")
|
|
||||||
else:
|
|
||||||
print("There are " + str(int(len(encodings) / 3)) + " existing face models for this user")
|
|
||||||
print("What do you want to do?\n")
|
|
||||||
|
|
||||||
print("1: Add additional face model")
|
|
||||||
print("2: Overwrite older model(s)")
|
|
||||||
print("0: Exit")
|
|
||||||
|
|
||||||
com = input("Option: ")
|
|
||||||
|
|
||||||
if com == "1":
|
|
||||||
return encodings
|
|
||||||
elif com == "2":
|
|
||||||
return []
|
|
||||||
elif com == "0":
|
|
||||||
sys.exit()
|
|
||||||
else:
|
|
||||||
print("Invalid option '" + com + "'\n")
|
|
||||||
return print_menu(encodings)
|
|
||||||
Loading…
Reference in a new issue