Skip to content

Commit 49b714d

Browse files
committed
cam recognition optimized
1 parent e97cde2 commit 49b714d

20 files changed

+213
-0
lines changed

requirements.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cachetools==2.0.1
2+
certifi==2018.1.18
3+
chardet==3.0.4
4+
click==6.7
5+
coverage==4.4.2
6+
dlib==19.7.0
7+
face-recognition==1.0.0
8+
face-recognition-models==0.3.0
9+
Flask==0.12.2
10+
Flask-Cors==3.0.3
11+
google-api-core==0.1.4
12+
google-auth==1.3.0
13+
google-cloud-vision==0.30.0
14+
googleapis-common-protos==1.5.3
15+
grpcio==1.9.1
16+
idna==2.6
17+
itsdangerous==0.24
18+
Jinja2==2.10
19+
MarkupSafe==1.0
20+
nose==1.3.7
21+
numpy==1.13.3
22+
olefile==0.44
23+
Pillow==4.3.0
24+
protobuf==3.5.1
25+
pyasn1==0.4.2
26+
pyasn1-modules==0.2.1
27+
pytesseract==0.2.0
28+
pytz==2017.3
29+
requests==2.18.4
30+
rsa==3.4.2
31+
scipy==1.0.0
32+
six==1.11.0
33+
urllib3==1.22
34+
Werkzeug==0.12.2

webcam/Encodings.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import face_recognition
3+
from flask import Flask, request, redirect
4+
from werkzeug.utils import secure_filename
5+
import PIL
6+
from PIL import Image
7+
import numpy as np
8+
9+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
10+
11+
app = Flask(__name__)
12+
app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024
13+
14+
15+
def allowed_file(filename):
16+
return '.' in filename and \
17+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
18+
19+
20+
@app.route('/', methods=['GET', 'POST'])
21+
def upload_file():
22+
if request.method == 'POST':
23+
# check if the post request has the file part
24+
if 'file' not in request.files:
25+
# flash('No file part')
26+
return redirect(request.url)
27+
file = request.files['file']
28+
# if user does not select file, browser also
29+
# submit a empty part without filename
30+
if file.filename == '':
31+
# flash('No selected file')
32+
return redirect(request.url)
33+
34+
if file and allowed_file(file.filename):
35+
filename = secure_filename(file.filename)
36+
37+
_image = face_recognition.load_image_file(file)
38+
face_locations = face_recognition.face_locations(_image)
39+
if len(face_locations) > 0:
40+
_face_encoding = face_recognition.face_encodings(_image)[0]
41+
dir = os.path.abspath("Encodings")
42+
np.save(dir+os.path.abspath(os.sep)+filename[:-4], _face_encoding)
43+
# file = open(dir+os.path.abspath(os.sep)+filename[:-4]+".txt", "w")
44+
# file.write(_face_encoding)
45+
# file.close()
46+
return '''
47+
<!doctype html>
48+
<title>Success Page</title>
49+
<h1>File Successfully Uploaded</h1>
50+
'''
51+
else:
52+
return '''
53+
<!doctype html>
54+
<title>Error Page</title>
55+
<h1>No face Detected</h1>
56+
'''
57+
58+
return '''
59+
<!doctype html>
60+
<title>Upload new File</title>
61+
<h1>Upload new File</h1>
62+
<form method=post enctype=multipart/form-data>
63+
<p><input type=file name=file>
64+
<input type=submit value=Upload>
65+
</form>
66+
'''
67+
68+
69+
def resize_image(img):
70+
base = 800
71+
new_image = Image.open(img)
72+
w_percent = (base / float(new_image.size[0]))
73+
h_size = int((float(new_image.size[1]) * float(w_percent)))
74+
new_image = new_image.resize((base, h_size), PIL.Image.ANTIALIAS)
75+
return new_image
76+
77+
if __name__ == "__main__":
78+
app.run(host='0.0.0.0', port=5001, debug=True)

