Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ Create the database by issuing the following in the data folder `sqlite3 images.
Download the [AT&T face database](http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html) and extract it to `data/images` before the server is started. This is needed to build the initial prediction model.

cd data
wget http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.tar.Z
curl -o att_faces.tar.Z http://www.cl.cam.ac.uk/research/dtg/attarchive/pub/data/att_faces.tar.Z
tar zxvf att_faces.tar.Z
mv att_faces images
mv orl_faces images

Copy `haarcascade_frontalface_alt.xml` from `<path to opencv source>/data/haarcascades/` to the data folder.

Run with `python server.py` and browse to http://localhost:8888 when the model has been trained.


## notice
```
pip install opencv-contrib-python
```
to make sure the `face` in cv2
42 changes: 19 additions & 23 deletions opencv.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ def crop_faces(img, faces):
def load_images(path):
images, labels = [], []
c = 0
print "test " + path
print("test " + path)
for dirname, dirnames, filenames in os.walk(path):
print "test"
print("test")
for subdirname in dirnames:
subjectPath = os.path.join(dirname, subdirname)
for filename in os.listdir(subjectPath):
try:
img = cv2.imread(os.path.join(subjectPath, filename), cv2.IMREAD_GRAYSCALE)
images.append(np.asarray(img, dtype=np.uint8))
labels.append(c)
except IOError, (errno, strerror):
print "IOError({0}): {1}".format(errno, strerror)
except:
print "Unexpected error:" , sys.exc_info()[0]
except IOError as e:
print("IOError({0}): {1}".format(e))
except Exception as e:
print("Unexpected error:" , sys.exc_info()[0])
raise
c += 1
return images, labels
Expand All @@ -61,32 +61,33 @@ def load_images_to_db(path):
for dirname, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
label = Label.get_or_create(name=subdirname)
label.save()
label, created = Label.get_or_create(name=subdirname)
if created is True:
label.save(force_insert=True)
for filename in os.listdir(subject_path):
path = os.path.abspath(os.path.join(subject_path, filename))
logging.info('saving path %s' % path)
image = Image.get_or_create(path=path, label=label)
image.save()
image, created = Image.get_or_create(path=path, label=label)
if created is True:
image.save(force_insert=True)

def load_images_from_db():
images, labels = [],[]
for label in Label.select():
for image in label.image_set:
try:
cv_image = cv2.imread(image.path, cv2.IMREAD_GRAYSCALE)
cv_image = cv2.resize(cv_image, (100,100))
cv_image = cv2.resize(cv_image, (100, 100))
images.append(np.asarray(cv_image, dtype=np.uint8))
labels.append(label.id)
except IOError, (errno, strerror):
print "IOError({0}): {1}".format(errno, strerror)
except IOError as e:
print("IOError({0}): {1}".format(e))
return images, np.asarray(labels)

def train():
images, labels = load_images_from_db()
model = cv2.createFisherFaceRecognizer()
#model = cv2.createEigenFaceRecognizer()
model.train(images,labels)
model = cv2.face.FisherFaceRecognizer_create()
model.train(images, labels)
model.save(MODEL_FILE)

def predict(cv_image):
Expand All @@ -95,9 +96,7 @@ def predict(cv_image):
if len(faces) > 0:
cropped = to_grayscale(crop_faces(cv_image, faces))
resized = cv2.resize(cropped, (100,100))

model = cv2.createFisherFaceRecognizer()
#model = cv2.createEigenFaceRecognizer()
model = cv2.face.FisherFaceRecognizer_create()
model.load(MODEL_FILE)
prediction = model.predict(resized)
result = {
Expand Down Expand Up @@ -160,8 +159,5 @@ def persist(self, cv_image):

if __name__ == "__main__":
load_images_to_db("data/images")
#train()

print 'done'
#predict()
#train()
print('done')
14 changes: 8 additions & 6 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import uuid
from PIL import Image
import time
import StringIO
from io import StringIO
import uuid
import numpy
import json
from tornado.options import define, options
from tornado.options import options
import opencv

define("port", default=8888, help="run on the given poort", type=int)
options.define("port", default=8888, help="run on the given poort", type=int)

class Application(tornado.web.Application):
def __init__(self):
handlers = [
#(r"/", MainHandler),
#(r"/facedetector", FaceDetectHandler),
(r"/main", MainHandler),
(r"/facedetector", FaceDetectHandler),
(r"/", SetupHarvestHandler),
(r"/harvesting", HarvestHandler),
(r"/predict", PredictHandler),
Expand Down Expand Up @@ -114,8 +114,10 @@ def main():
opencv.train()
logging.info("Model trained")
app = Application()
options.parse_command_line()
print(' -- app is listen on: %s' % options.port)
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
main()