From b0f7cd9b5d8f91bcfd73f9e2d5c86f22f358652e Mon Sep 17 00:00:00 2001 From: jack Date: Tue, 19 Dec 2017 18:45:34 +0800 Subject: [PATCH 1/2] with opencv 3.x --- README.md | 6 ++++++ opencv.py | 29 ++++++++++++----------------- server.py | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b0a5e1e..50dea77 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,9 @@ Copy `haarcascade_frontalface_alt.xml` from `/data/haarca 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 \ No newline at end of file diff --git a/opencv.py b/opencv.py index 30af104..d0bf185 100644 --- a/opencv.py +++ b/opencv.py @@ -39,9 +39,9 @@ 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): @@ -49,10 +49,10 @@ def load_images(path): 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 @@ -78,15 +78,14 @@ def load_images_from_db(): 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): @@ -95,8 +94,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.face.FisherFaceRecognizer_create() #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) @@ -160,8 +158,5 @@ def persist(self, cv_image): if __name__ == "__main__": load_images_to_db("data/images") - #train() - print 'done' - #predict() - #train() + print('done') diff --git a/server.py b/server.py index 7bffb10..89add2f 100644 --- a/server.py +++ b/server.py @@ -8,7 +8,7 @@ import uuid from PIL import Image import time -import StringIO +from io import StringIO import uuid import numpy import json From 5d1b955d5c653e3379dc7cfd2aeb851397fc84bc Mon Sep 17 00:00:00 2001 From: jack Date: Tue, 19 Dec 2017 19:19:44 +0800 Subject: [PATCH 2/2] fix something, but not loose with pythion 3.x --- README.md | 4 ++-- opencv.py | 13 +++++++------ server.py | 12 +++++++----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 50dea77..7ec726b 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,9 @@ 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 `/data/haarcascades/` to the data folder. diff --git a/opencv.py b/opencv.py index d0bf185..99534ae 100644 --- a/opencv.py +++ b/opencv.py @@ -61,13 +61,15 @@ 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 = [],[] @@ -75,7 +77,7 @@ def load_images_from_db(): 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 as e: @@ -95,7 +97,6 @@ def predict(cv_image): cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100,100)) model = cv2.face.FisherFaceRecognizer_create() - #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) result = { diff --git a/server.py b/server.py index 89add2f..cca941e 100644 --- a/server.py +++ b/server.py @@ -12,16 +12,16 @@ 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), @@ -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()