@@ -39,20 +39,20 @@ def crop_faces(img, faces):
3939def 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
7274def 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
8587def 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()
0 commit comments