diff --git a/application/endpoints.py b/application/endpoints.py index 324f23e..2b442ba 100644 --- a/application/endpoints.py +++ b/application/endpoints.py @@ -4,6 +4,7 @@ import requests import application.config as config import json import cv2 +import base64 from application.db import Session, Person, Fingerprint lastImage = "" @@ -14,17 +15,20 @@ class PersonList(Resource): try: jsonData = flask.request.get_json(force=True) personJSON = jsonData["person"] - + print(personJSON) session = Session() + + # get Fingerprints with Middleware fingerprintsObj = [] - 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) + 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) personJSON["fingerprints"] = fingerprintsObj - personJSON["face"] = personJSON["face"].encode('utf-8') + personJSON["face"] = ("data:image/png;base64,"+str(lastImage)).encode('utf-8') person = Person(**personJSON) session.add(person) session.commit() @@ -120,13 +124,13 @@ class Camera(Resource): def get_frame(self, ending): success, image = self.video.read() ret, jpeg = cv2.imencode(ending, image) - return jpeg.tobytes() + return jpeg def gen(self, camera): """Video streaming generator function.""" while True: - frame = camera.get_frame('.jpg') + frame = camera.get_frame('.jpg').tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') @@ -137,7 +141,8 @@ class Camera(Resource): return flask.Response(self.gen(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame') elif type == "still": - return flask.Response(lastImage, mimetype='image/jpeg') + lastImage1 = base64.b64decode(lastImage.encode()) + return flask.Response(lastImage1, mimetype='image/png') return flask.make_response(flask.jsonify({'error': "No idea how you got here"}), 404) except Exception as e: @@ -147,7 +152,7 @@ class Camera(Resource): def post(self): global lastImage try: - lastImage = self.VideoCamera().get_frame('.png') + lastImage = base64.b64encode(self.VideoCamera().get_frame('.png')).decode() except Exception as e: print("error: -", e) return flask.make_response(flask.jsonify({'error': str(e)}), 404) \ No newline at end of file diff --git a/application/static/coms.js b/application/static/coms.js index 71e1d85..85c6b9f 100644 --- a/application/static/coms.js +++ b/application/static/coms.js @@ -1,4 +1,4 @@ -function getJSON(url, callback) { +function getJSON(url, callback, fallback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'json'; @@ -7,14 +7,13 @@ function getJSON(url, callback) { if (status < 400) { callback(null, xhr.response); } else { - console.log("failed getting"); - console.log(status); + fallback(); } }; xhr.send(); }; -function putJSON(url, data, callback) { +function putJSON(url, data, callback, fallback) { var xhr = new XMLHttpRequest(); xhr.open('PUT', url, true); xhr.responseType = 'json'; @@ -24,14 +23,13 @@ function putJSON(url, data, callback) { if (status < 400) { callback(null, xhr.response); } else { - console.log("failed posting"); - console.log(status); + fallback(); } }; xhr.send(data); }; -function postJSON(url, data, callback) { +function postJSON(url, data, callback, fallback) { var xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.responseType = 'json'; @@ -41,8 +39,7 @@ function postJSON(url, data, callback) { if (status < 400) { callback(null, xhr.response); } else { - console.log("failed posting"); - console.log(status); + fallback(); } }; xhr.send(data); diff --git a/application/static/main.css b/application/static/main.css index ff65f29..da21677 100644 --- a/application/static/main.css +++ b/application/static/main.css @@ -27,7 +27,7 @@ top: 10rem; left: -50%; display: block; - + padding: 0; } .listIMG{ @@ -46,7 +46,7 @@ padding:0; } .card{ - margin-top:2rem; + margin-top:1rem; } .card-text{ padding: 0; @@ -60,25 +60,27 @@ float:right; } .middle{ - margin: 0rem 2rem 0 2rem; + padding: 2rem ; + margin: 0 !important; height: 100%; - width: 40%; + width: 45%; display: block; } #image-left{ width:100%; - padding: 1rem; + } #image-right{ width:100%; - padding: 1rem; + } .heroInfo{ - padding: 1rem; + } .middle-controls{ - margin-right: 1rem; + margin-left: 1rem; + margin-top: 1rem; } \ No newline at end of file diff --git a/application/static/main.js b/application/static/main.js index 9bbc4a7..57c847f 100644 --- a/application/static/main.js +++ b/application/static/main.js @@ -5,6 +5,15 @@ var ml = document.getElementById('middle-left'); var mr = document.getElementById('middle-right'); personData = {} +/** + * Retrieves input data from a form and returns it as a JSON object. + * @param {HTMLFormControlsCollection} elements the form elements + * @return {Object} form data as an object literal + */ +const formToJSON = elements => [].reduce.call(elements, (data, element) => { + data[element.name] = element.value; + return data; + }, {}); function focusNav(id) { focusedID = id; @@ -51,7 +60,8 @@ function snapShot(){ postJSON(rootKontext + "/api/v1/camera/", {}, function (error, data) { document.getElementById('image-left').src = rootKontext + "/api/v1/camera/still"; - } + }, + null ); } @@ -70,12 +80,32 @@ function renderPersonRight(data){ mr.innerHTML = string; } +function enrole(){ + data = {} + data["fname"] = document.getElementById("personform")["fname"].value + data["lname"] = document.getElementById("personform")["lname"].value + data["gender"] = document.getElementById("personform")["gender"].value + data["yob"] = document.getElementById("personform")["yob"].value + data = {"person": data} + console.log(data) + postJSON(rootKontext + "/api/v1/person/", JSON.stringify(data), + function(){ + location.reload() + }, + null + ) +} + function identify(){ snapShot() getJSON(rootKontext + "/api/v1/person/?useFace=True", function (error, data) { data = data["data"] renderPersonRight(data) + $("#middle-right").removeClass("border-danger").addClass("boarder-success") + }, + function(){ + $("#middle-right").removeClass("border-success").addClass("border-danger") } ); } @@ -86,6 +116,11 @@ function validate(){ function (error, data) { data = data["data"] renderPersonRight(data) + $("#middle-right").removeClass("border-danger").addClass("border-success") + }, + function(){ + mr.innerHTML="

Please select a person
from the list, which you want to use for validation

" + $("#middle-right").removeClass("border-success").addClass("border-danger") } ); } @@ -110,7 +145,8 @@ function loadData() { personData = data loadPersonList(data) renderIdentify() - } + }, + null ); } diff --git a/application/static/render.js b/application/static/render.js index 6f346fc..2f27123 100644 --- a/application/static/render.js +++ b/application/static/render.js @@ -12,6 +12,7 @@ function renderValidate(){ ` ml.innerHTML = string; + } function renderChange(){ @@ -20,7 +21,29 @@ function renderChange(){ } function renderEnrole(){ clearMiddle() - console.log("enrole") + string = ` + + + + + ` + ml.innerHTML = string; + + string2 = ` +
+
+
+
+
+ +
+ + ` + mr.innerHTML = string2; } function renderIdentify(){ clearMiddle() diff --git a/application/templates/index.html b/application/templates/index.html index f447829..211dcd7 100644 --- a/application/templates/index.html +++ b/application/templates/index.html @@ -40,11 +40,11 @@
-
+
-
+
diff --git a/test.sqlite b/test.sqlite index 35f6d66..17e2ceb 100644 Binary files a/test.sqlite and b/test.sqlite differ