FacialRecognition-Demo/application/endpoints.py

172 lines
6.4 KiB
Python
Raw Normal View History

2020-03-15 19:02:37 +00:00
from flask_restful import Resource, reqparse
import flask
import requests
import application.config as config
import json
2020-03-19 16:52:30 +00:00
import cv2
2020-03-20 20:21:31 +00:00
import base64
2020-03-15 22:15:40 +00:00
from application.db import Session, Person, Fingerprint
2020-04-27 17:44:55 +00:00
import application.face_rec as fr
2020-03-19 16:52:30 +00:00
lastImage = ""
2020-03-16 12:31:18 +00:00
2020-03-15 19:02:37 +00:00
class PersonList(Resource):
2020-03-15 23:31:07 +00:00
def post(self, id = None):
2020-03-15 19:02:37 +00:00
""" """
try:
2020-03-16 12:31:18 +00:00
jsonData = flask.request.get_json(force=True)
personJSON = jsonData["person"]
2020-03-20 20:21:31 +00:00
print(personJSON)
2020-03-16 12:31:18 +00:00
session = Session()
2020-03-20 20:21:31 +00:00
# get Fingerprints with Middleware
2020-03-16 12:31:18 +00:00
fingerprintsObj = []
2020-03-20 20:21:31 +00:00
if "fingerprints" in personJSON:
for fingerprint in personJSON["fingerprints"]:
fingerprint["fingerprint"] = fingerprint["fingerprint"].encode('utf-8')
fp = Fingerprint(**fingerprint) # ** Operator converts DICT/JSON to initializable
fingerprintsObj.append(fp)
session.add(fp)
2020-03-16 12:31:18 +00:00
personJSON["fingerprints"] = fingerprintsObj
personJSON["face"] = lastImage
2020-03-16 12:31:18 +00:00
person = Person(**personJSON)
session.add(person)
session.commit()
data = list(session.query(Person).filter_by(person_id=person.person_id))
arr = []
for x in data:
arr.append(x.serialize())
2020-04-29 20:00:17 +00:00
session.close()
2020-03-16 12:31:18 +00:00
return flask.make_response(flask.jsonify({'data': arr}), 201)
2020-03-15 19:02:37 +00:00
except Exception as e:
print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
2020-03-15 23:31:07 +00:00
def get(self, id = None):
2020-03-15 19:02:37 +00:00
""" """
2020-04-29 20:00:17 +00:00
session = Session()
2020-03-15 19:02:37 +00:00
try:
2020-03-19 16:52:30 +00:00
parser = reqparse.RequestParser()
parser.add_argument('useFace', type=bool, required=False)
args = parser.parse_args()
2020-04-29 20:00:17 +00:00
2020-03-19 16:52:30 +00:00
2020-03-19 18:11:04 +00:00
# this indicates that the captured face should be use for identification / validation
2020-03-19 16:52:30 +00:00
if "useFace" in args and args["useFace"]:
2020-04-29 20:00:17 +00:00
Camera().post()
2020-03-19 18:11:04 +00:00
if id is not None:
# validate
data = list(session.query(Person).filter_by(person_id=id))[0].serialize()
2020-04-27 17:44:55 +00:00
results = fr.identifyFace(lastImage)
data["matching_score"] = 1 - results[int(id)]
2020-03-19 18:11:04 +00:00
# return identified person object + matching score
return flask.make_response(flask.jsonify({'data': data}), 200)
else:
# replace by Biometric function
# identify
# return identified person object + matching score
2020-04-27 17:44:55 +00:00
results = fr.identifyFace(lastImage)
2020-04-15 14:35:51 +00:00
data = []
for x in list(session.query(Person).all()):
ser = x.serialize()
2020-04-27 17:44:55 +00:00
ser["matching_score"] = 1 - results[x.person_id]
2020-04-15 14:35:51 +00:00
data.append(ser)
2020-03-19 18:11:04 +00:00
return flask.make_response(flask.jsonify({'data': data}), 200)
2020-03-16 12:31:18 +00:00
if id is None:
data = list(session.query(Person).all())
else:
data = list(session.query(Person).filter_by(person_id=id))
2020-03-15 19:02:37 +00:00
arr = []
for x in data:
2020-03-15 22:15:40 +00:00
arr.append(x.serialize())
2020-04-29 20:00:17 +00:00
session.close()
2020-03-15 19:02:37 +00:00
return flask.make_response(flask.jsonify({'data': arr}), 200)
except Exception as e:
2020-04-29 20:00:17 +00:00
session.close()
2020-03-15 19:02:37 +00:00
print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
2020-03-15 23:31:07 +00:00
def put(self, id = None):
2020-03-15 19:02:37 +00:00
""" """
try:
data = ""
return flask.make_response(flask.jsonify({'data': data}), 200)
except Exception as e:
print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
2020-03-15 23:31:07 +00:00
def delete(self, id = None):
2020-03-15 19:02:37 +00:00
""" """
try:
2020-03-16 12:50:13 +00:00
if id is None:
return flask.make_response(flask.jsonify({'error': "No ID given"}), 404)
session = Session()
data = session.query(Person).filter_by(person_id=id).delete()
session.commit()
2020-04-29 20:00:17 +00:00
session.close()
2020-03-15 19:02:37 +00:00
return flask.make_response(flask.jsonify({'data': data}), 204)
2020-03-16 12:50:13 +00:00
2020-03-15 19:02:37 +00:00
except Exception as e:
print("error: -", e)
2020-03-16 12:50:13 +00:00
return flask.make_response(flask.jsonify({'error': str(e)}), 404)
2020-03-15 19:02:37 +00:00
2020-03-19 16:52:30 +00:00
class Camera(Resource):
2020-04-29 20:00:17 +00:00
# provides the function used for the live streams
2020-03-19 16:52:30 +00:00
class VideoCamera(object):
"""Video stream object"""
url = "http://192.168.178.56:8080/video"
2020-03-19 16:52:30 +00:00
def __init__(self):
self.video = cv2.VideoCapture(self.url)
def __del__(self):
self.video.release()
def get_frame(self, ending):
success, image = self.video.read()
ret, jpeg = cv2.imencode(ending, image)
2020-03-20 20:21:31 +00:00
return jpeg
2020-03-19 16:52:30 +00:00
def gen(self, camera):
"""Video streaming generator function."""
while True:
2020-03-20 20:21:31 +00:00
frame = camera.get_frame('.jpg').tobytes()
2020-03-19 16:52:30 +00:00
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
2020-05-07 17:43:16 +00:00
def genProcessed(self, url=None):
"""Video streaming generator function."""
url = "http://192.168.178.56:8080/video"
while True:
frame = fr.identifyFaceVideo(url).tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
2020-03-19 16:52:30 +00:00
def get(self, type = "stream"):
global lastImage
try:
if type == "stream":
return flask.Response(self.gen(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
2020-05-09 16:44:38 +00:00
elif type == "processed":
2020-05-07 17:43:16 +00:00
return flask.Response(self.genProcessed(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
2020-03-19 16:52:30 +00:00
elif type == "still":
2020-05-09 16:44:38 +00:00
return flask.Response(base64.b64decode(lastImage), mimetype='image/png')
2020-03-19 16:52:30 +00:00
return flask.make_response(flask.jsonify({'error': "No idea how you got here"}), 404)
except Exception as e:
print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 404)
def post(self):
global lastImage
try:
lastImage = base64.b64encode(self.VideoCamera().get_frame('.png'))
2020-03-19 16:52:30 +00:00
except Exception as e:
print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 404)