cnn works
This commit is contained in:
parent
58fa46a92d
commit
8fa3ef8427
|
|
@ -153,12 +153,10 @@ class Camera(Resource):
|
||||||
try:
|
try:
|
||||||
if type == "stream":
|
if type == "stream":
|
||||||
return flask.Response(self.gen(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
|
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')
|
return flask.Response(self.genProcessed(self.VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||||
|
|
||||||
elif type == "still":
|
elif type == "still":
|
||||||
lastImage1 = base64.b64decode(lastImage)
|
return flask.Response(base64.b64decode(lastImage), mimetype='image/png')
|
||||||
return flask.Response(lastImage1, mimetype='image/png')
|
|
||||||
|
|
||||||
return flask.make_response(flask.jsonify({'error': "No idea how you got here"}), 404)
|
return flask.make_response(flask.jsonify({'error': "No idea how you got here"}), 404)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
|
|
@ -10,19 +10,15 @@ from io import StringIO
|
||||||
TOLERANCE = 0.6
|
TOLERANCE = 0.6
|
||||||
FRAME_THICKNESS = 3
|
FRAME_THICKNESS = 3
|
||||||
FONT_THICKNESS = 2
|
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_faces = []
|
||||||
known_names = []
|
known_names = []
|
||||||
|
|
||||||
def initFaceRec():
|
def initFaceRec():
|
||||||
|
dlib.DLIB_USE_CUDA=True
|
||||||
|
print('Loading known faces...', dlib.DLIB_USE_CUDA)
|
||||||
session = Session()
|
session = Session()
|
||||||
# We oranize known faces as subfolders of KNOWN_FACES_DIR
|
# We oranize known faces as subfolders of KNOWN_FACES_DIR
|
||||||
# Each subfolder's name becomes our label (name)
|
# Each subfolder's name becomes our label (name)
|
||||||
|
|
@ -38,7 +34,7 @@ def initFaceRec():
|
||||||
# Append encodings and name
|
# Append encodings and name
|
||||||
known_faces.append(encoding)
|
known_faces.append(encoding)
|
||||||
known_names.append(name)
|
known_names.append(name)
|
||||||
|
print('DONE Loading known faces...')
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
def identifyFace(image):
|
def identifyFace(image):
|
||||||
|
|
@ -58,8 +54,10 @@ def identifyFace(image):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def identifyFaceVideo(url):
|
def identifyFaceVideo(url):
|
||||||
|
|
||||||
video = cv2.VideoCapture(url)
|
video = cv2.VideoCapture(url)
|
||||||
image = video.read()[1]
|
image = video.read()[1]
|
||||||
|
image = cv2.resize(image,None,fx=0.5,fy=0.5)
|
||||||
ret, image = cv2.imencode(".png", image)
|
ret, image = cv2.imencode(".png", image)
|
||||||
|
|
||||||
nparr = np.fromstring(image, np.uint8)
|
nparr = np.fromstring(image, np.uint8)
|
||||||
|
|
@ -74,13 +72,16 @@ def identifyFaceVideo(url):
|
||||||
|
|
||||||
face_locations.update(compareFace(face_encoding, face_location))
|
face_locations.update(compareFace(face_encoding, face_location))
|
||||||
|
|
||||||
|
session = Session()
|
||||||
for k, v in face_locations.items():
|
for k, v in face_locations.items():
|
||||||
# Paint frame
|
# Paint frame
|
||||||
cv2.rectangle(image, v[0], v[1], [255, 0, 0], FRAME_THICKNESS)
|
cv2.rectangle(image, v[0], v[1], [255, 0, 0], FRAME_THICKNESS)
|
||||||
# Wite a name
|
# 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
|
# Show image
|
||||||
|
session.close()
|
||||||
image = cv2.imencode(".jpg", image)[1]
|
image = cv2.imencode(".jpg", image)[1]
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
@ -90,10 +91,9 @@ def compareFace(face_encoding, face_location):
|
||||||
face_locations = {}
|
face_locations = {}
|
||||||
match = None
|
match = None
|
||||||
if True in results: # If at least one is true, get a name of first of found labels
|
if True in results: # If at least one is true, get a name of first of found labels
|
||||||
match = "name"
|
match = known_names[results.index(True)]
|
||||||
print(f' - {match} from {results}')
|
|
||||||
top_left = (face_location[3], face_location[0])
|
top_left = (face_location[3], face_location[0])
|
||||||
bottom_right = (face_location[1], face_location[2])
|
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
|
return face_locations
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
https://stackoverflow.com/a/57592670/10785079
|
||||||
3
run.py
3
run.py
|
|
@ -1,7 +1,8 @@
|
||||||
from application import app
|
from application import app
|
||||||
from application.face_rec import initFaceRec
|
from application.face_rec import initFaceRec
|
||||||
|
|
||||||
initFaceRec()
|
initFaceRec()
|
||||||
app.run(host="localhost", port='5001', debug=True)
|
app.run(host="localhost", port='5001', debug=True, threaded=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue