Dropped fswebcam in add command
This commit is contained in:
parent
7c9a8ad849
commit
038667ef09
2 changed files with 50 additions and 54 deletions
97
cli/add.py
97
cli/add.py
|
|
@ -6,6 +6,7 @@ import time
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import cv2
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
# Try to import face_recognition and give a nice error if we can't
|
# Try to import face_recognition and give a nice error if we can't
|
||||||
|
|
@ -26,50 +27,8 @@ path = os.path.dirname(os.path.abspath(__file__))
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(path + "/../config.ini")
|
config.read(path + "/../config.ini")
|
||||||
|
|
||||||
def captureFrame(delay):
|
|
||||||
"""Capture and encode 1 frame of video"""
|
|
||||||
global insert_model
|
|
||||||
|
|
||||||
# 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])
|
|
||||||
|
|
||||||
# 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
|
|
||||||
enc = face_recognition.face_encodings(ref)
|
|
||||||
|
|
||||||
# If 0 faces are detected we can't continue
|
|
||||||
if len(enc) == 0:
|
|
||||||
print("No face detected, aborting")
|
|
||||||
sys.exit()
|
|
||||||
# If more than 1 faces are detected we can't know wich one belongs to the user
|
|
||||||
if len(enc) > 1:
|
|
||||||
print("Multiple faces detected, aborting")
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
clean_enc = []
|
|
||||||
|
|
||||||
# Copy the values into a clean array so we can export it as JSON later on
|
|
||||||
for point in enc[0]:
|
|
||||||
clean_enc.append(point)
|
|
||||||
|
|
||||||
insert_model["data"].append(clean_enc)
|
|
||||||
|
|
||||||
# The current user
|
# The current user
|
||||||
user = sys.argv[1]
|
user = sys.argv[1]
|
||||||
# The name of the tmp frame file to user
|
|
||||||
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 = path + "/../models/" + user + ".dat"
|
enc_file = path + "/../models/" + user + ".dat"
|
||||||
# Known encodings
|
# Known encodings
|
||||||
|
|
@ -110,15 +69,52 @@ insert_model = {
|
||||||
"data": []
|
"data": []
|
||||||
}
|
}
|
||||||
|
|
||||||
print("\nPlease look straight into the camera for 5 seconds")
|
# Open the camera
|
||||||
|
video_capture = cv2.VideoCapture(int(config.get("video", "device_id")))
|
||||||
|
video_capture.read()
|
||||||
|
|
||||||
|
print("\nPlease look straight into the camera")
|
||||||
|
|
||||||
# Give the user time to read
|
# Give the user time to read
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
# Capture with 3 different delays to simulate different camera exposures
|
# Will contain found face encodings
|
||||||
for delay in [30, 6, 0]:
|
enc = []
|
||||||
time.sleep(.3)
|
# Count the amount or read frames
|
||||||
captureFrame(delay)
|
frames = 0
|
||||||
|
|
||||||
|
# Loop through frames till we hit a timeout
|
||||||
|
while frames < 60:
|
||||||
|
frames += 1
|
||||||
|
|
||||||
|
# Grab a single frame of video
|
||||||
|
# Don't remove ret, it doesn't work without it
|
||||||
|
ret, frame = video_capture.read()
|
||||||
|
|
||||||
|
# Get the encodings in the frame
|
||||||
|
enc = face_recognition.face_encodings(frame)
|
||||||
|
|
||||||
|
# If we've found at least one, we can continue
|
||||||
|
if len(enc) > 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
# If 0 faces are detected we can't continue
|
||||||
|
if len(enc) == 0:
|
||||||
|
print("No face detected, aborting")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# If more than 1 faces are detected we can't know wich one belongs to the user
|
||||||
|
if len(enc) > 1:
|
||||||
|
print("Multiple faces detected, aborting")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
clean_enc = []
|
||||||
|
|
||||||
|
# Copy the values into a clean array so we can export it as JSON later on
|
||||||
|
for point in enc[0]:
|
||||||
|
clean_enc.append(point)
|
||||||
|
|
||||||
|
insert_model["data"].append(clean_enc)
|
||||||
|
|
||||||
# Insert full object into the list
|
# Insert full object into the list
|
||||||
encodings.append(insert_model)
|
encodings.append(insert_model)
|
||||||
|
|
@ -127,7 +123,6 @@ encodings.append(insert_model)
|
||||||
with open(enc_file, "w") as datafile:
|
with open(enc_file, "w") as datafile:
|
||||||
json.dump(encodings, datafile)
|
json.dump(encodings, datafile)
|
||||||
|
|
||||||
# Remove any left over temp files
|
# Give let the user know how it went
|
||||||
os.remove(tmp_file)
|
print("Scan complete")
|
||||||
|
print("\nAdded a new model to " + user)
|
||||||
print("Done.")
|
|
||||||
|
|
|
||||||
|
|
@ -125,10 +125,11 @@ while True:
|
||||||
print("\nFrames searched: " + str(frames) + " (" + str(round(float(frames) / (timings[4] - timings[2]), 2)) + " fps)")
|
print("\nFrames searched: " + str(frames) + " (" + str(round(float(frames) / (timings[4] - timings[2]), 2)) + " fps)")
|
||||||
print("Certainty of winning frame: " + str(round(match * 10, 3)))
|
print("Certainty of winning frame: " + str(round(match * 10, 3)))
|
||||||
|
|
||||||
exposures = ["long", "medium", "short"]
|
# Catch older 3-encoding models
|
||||||
model_id = math.floor(float(match_index) / 3)
|
if not match_index in models:
|
||||||
|
match_index = 0
|
||||||
|
|
||||||
print("Winning model: " + str(model_id) + " (\"" + models[model_id]["label"] + "\") using " + exposures[match_index % 3] + " exposure\n")
|
print("Winning model: " + str(match_index) + " (\"" + models[match_index]["label"] + "\")")
|
||||||
|
|
||||||
# End peacegully
|
# End peacegully
|
||||||
stop(0)
|
stop(0)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue