FacialRecognition-Demo/application/endpoints.py

158 lines
5.7 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-03-15 19:02:37 +00:00
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
2020-03-20 20:21:31 +00:00
personJSON["face"] = ("data:image/png;base64,"+str(lastImage)).encode('utf-8')
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())
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
""" """
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-03-15 19:02:37 +00:00
session = Session()
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-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()
data["matching_score"] = 0.95
# 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
data = list(session.query(Person).all())[1].serialize()
data["matching_score"] = 0.95
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-03-15 19:02:37 +00:00
return flask.make_response(flask.jsonify({'data': arr}), 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 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-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):
# provides th function used for the live streams
class VideoCamera(object):
"""Video stream object"""
url = "https://webcam1.lpl.org/axis-cgi/mjpg/video.cgi"
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')
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')
elif type == "still":
2020-03-20 20:21:31 +00:00
lastImage1 = base64.b64decode(lastImage.encode())
return flask.Response(lastImage1, 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:
2020-03-20 20:21:31 +00:00
lastImage = base64.b64encode(self.VideoCamera().get_frame('.png')).decode()
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)