-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20421ea
commit 73230eb
Showing
449 changed files
with
487 additions
and
0 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,35 @@ | ||
This example demonstrates usage of FaceDB to query an embedding from a DB. Please go through code to understand FaceDB usage. | ||
|
||
> This example only works in Linux. FaceDB is currently not available for Windows. | ||
> Free version of Face DB only supports 50 faces. | ||
> Email us at contact@baseapp.com for further queries. | ||
![face_db](https://github.com/baseapp/DeepSight-Face-Recognition-SDK/blob/master/examples/face_db/static/gen/2018-07-31_144814.jpg) | ||
|
||
|
||
### Running | ||
|
||
* To run this example, install the following dependencies | ||
|
||
```sh | ||
pip install requests | ||
pip install opencv-python | ||
pip install scipy flask | ||
``` | ||
* Next, start `Deepsight Face SDK` and let it run. | ||
* Next, start `facedb --serve` and let it run | ||
* Start the flask app using `python app.py` | ||
* The application will say `fifa db initialized` | ||
* Open a browser and point to `localhost:5101` | ||
* Use the gui to upload a photo | ||
* The application will return closest matching fifa celebrity | ||
|
||
### Files | ||
|
||
* `facedb` - This is the free version of faceDB binary. It supports upto 50 faces. | ||
* `app.py` - This is the flask app. | ||
* `dsface.py` - A simple python wrapper to generate embeddings using Deepsight Face | ||
* `facedb.py` - A simple python wrapper to `facedb` binary | ||
* `save_to_facedb.py` - This demonstrates how to save embeddings in `facedb`. It reads values from `fifadb.json` and stores them in `facedb`. It then dumps the database so that it can be loaded later. |
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,73 @@ | ||
""" | ||
* Author: Azmath Moosa | ||
* Created: 31st July 2018 | ||
* Description: Flask app to demonstrate use of FaceDB. | ||
This app can be accessed at localhost:5101 | ||
It takes an image and finds the closest matching | ||
FIFA celebrity. | ||
It uses Deepsight SDK for face detection and retrieves embeddings. | ||
It then uses FaceDB to query the closest embeddings and retrieves celeb name. | ||
It then shows the celeb's photo next to the input photo. | ||
Please launch and run facedb and dsFace manually | ||
* Dependencies: python requests, json, flask, opencv-python, numpy | ||
""" | ||
|
||
from flask import Flask, render_template, request, make_response, Markup, send_from_directory | ||
import io | ||
import numpy as np | ||
import cv2 | ||
import utils | ||
import base64 | ||
import datetime | ||
from facedb import FaceDBHandler | ||
from dsface import DSFaceHandler | ||
|
||
app = Flask(__name__) | ||
|
||
faceDB = FaceDBHandler(filename="fifadb") | ||
dsFace = DSFaceHandler(face_rec=True,face_lmk=True) | ||
|
||
|
||
@app.route("/") | ||
def index(): | ||
return render_template("input.html") | ||
|
||
@app.route("/result", methods=['POST']) | ||
def result(): | ||
photo = request.files['face'] | ||
in_memory_file = io.BytesIO() | ||
photo.save(in_memory_file) | ||
data = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8) | ||
color_image_flag = 1 | ||
img = cv2.imdecode(data, color_image_flag) | ||
|
||
vec, crop = dsFace.get_embedding(img) | ||
|
||
search = faceDB.search_database(vec) | ||
|
||
print(search) | ||
|
||
celebname = search["meta"] | ||
|
||
output_img = utils.read_transparent_png("static/pics/%s.png"%celebname) | ||
outimg = utils.build_montages([crop, output_img], (180,180),(2,1)) | ||
fname = "static/gen/"+datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S') + ".jpg" | ||
cv2.imwrite(fname, outimg[0]) | ||
|
||
return render_template("result.html", name=celebname, imgpath = fname) | ||
|
||
@app.route('/<path:path>') | ||
def send_file(path): | ||
return send_from_directory('.', path) | ||
|
||
|
||
if __name__ == "__main__": | ||
if faceDB.load_database(): | ||
faceDB.reindex() | ||
print("fifa db initialized") | ||
app.run(host= '0.0.0.0', port=5101) | ||
else: | ||
print("Failed to load and init fifa database; is fifadb.db and fifadb.index files present?\ | ||
Use save_to_facedb.py to generate them") |
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,68 @@ | ||
""" | ||
* Author: Azmath Moosa | ||
* Created: 30th July 2018 | ||
* Description: Python wrapper class for dsFace. | ||
Please launch and run the dsFace separately | ||
* Dependencies: python requests, opencv-python, numpy | ||
""" | ||
|
||
import time | ||
import json | ||
import cv2 | ||
import requests | ||
import numpy as np | ||
|
||
face_api = "http://127.0.0.1:5000/inferImage?returnFaceId=true&detector=mmod&returnFaceLandmarks=true" | ||
|
||
|
||
|
||
class DSFaceHandler(): | ||
|
||
def __init__(self, api="http://127.0.0.1:5000/inferImage", face_rec=False, detector="mmod", face_lmk=False): | ||
print("Please make sure dsFace is running in the background (./dsFace )") | ||
self.face_api = api | ||
self.detector = detector | ||
self.faceRecognition = face_rec | ||
self.faceLmk = face_lmk | ||
|
||
|
||
def endpt(self): | ||
detector = "detector="+self.detector+"&" | ||
faceRecognition = "returnFaceId=true&" if self.faceRecognition else "" | ||
faceLmk = "returnFaceLandmarks=true" if self.faceLmk else "" | ||
|
||
return self.face_api + "?" + detector + faceRecognition + faceLmk | ||
|
||
|
||
def get_embedding(self, img): | ||
img = cv2.resize(img,(250,250)) | ||
|
||
r, imgbuf = cv2.imencode(".bmp", img) | ||
image = {'pic':bytearray(imgbuf)} | ||
|
||
r = requests.post(self.endpt(), files=image) | ||
try: | ||
result = r.json() | ||
except Exception as e: | ||
print(e, r.text) | ||
|
||
if len(result) > 1: | ||
faces = result[:-1] | ||
diag = result[-1]['diagnostics'] | ||
|
||
for face in faces: | ||
rect, embedding = [face[i] for i in ['faceRectangle','faceEmbeddings']] | ||
x,y,w,h, confidence = [rect[i] for i in ['left', 'top', 'width', 'height', 'confidence']] | ||
|
||
if confidence < 0.5: | ||
continue | ||
|
||
crop = img[y:y+h, x:x+w] | ||
|
||
return np.asarray(embedding), crop | ||
else: | ||
return np.zeros([128]), img | ||
|
||
|
Binary file not shown.
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,60 @@ | ||
""" | ||
* Author: Azmath Moosa | ||
* Created: 30th July 2018 | ||
* Description: Python wrapper class for facedb. | ||
Please launch and run the facedb app separately | ||
* Dependencies: python requests, json | ||
""" | ||
import json | ||
import requests | ||
|
||
class FaceDBHandler(): | ||
|
||
def __init__(self, api="http://localhost:5100/", filename="facedb"): | ||
print("Please make sure facedb is running in the background( ./facedb --serve )") | ||
self.facedb_api = api | ||
self.db_path = filename | ||
|
||
def save_embedding(self, vec, meta): | ||
endpt = self.facedb_api + "save_embedding" | ||
body = {"embedding": vec, "meta":meta} | ||
res = requests.post(endpt,json=body).json() | ||
return res | ||
|
||
def reindex(self): | ||
endpt = self.facedb_api + "reindex" | ||
res = requests.post(endpt, json={}).json() | ||
return res["message"] == "ok" | ||
|
||
def dump_database(self): | ||
endpt = self.facedb_api + "dump_database" | ||
res = requests.post(endpt, json={"filename":self.db_path}).json() | ||
return res | ||
|
||
def load_database(self): | ||
endpt = self.facedb_api + "load_database" | ||
res = requests.post(endpt, json={"filename":self.db_path}).json() | ||
return res["message"] == "ok" | ||
|
||
def clear_database(self): | ||
endpt = self.facedb_api + "clear_database" | ||
res = requests.post(endpt, json={}).json() | ||
return res | ||
|
||
def print_database(self): | ||
endpt = self.facedb_api + "print_database" | ||
res = requests.post(endpt, json={}).json() | ||
return res | ||
|
||
def search_database(self, vec): | ||
endpt = self.facedb_api + "search_embedding" | ||
res = requests.post(endpt, json={"embedding": vec.tolist()}) | ||
try: | ||
print(res.text) | ||
return res.json() | ||
except: | ||
return {"index":0, "distance":0.0} | ||
|
||
|
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,51 @@ | ||
""" | ||
* Author: Azmath Moosa | ||
* Created: 31 July 2018 | ||
* Description: Loads FIFA player embeddings and names from fifadb.json and loads them into faceDB. It then | ||
dumps database so that it can be used later. | ||
* Dependencies: Deepsight Face, FaceDB, python packages:- requests, argparse, opencv-python | ||
""" | ||
|
||
import json | ||
import time | ||
import requests | ||
|
||
facedb_api = "http://localhost:5100/" | ||
json_file = "fifadb.json" | ||
facedb_dump_filename = "fifadb" | ||
|
||
def load_db(): | ||
data = json.loads(open(json_file).read()) | ||
names = [] | ||
embeddings = [] | ||
for d in data: | ||
names.append(d['name']) | ||
embeddings.append(d['embedding']) | ||
|
||
return names, embeddings | ||
|
||
def save_embeddings(names, embeddings): | ||
endpt = facedb_api + "save_embedding" | ||
t = time.time() | ||
count = 0 | ||
|
||
body = {"embeddings": embeddings, "metas": names} | ||
res = requests.post(endpt,json=body) | ||
count = res.json() | ||
|
||
print(count, "items added in ", time.time() - t, "seconds") | ||
|
||
def dump_database(): | ||
endpt = facedb_api + "dump_database" | ||
t = time.time() | ||
res = requests.post(endpt, json={'filename':facedb_dump_filename}) | ||
print("dump_database", res.json()," ", time.time() - t, "seconds") | ||
|
||
if __name__ == "__main__": | ||
names, embeddings = load_db() | ||
print("items read " , len(names), len(embeddings)) | ||
save_embeddings(names[:50], embeddings[:50]) #limited by 50 in free version of faceDB | ||
dump_database() | ||
print("done") |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.