-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Extract face_recognition and dlib into its own context - Prevents initilizing the cuda context multiple times from dlib and pytorch
- Loading branch information
Showing
4 changed files
with
90 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import requests | ||
import numpy as np | ||
|
||
|
||
def get_face_encodings(image_path, known_face_locations): | ||
json = { | ||
"source": image_path, | ||
"face_locations": known_face_locations, | ||
} | ||
face_encoding = requests.post( | ||
"http://localhost:8005/face-encodings", json=json | ||
).json() | ||
|
||
face_encodings_list = face_encoding["encodings"] | ||
face_encodings = [np.array(enc) for enc in face_encodings_list] | ||
|
||
return face_encodings | ||
|
||
|
||
def get_face_locations(image_path, model="hog"): | ||
json = {"source": image_path, "model": model} | ||
face_locations = requests.post( | ||
"http://localhost:8005/face-locations", json=json | ||
).json() | ||
return face_locations["face_locations"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import gevent | ||
from flask import Flask, request | ||
from gevent.pywsgi import WSGIServer | ||
import face_recognition | ||
import numpy as np | ||
import PIL | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
def log(message): | ||
print("face_recognition: {}".format(message)) | ||
|
||
|
||
@app.route("/face-encodings", methods=["POST"]) | ||
def create_face_encodings(): | ||
try: | ||
data = request.get_json() | ||
source = data["source"] | ||
face_locations = data["face_locations"] | ||
except Exception: | ||
return "", 400 | ||
|
||
image = np.array(PIL.Image.open(source)) | ||
face_encodings = face_recognition.face_encodings( | ||
image, | ||
known_face_locations=face_locations, | ||
) | ||
# Convert NumPy arrays to Python lists | ||
face_encodings_list = [enc.tolist() for enc in face_encodings] | ||
# Log number of face encodings | ||
log(f"created face_encodings={len(face_encodings_list)}") | ||
return {"encodings": face_encodings_list}, 201 | ||
|
||
|
||
@app.route("/face-locations", methods=["POST"]) | ||
def create_face_locations(): | ||
try: | ||
data = request.get_json() | ||
source = data["source"] | ||
model = data["model"] | ||
except Exception: | ||
return "", 400 | ||
|
||
image = np.array(PIL.Image.open(source)) | ||
face_locations = face_recognition.face_locations(image, model=model) | ||
log(f"created face_location={face_locations}") | ||
return {"face_locations": face_locations}, 201 | ||
|
||
|
||
if __name__ == "__main__": | ||
log("service starting") | ||
server = WSGIServer(("0.0.0.0", 8005), app) | ||
server_thread = gevent.spawn(server.serve_forever) | ||
gevent.joinall([server_thread]) |