Skip to content
This repository was archived by the owner on Dec 23, 2020. It is now read-only.

Commit e10ef36

Browse files
committed
Applied python3 pull requst
source: ragulin#11
1 parent 6851945 commit e10ef36

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ tags
55
*.swn
66
*.swm
77
*.pyc
8+
9+
.DS_Store
10+
.vscode

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Download the [AT&T face database](http://www.cl.cam.ac.uk/research/dtg/attarchiv
4949
cd data
5050
wget http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.tar.Z
5151
tar zxvf att_faces.tar.Z
52-
mv att_faces images
52+
mv orl_faces images
5353

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

opencv.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ def crop_faces(img, faces):
3939
def load_images(path):
4040
images, labels = [], []
4141
c = 0
42-
print "test " + path
42+
print("test " + path)
4343
for dirname, dirnames, filenames in os.walk(path):
44-
print "test"
44+
print("test")
4545
for subdirname in dirnames:
4646
subjectPath = os.path.join(dirname, subdirname)
4747
for filename in os.listdir(subjectPath):
4848
try:
4949
img = cv2.imread(os.path.join(subjectPath, filename), cv2.IMREAD_GRAYSCALE)
5050
images.append(np.asarray(img, dtype=np.uint8))
5151
labels.append(c)
52-
except IOError, (errno, strerror):
53-
print "IOError({0}): {1}".format(errno, strerror)
52+
except IOError as e:
53+
print("IOError({0})".format(e))
5454
except:
55-
print "Unexpected error:" , sys.exc_info()[0]
55+
print("Unexpected error:" , sys.exc_info()[0])
5656
raise
5757
c += 1
5858
return images, labels
@@ -61,13 +61,15 @@ def load_images_to_db(path):
6161
for dirname, dirnames, filenames in os.walk(path):
6262
for subdirname in dirnames:
6363
subject_path = os.path.join(dirname, subdirname)
64-
label = Label.get_or_create(name=subdirname)
65-
label.save()
64+
label, created = Label.get_or_create(name=subdirname)
65+
if created is True:
66+
label.save()
6667
for filename in os.listdir(subject_path):
6768
path = os.path.abspath(os.path.join(subject_path, filename))
6869
logging.info('saving path %s' % path)
69-
image = Image.get_or_create(path=path, label=label)
70-
image.save()
70+
image, created = Image.get_or_create(path=path, label=label)
71+
if created is True:
72+
image.save()
7173

7274
def load_images_from_db():
7375
images, labels = [],[]
@@ -78,14 +80,13 @@ def load_images_from_db():
7880
cv_image = cv2.resize(cv_image, (100,100))
7981
images.append(np.asarray(cv_image, dtype=np.uint8))
8082
labels.append(label.id)
81-
except IOError, (errno, strerror):
82-
print "IOError({0}): {1}".format(errno, strerror)
83+
except IOError as e:
84+
print("IOError({0})".format(e))
8385
return images, np.asarray(labels)
8486

8587
def train():
8688
images, labels = load_images_from_db()
87-
model = cv2.createFisherFaceRecognizer()
88-
#model = cv2.createEigenFaceRecognizer()
89+
model = cv2.face.FisherFaceRecognizer_create()
8990
model.train(images,labels)
9091
model.save(MODEL_FILE)
9192

@@ -96,8 +97,7 @@ def predict(cv_image):
9697
cropped = to_grayscale(crop_faces(cv_image, faces))
9798
resized = cv2.resize(cropped, (100,100))
9899

99-
model = cv2.createFisherFaceRecognizer()
100-
#model = cv2.createEigenFaceRecognizer()
100+
model = cv2.face.FisherFaceRecognizer_create()
101101
model.load(MODEL_FILE)
102102
prediction = model.predict(resized)
103103
result = {
@@ -162,6 +162,6 @@ def persist(self, cv_image):
162162
load_images_to_db("data/images")
163163
#train()
164164

165-
print 'done'
165+
print('done')
166166
#predict()
167167
#train()

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
opencv-contrib-python
2+
pillow
3+
numpy
4+
sklearn
5+
tornado
6+
peewee

server.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
import uuid
99
from PIL import Image
1010
import time
11-
import StringIO
11+
from io import StringIO
1212
import uuid
1313
import numpy
1414
import json
15-
from tornado.options import define, options
15+
from tornado.options import options
1616
import opencv
1717

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

2020
class Application(tornado.web.Application):
2121
def __init__(self):
2222
handlers = [
23-
#(r"/", MainHandler),
24-
#(r"/facedetector", FaceDetectHandler),
23+
(r"/main", MainHandler),
24+
(r"/facedetector", FaceDetectHandler),
2525
(r"/", SetupHarvestHandler),
2626
(r"/harvesting", HarvestHandler),
2727
(r"/predict", PredictHandler),
@@ -114,8 +114,10 @@ def main():
114114
opencv.train()
115115
logging.info("Model trained")
116116
app = Application()
117+
options.parse_command_line()
118+
print(' -- app is listen on: {}'.format(options.port))
117119
app.listen(options.port)
118-
tornado.ioloop.IOLoop.instance().start()
120+
tornado.ioloop.IOLoop.current().start()
119121

120122
if __name__ == "__main__":
121123
main()

0 commit comments

Comments
 (0)