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

Commit 1f1cc81

Browse files
committed
Rename .target to .label and class
1 parent 47f50c7 commit 1f1cc81

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ You can install the package as follows:
2424
cd <project_root>
2525
pip install .
2626
```
27-
Next, you can import the package as `import ezfaces` or its main class as `from ezfaces.face_classifier import faceClassifier`.
27+
Next, you can import the package as `import ezfaces` or its main class as `from ezfaces.face_classifier import FaceClassifier`.
2828

2929

3030
The project has been tested in CI (see [workflows](https://github.com/0xLeo/EZfaces/tree/master/.github/workflows)) in Python 3.7 and 3.8 with the following dependencies installed, but newer versions will also work:
@@ -41,9 +41,9 @@ scikit-learn 0.22
4141
# Usage examples
4242
**1. Load new subject from folder**
4343
```
44-
from ezfaces.face_classifier import faceClassifier
44+
from ezfaces.face_classifier import FaceClassifier
4545
46-
fc = faceClassifier()
46+
fc = FaceClassifier()
4747
lbl_new = fc.add_img_data('tests/images_yale')
4848
print(fc)
4949
print("New subject\'s label is %d" % lbl_new)
@@ -57,11 +57,11 @@ New subject's label is 40
5757

5858
**2. Load new subject and predict from webcam**
5959
```
60-
from from ezfaces.face_classifier import faceClassifier
60+
from from ezfaces.face_classifier import FaceClassifier
6161
import cv2
6262
6363
64-
fc = faceClassifier()
64+
fc = FaceClassifier()
6565
lbl_new = fc.add_img_data(from_webcam=True)
6666
fc.train()
6767
# take a snapshot from webcam
@@ -78,18 +78,18 @@ cv2.destroyAllWindows()
7878

7979
**3. Export and import dataset**
8080
```
81-
from ezfaces.face_classifier import faceClassifier
81+
from ezfaces.face_classifier import FaceClassifier
8282
8383
84-
fc = faceClassifier()
84+
fc = FaceClassifier()
8585
data_file, lbl_file = fc.export('/tmp')
8686
8787
# add some data
8888
lbl_new = fc.add_img_data('tests/images_yale')
8989
print(fc)
9090
9191
# now let's say we made a mistake and don't like the new data
92-
fc = faceClassifier(data_pkl = data_file, target_pkl = lbl_file)
92+
fc = FaceClassifier(data_pkl = data_file, target_pkl = lbl_file)
9393
print(fc)
9494
```
9595
Output:

ezfaces/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# __init__.py
2-
from .face_classifier import faceClassifier
2+
from .face_classifier import FaceClassifier

ezfaces/face_classifier.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import List, Tuple
1313

1414

15-
class faceClassifier():
15+
class FaceClassifier():
1616
def __init__(self, ratio = 0.85, K = 200, data_pkl = None, target_pkl = None):
1717
"""__init__. Class constructor.
1818
@@ -34,9 +34,9 @@ def __init__(self, ratio = 0.85, K = 200, data_pkl = None, target_pkl = None):
3434
self.data = None # data vectors
3535
if target_pkl is not None:
3636
with open(target_pkl, 'rb') as f:
37-
self.target = pkl.load(f)
37+
self.labels = pkl.load(f)
3838
else:
39-
self.target = None # label (ground truth) vectors
39+
self.labels = None # label (ground truth) vectors
4040
self.train_data = OD() # maps sample index to data and label
4141
self.test_data = OD() # maps sample index to data and label
4242
# how many eigenfaces to keep
@@ -48,7 +48,7 @@ def __init__(self, ratio = 0.85, K = 200, data_pkl = None, target_pkl = None):
4848
self.classification_report = None # obtained from benchmarking
4949
# mean needed for reconstruction
5050
self._mean = np.zeros((1, 64*64), dtype=np.float32)
51-
if self.data is None and self.target is None: # no pre-loaded data
51+
if self.data is None and self.labels is None: # no pre-loaded data
5252
self._load_olivetti_data()
5353
self._TRAIN_SAMPLE = 1
5454
self._PRED_SAMPLE = 0
@@ -67,7 +67,7 @@ def _load_olivetti_data(self):
6767
# data as floating vectors of length 64^2, ranging from 0 to 1
6868
self.data = np.array(data)
6969
# subject labels (sequential from to 0 to ...)
70-
self.target = target
70+
self.labels = target
7171

7272

7373
def _record_mean(self):
@@ -112,7 +112,7 @@ def _read_from_webcam(self, new_label, stream = 0):
112112
cv2.destroyWindow("new data")
113113
x = self.img2vec(im_cropped)
114114
self.data = np.array([*self.data, np.array(x, dtype=np.float32)])
115-
self.target = np.append(self.target, new_label)
115+
self.labels = np.append(self.labels, new_label)
116116
cap.release()
117117
cv2.destroyAllWindows()
118118

@@ -132,14 +132,14 @@ def add_img_data(self, dir_img: str = "", from_webcam: bool = False) -> int:
132132
int
133133
The label of the newly added subject.
134134
"""
135-
assert len(self.target) != 0, "No labels have been generated!"
135+
assert len(self.labels) != 0, "No labels have been generated!"
136136
# find all images in given folder
137137
fpaths = glob.glob(os.path.join(dir_img, '*.png'))
138138
fpaths += glob.glob(os.path.join(dir_img, '*.jpg'))
139139
fpaths += glob.glob(os.path.join(dir_img, '*.bmp'))
140140
# create new label for new subject
141-
target_new = self.target[-1] + 1
142-
self.target = np.append(self.target, [target_new]*len(fpaths))
141+
target_new = self.labels[-1] + 1
142+
self.labels = np.append(self.labels, [target_new]*len(fpaths))
143143
# convert image to 64*64 data vector (ranging from 0 to 1)
144144
for i, f in enumerate(fpaths):
145145
im = cv2.imread(f)
@@ -185,11 +185,11 @@ def _divide_dataset(self, ratio = 0.85):
185185
# {index: (data_vector, data_label)}, index starts from 0
186186
self.train_data = OD( # ordered dict
187187
dict(zip(train_inds, # keys
188-
zip(self.data[train_inds,:], self.target[train_inds]))) # vals
188+
zip(self.data[train_inds,:], self.labels[train_inds]))) # vals
189189
)
190190
self.test_data = OD( # ordered dict
191191
dict(zip(test_inds, # keys
192-
zip(self.data[test_inds,:], self.target[test_inds]))) # vals
192+
zip(self.data[test_inds,:], self.labels[test_inds]))) # vals
193193
)
194194

195195

@@ -246,7 +246,6 @@ def img2vec(self, im) -> np.ndarray:
246246
if not len(im.shape) == 2:
247247
raise RuntimeError("Provide a greyscale image as input.")
248248
x = np.asarray(cv2.resize(im, dsize=(64,64)), np.float32).ravel()
249-
import pdb; pdb.set_trace()
250249
x /= 255
251250
x = np.reshape(x, self._mean.shape)
252251
x -= self._mean
@@ -343,11 +342,11 @@ def export(self, dest_folder = '/tmp') -> Tuple[str, str]:
343342
"""
344343
try:
345344
fpath_data = os.path.join(dest_folder, 'data.pkl')
346-
fpath_lbl = os.path.join(dest_folder, 'target.pkl')
345+
fpath_lbl = os.path.join(dest_folder, 'labels.pkl')
347346
with open(fpath_data, 'wb') as f:
348347
pkl.dump(self.data, f)
349348
with open(fpath_lbl, 'wb') as f:
350-
pkl.dump(self.target, f)
349+
pkl.dump(self.labels, f)
351350
print("Wrote data and target as .pkl at:\n%s"
352351
% os.path.abspath(dest_folder))
353352
return os.path.abspath(fpath_data), os.path.abspath(fpath_lbl)
@@ -376,7 +375,7 @@ def show_album(self, wait_time = 2.0):
376375
how long to wait between successive grid pages in sec
377376
"""
378377
data_every_64 = self.grouper(self.data, 64)
379-
lbl_every_64 = self.grouper(self.target, 64)
378+
lbl_every_64 = self.grouper(self.labels, 64)
380379
cols, rows = 8, 8
381380
blank = np.zeros((64, 64), np.uint8)
382381

tests/test_face_classifier.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,40 @@
44
this_script_path = os.path.abspath(__file__)
55
this_script_folder = os.path.dirname(this_script_path)
66
sys.path.insert(1, os.path.join(this_script_folder, '..', 'ezfaces'))
7-
from face_classifier import faceClassifier
7+
from face_classifier import FaceClassifier
88
import numpy as np
99

1010

1111
class TestUM(unittest.TestCase):
1212

1313
def test_train_with_olivetti(self):
14-
fc = faceClassifier()
14+
fc = FaceClassifier()
1515
self.assertEqual(len(fc.data.shape), 2)
1616
# data stored as 64*64 row vectors
1717
self.assertEqual(fc.data.shape[1], 64*64)
1818
# Olivetti data contain 40 subjects
19-
self.assertEqual(len(np.unique(fc.target)), 40)
19+
self.assertEqual(len(np.unique(fc.labels)), 40)
2020
fc.train()
2121
# their coordinates in eigenface space as a matrix (.W)
2222
self.assertEqual(len(fc.W.shape), 2)
2323

2424

2525
def test_train_with_subject(self):
2626
img_dir = os.path.join(this_script_folder, 'images_yale')
27-
fc = faceClassifier()
27+
fc = FaceClassifier()
2828
fc.add_img_data(img_dir)
2929
# data stored as 64*64 row vectors
3030
self.assertEqual(fc.data.shape[1], 64*64)
3131
# 40 + 1 subjects
32-
self.assertEqual(len(np.unique(fc.target)), 41)
32+
self.assertEqual(len(np.unique(fc.labels)), 41)
3333
fc.train()
3434
# their coordinates in eigenface space as a matrix (.W)
3535
self.assertEqual(len(fc.W.shape), 2)
3636

3737

3838
def test_benchmark(self):
3939
img_dir = os.path.join(this_script_folder, 'images_yale')
40-
fc = faceClassifier(ratio = .725)
40+
fc = FaceClassifier(ratio = .725)
4141
fc.add_img_data(img_dir)
4242
fc.benchmark()
4343
self.assertNotEqual(fc.classification_report, None)
@@ -47,17 +47,17 @@ def test_benchmark(self):
4747

4848
def test_export_import(self):
4949
img_dir = os.path.join(this_script_folder, 'images_yale')
50-
fc = faceClassifier()
50+
fc = FaceClassifier()
5151
fc.add_img_data(img_dir)
5252
# write as pickle files
5353
fc.export()
5454

55-
fc2 = faceClassifier(data_pkl = '/tmp/data.pkl', target_pkl = '/tmp/target.pkl')
56-
self.assertEqual(len(np.unique(fc2.target)), 41)
55+
fc2 = FaceClassifier(data_pkl = '/tmp/data.pkl', target_pkl = '/tmp/labels.pkl')
56+
self.assertEqual(len(np.unique(fc2.labels)), 41)
5757

5858

5959
def test_show_album(self):
60-
fc = faceClassifier()
60+
fc = FaceClassifier()
6161
fc.show_album(wait_time=.1)
6262

6363

0 commit comments

Comments
 (0)