webcam/Encodings/Baby_Shantel.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Chioma_ECE.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Diribe.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Doris_Alumona.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Gibbs.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Jon_snow.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Kingsley.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Nenye.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Njay.npy

1.08 KB
Binary file not shown.

webcam/Encodings/Santos.npy

1.08 KB
Binary file not shown.

webcam/Encodings/TOC_.npy

1.08 KB
Binary file not shown.

webcam/Encodings/WENDY.npy

1.08 KB
Binary file not shown.

webcam/Encodings/daenerys.npy

1.08 KB
Binary file not shown.

webcam/Encodings/greg.npy

1.08 KB
Binary file not shown.

webcam/Encodings/lizzy_nysc.npy

1.08 KB
Binary file not shown.

webcam/Encodings/natalie_dormer.npy

1.08 KB
Binary file not shown.

webcam/Encodings/obinna.npy

1.08 KB
Binary file not shown.
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import face_recognition
2+
import cv2
3+
import os
4+
import numpy as np
5+
6+
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
7+
# other example, but it includes some basic performance tweaks to make things run a lot faster:
8+
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
9+
# 2. Only detect faces in every other frame of video.
10+
11+
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
12+
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
13+
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
14+
15+
# Get a reference to webcam #0 (the default one)
16+
video_capture = cv2.VideoCapture(0)
17+
18+
# Load a sample picture and learn how to recognize it.
19+
encodings = []
20+
images = []
21+
names = []
22+
23+
directory = os.fsencode("Encodings/")
24+
_dir = os.path.abspath("Encodings")
25+
26+
27+
def process_encodings():
28+
list_dir = os.listdir(directory)
29+
for file in list_dir:
30+
filename = os.fsdecode(file)
31+
a = _dir + os.path.abspath(os.sep) + filename
32+
encoding = np.load(a)
33+
encodings.append(encoding)
34+
names.append(filename[:-4])
35+
36+
# Initialize some variables
37+
process_encodings()
38+
encoding_size = len(encodings)
39+
face_locations = []
40+
face_encodings = []
41+
face_names = []
42+
process_this_frame = True
43+
44+
while True:
45+
# Grab a single frame of video
46+
ret, frame = video_capture.read()
47+
48+
# Resize frame of video to 1/4 size for faster face recognition processing
49+
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
50+
51+
# Only process every other frame of video to save time
52+
if process_this_frame:
53+
# Find all the faces and face encodings in the current frame of video
54+
face_locations = face_recognition.face_locations(small_frame)
55+
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
56+
57+
face_names = []
58+
for face_encoding in face_encodings:
59+
# See if the face is a match for the known face(s)
60+
match = face_recognition.compare_faces(encodings, face_encoding, 0.45)
61+
name = "Unknown"
62+
for i in range(len(match)):
63+
if match[i]:
64+
name = names[i]
65+
face_names.append(name)
66+
67+
process_this_frame = not process_this_frame
68+
69+
70+
# Display the results
71+
for (top, right, bottom, left), name in zip(face_locations, face_names):
72+
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
73+
top *= 4
74+
right *= 4
75+
bottom *= 4
76+
left *= 4
77+
78+
# Draw a box around the face
79+
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
80+
81+
# Draw a label with a name below the face
82+
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
83+
font = cv2.FONT_HERSHEY_DUPLEX
84+
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
85+
86+
# Display the resulting image
87+
cv2.imshow('Video', frame)
88+
89+
# Hit 'q' on the keyboard to quit!
90+
if cv2.waitKey(1) & 0xFF == ord('q'):
91+
break
92+
93+
# Refresh Encodings
94+
current_encoding_size = len(os.listdir(directory))
95+
if current_encoding_size != encoding_size:
96+
encoding_size = current_encoding_size
97+
process_encodings()
98+
99+
# Release handle to the webcam
100+
video_capture.release()
101+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)