From 8fa3ef84272cd48d42d09b7d2d796c62e8ac559c Mon Sep 17 00:00:00 2001 From: Askill Date: Sat, 9 May 2020 18:44:38 +0200 Subject: [PATCH] cnn works --- application/endpoints.py | 6 ++---- application/face_rec.py | 26 +++++++++++++------------- dlibinstallation.txt | 1 + run.py | 3 ++- 4 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 dlibinstallation.txt diff --git a/application/endpoints.py b/application/endpoints.py index e68b382..a379859 100644 --- a/application/endpoints.py +++ b/application/endpoints.py @@ -153,12 +153,10 @@ class Camera(Resource): try: if type == "stream": return flask.Response(self.gen(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame') - if type == "processed": + elif type == "processed": return flask.Response(self.genProcessed(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame') - elif type == "still": - lastImage1 = base64.b64decode(lastImage) - return flask.Response(lastImage1, mimetype='image/png') + return flask.Response(base64.b64decode(lastImage), mimetype='image/png') return flask.make_response(flask.jsonify({'error': "No idea how you got here"}), 404) except Exception as e: diff --git a/application/face_rec.py b/application/face_rec.py index 0c17e7a..eeff553 100644 --- a/application/face_rec.py +++ b/application/face_rec.py @@ -10,19 +10,15 @@ from io import StringIO TOLERANCE = 0.6 FRAME_THICKNESS = 3 FONT_THICKNESS = 2 -MODEL = "hog" # default: 'hog', other one can be 'cnn' - CUDA accelerated (if available) deep-learning pretrained model +MODEL = "cnn" # default: 'hog', other one can be 'cnn' - CUDA accelerated (if available) deep-learning pretrained model -def readb64(base64_string): - sbuf = StringIO() - sbuf.write(base64.b64decode(base64_string)) - pimg = Image.open(sbuf) - return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR) -print('Loading known faces...') known_faces = [] known_names = [] def initFaceRec(): + dlib.DLIB_USE_CUDA=True + print('Loading known faces...', dlib.DLIB_USE_CUDA) session = Session() # We oranize known faces as subfolders of KNOWN_FACES_DIR # Each subfolder's name becomes our label (name) @@ -38,7 +34,7 @@ def initFaceRec(): # Append encodings and name known_faces.append(encoding) known_names.append(name) - + print('DONE Loading known faces...') session.close() def identifyFace(image): @@ -58,8 +54,10 @@ def identifyFace(image): return res def identifyFaceVideo(url): + video = cv2.VideoCapture(url) image = video.read()[1] + image = cv2.resize(image,None,fx=0.5,fy=0.5) ret, image = cv2.imencode(".png", image) nparr = np.fromstring(image, np.uint8) @@ -71,16 +69,19 @@ def identifyFaceVideo(url): face_locations = {} #face locations to be drawn for face_encoding, face_location in zip(encodings, locations): - + face_locations.update(compareFace(face_encoding, face_location)) + session = Session() for k, v in face_locations.items(): # Paint frame cv2.rectangle(image, v[0], v[1], [255, 0, 0], FRAME_THICKNESS) # Wite a name - cv2.putText(image, k, v[0], cv2.FONT_HERSHEY_SIMPLEX, 1.5, [255, 0, 255], FONT_THICKNESS) + name = " ".join(session.query(Person.fname, Person.lname).filter(Person.person_id == int(k)).first()) + cv2.putText(image, name, v[0], cv2.FONT_HERSHEY_SIMPLEX, 1.5, [255, 0, 255], FONT_THICKNESS) # Show image + session.close() image = cv2.imencode(".jpg", image)[1] return image @@ -90,10 +91,9 @@ def compareFace(face_encoding, face_location): face_locations = {} match = None if True in results: # If at least one is true, get a name of first of found labels - match = "name" - print(f' - {match} from {results}') + match = known_names[results.index(True)] top_left = (face_location[3], face_location[0]) bottom_right = (face_location[1], face_location[2]) - face_locations[match] = (top_left, bottom_right) + face_locations[str(match)] = (top_left, bottom_right) return face_locations \ No newline at end of file diff --git a/dlibinstallation.txt b/dlibinstallation.txt new file mode 100644 index 0000000..e8aa73e --- /dev/null +++ b/dlibinstallation.txt @@ -0,0 +1 @@ +https://stackoverflow.com/a/57592670/10785079 \ No newline at end of file diff --git a/run.py b/run.py index 82a3b93..5ee6923 100644 --- a/run.py +++ b/run.py @@ -1,7 +1,8 @@ from application import app from application.face_rec import initFaceRec + initFaceRec() -app.run(host="localhost", port='5001', debug=True) +app.run(host="localhost", port='5001', debug=True, threaded=True)