Skip to content

Commit

Permalink
Change our protocol. Dont need call open to use the service. Issue #12
Browse files Browse the repository at this point in the history
It is recommended, since the maximum size and the type of files are
reported there, but if not called (Because for example the service was
restarted, or Docker Swarm is being used), automatically initializes
models before using them.

The security model is the use of shared API keys. and in any case we
must reject the requests if they do not have the correct keys, or if
dont comply with the imposed restrictions.
  • Loading branch information
matiasdelellis committed Jan 15, 2024
1 parent d5bb259 commit bc4fbb4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions facerecognition-external-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def cnn_hog_detect(img: numpy.ndarray) -> Tuple[int, list]:

def open_dlib_models():
global CNN_DETECTOR, HOG_DETECTOR, PREDICTOR, FACE_REC

if FACE_REC is not None:
return

# we don't need the cnn detector for model 3
if FACE_MODEL != 3:
CNN_DETECTOR = dlib.cnn_face_detection_model_v1(DETECTOR_PATH)
Expand Down Expand Up @@ -143,9 +147,6 @@ def decorated_function(*args, **kwargs):
@app.route("/detect", methods=["POST"])
@require_appkey
def detect_faces() -> dict:
if FACE_REC is None:
abort(412)

uploaded_file = request.files["file"]

filename = os.path.basename(uploaded_file.filename)
Expand All @@ -157,6 +158,9 @@ def detect_faces() -> dict:
if numpy.shape(img)[0] * numpy.shape(img)[1] > MAX_IMG_SIZE:
abort(412)

if FACE_REC is None:
open_dlib_models()

faces = DETECT_FACES_FUNCTIONS[FACE_MODEL](img)

os.remove(image_path)
Expand All @@ -166,9 +170,6 @@ def detect_faces() -> dict:
@app.route("/compute", methods=["POST"])
@require_appkey
def compute():
if FACE_REC is None:
abort(412)

uploaded_file = request.files["file"]
face_json: dict = json.loads(request.form.get("face"))

Expand All @@ -180,6 +181,9 @@ def compute():
if numpy.shape(img)[0] * numpy.shape(img)[1] > MAX_IMG_SIZE:
abort(412)

if FACE_REC is None:
open_dlib_models()

shape: dlib.full_object_detection = PREDICTOR(img, jsonToRect(face_json))
descriptor: dlib.vector = FACE_REC.compute_face_descriptor(img, shape)

Expand Down

0 comments on commit bc4fbb4

Please sign in to comment.