def cropFaces(self, cascade=None): """ **SUMMARY** This function helps in cropping a certain object in all of the images by using the provided haar cascade. A haar classifier is implemented and images are cropped. This function also supports multiple faces in one single image. **PARAMETERS** cascade - haar cascade string **RETURNS** ImageSet **EXAMPLES** >>> imgs = ImageSet("some_directory/") >>> cascade = "face.xml" >>> faces = imgs.cropFaces(cascade) >>> faces.show() """ if not cascade: cascade = CASCADE_PATH else: if not os.path.isfile(cascade): warnings.warn( "The provided cascade does not exist. Using default cascade" ) cascade = CASCADE_PATH classifier = cv2.CascadeClassifier(cascade) gray = [cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) for img in self] objects = [ classifier.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) for img in gray ] imgs = [[img[ob[1]:ob[1] + ob[3], ob[0]:ob[0] + ob[2]] for ob in obj] for img, obj in zip(self, objects) if isinstance(obj, np.ndarray)] imgs = concatenate(*imgs) return ImageSet(imgs=imgs)
def cropFaces(self, cascade=None): """ **SUMMARY** This function helps in cropping a certain object in all of the images by using the provided haar cascade. A haar classifier is implemented and images are cropped. This function also supports multiple faces in one single image. **PARAMETERS** cascade - haar cascade string **RETURNS** ImageSet **EXAMPLES** >>> imgs = ImageSet("some_directory/") >>> cascade = "face.xml" >>> faces = imgs.cropFaces(cascade) >>> faces.show() """ if not cascade: cascade = CASCADE_PATH else: if not os.path.isfile(cascade): warnings.warn("The provided cascade does not exist. Using default cascade") cascade = CASCADE_PATH classifier = cv2.CascadeClassifier(cascade) gray = [cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) for img in self] objects = [classifier.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags = cv2.cv.CV_HAAR_SCALE_IMAGE) for img in gray] imgs = [[img[ob[1]:ob[1]+ob[3], ob[0]:ob[0]+ob[2]] for ob in obj] for img, obj in zip(self, objects) if isinstance(obj, np.ndarray)] imgs = concatenate(*imgs) return ImageSet(imgs=imgs)
def train(self, images=None, labels=None, csvfile=None, delimiter=";"): """ **SUMMARY** Train the face recognizer with images and labels. **PARAMETERS** * *images* - A list of Images or ImageSet. All the images must be of same size. * *labels* - A list of labels(int) corresponding to the image in images. There must be at least two different labels. * *csvfile* - You can also provide a csv file with image filenames and labels instead of providing labels and images separately. * *delimiter* - The delimiter used in csv files. **RETURNS** Nothing. None. **EXAMPLES** >>> f = FaceRecognizer() >>> imgs1 = ImageSet(path/to/images_of_type1) >>> labels1 = LabelSet("type1", imgs1) >>> imgs2 = ImageSet(path/to/images_of_type2) >>> labels2 = LabelSet("type2", imgs2) >>> imgs3 = ImageSet(path/to/images_of_type3) >>> labels3 = LabelSet("type3", imgs3) >>> imgs = concatenate(imgs1, imgs2, imgs3) >>> labels = concatenate(labels1, labels2, labels3) >>> f.train(imgs, labels) >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) Save Fisher Training Data >>> f.save("trainingdata.xml") Load Fisher Training Data and directly use without trainging >>> f1 = FaceRecognizer() >>> f1.load("trainingdata.xml") >>> imgs = ImageSet("path/to/testing_images") >>> print f1.predict(imgs) Use CSV files for training >>> f = FaceRecognizer() >>> f.train(csvfile="CSV_file_name", delimiter=";") >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) """ if csvfile: images = [] labels = [] import csv try: f = open(csvfile, "rb") except IOError: warnings.warn("No such file found. Training not initiated") return None self.csvfiles.append(csvfile) filereader = csv.reader(f, delimiter=delimiter) for row in filereader: images.append(Image(row[0])) labels.append(row[1]) if isinstance(labels, type(None)): warnings.warn("Labels not provided. Training not inititated.") return None self.labels_set = list(set(labels)) i = 0 for label in self.labels_set: self.labels_dict.update({label: i}) self.labels_dict_rev.update({i: label}) i += 1 if len(self.labels_set) < 2: warnings.warn("At least two classes/labels are required" "for training. Training not inititated.") return None if len(images) != len(labels): warnings.warn("Mismatch in number of labels and number of" "training images. Training not initiated.") return None self.imageSize = images[0].shape[:2] h, w = self.imageSize images = [img if img.shape[:2] == self.imageSize else cv2.resize(img, (w, h)) for img in images] self.int_labels = [self.labels_dict[key] for key in labels] self.train_labels = labels labels = np.array(self.int_labels) self.train_imgs = images cv2imgs = [cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) for img in images] self.model.train(cv2imgs, labels)
def predict(self, imgs): """ **SUMMARY** Predict the class of the image using trained face recognizer. **PARAMETERS** * *image* - Image.The images must be of the same size as provided in training. **RETURNS** * *label* - Class of the image which it belongs to. **EXAMPLES** >>> f = FaceRecognizer() >>> imgs1 = ImageSet(path/to/images_of_type1) >>> labels1 = LabelSet("type1", imgs1) >>> imgs2 = ImageSet(path/to/images_of_type2) >>> labels2 = LabelSet("type2", imgs2) >>> imgs3 = ImageSet(path/to/images_of_type3) >>> labels3 = LabelSet("type3", imgs3) >>> imgs = concatenate(imgs1, imgs2, imgs3) >>> labels = concatenate(labels1, labels2, labels3) >>> f.train(imgs, labels) >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) Save Fisher Training Data >>> f.save("trainingdata.xml") Load Fisher Training Data and directly use without trainging >>> f1 = FaceRecognizer() >>> f1.load("trainingdata.xml") >>> imgs = ImageSet("path/to/testing_images") >>> print f1.predict(imgs) Use CSV files for training >>> f = FaceRecognizer() >>> f.train(csvfile="CSV_file_name", delimiter=";") >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) """ if not self.supported: warnings.warn("Fisher Recognizer is supported by OpenCV >= 2.4.4") return None h, w = self.imageSize images = [img if img.shape[:2] == self.imageSize else cv2.resize(img, (w, h)) for img in imgs] if isinstance(imgs, np.ndarray): if imgs.shape[:2] != self.imageSize: image = cv2.resize(imgs, (w, h)) cv2img = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) label, confidence = self.model.predict(cv2img) retLabel = self.labels_dict_rev.get(label) if not retLabel: retLabel = label return (retLabel, confidence) retVal = [] for image in images: cv2img = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) label, confidence = self.model.predict(cv2img) retLabel = self.labels_dict_rev.get(label) if not retLabel: retLabel = label retVal.append((retLabel, confidence)) return retVal
def train(self, images=None, labels=None, csvfile=None, delimiter=";"): """ **SUMMARY** Train the face recognizer with images and labels. **PARAMETERS** * *images* - A list of Images or ImageSet. All the images must be of same size. * *labels* - A list of labels(int) corresponding to the image in images. There must be at least two different labels. * *csvfile* - You can also provide a csv file with image filenames and labels instead of providing labels and images separately. * *delimiter* - The delimiter used in csv files. **RETURNS** Nothing. None. **EXAMPLES** >>> f = FaceRecognizer() >>> imgs1 = ImageSet(path/to/images_of_type1) >>> labels1 = LabelSet("type1", imgs1) >>> imgs2 = ImageSet(path/to/images_of_type2) >>> labels2 = LabelSet("type2", imgs2) >>> imgs3 = ImageSet(path/to/images_of_type3) >>> labels3 = LabelSet("type3", imgs3) >>> imgs = concatenate(imgs1, imgs2, imgs3) >>> labels = concatenate(labels1, labels2, labels3) >>> f.train(imgs, labels) >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) Save Fisher Training Data >>> f.save("trainingdata.xml") Load Fisher Training Data and directly use without trainging >>> f1 = FaceRecognizer() >>> f1.load("trainingdata.xml") >>> imgs = ImageSet("path/to/testing_images") >>> print f1.predict(imgs) Use CSV files for training >>> f = FaceRecognizer() >>> f.train(csvfile="CSV_file_name", delimiter=";") >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) """ if csvfile: images = [] labels = [] import csv try: f = open(csvfile, "rb") except IOError: warnings.warn("No such file found. Training not initiated") return None self.csvfiles.append(csvfile) filereader = csv.reader(f, delimiter=delimiter) for row in filereader: images.append(Image(row[0])) labels.append(row[1]) if isinstance(labels, type(None)): warnings.warn("Labels not provided. Training not inititated.") return None self.labels_set = list(set(labels)) i = 0 for label in self.labels_set: self.labels_dict.update({label: i}) self.labels_dict_rev.update({i: label}) i += 1 if len(self.labels_set) < 2: warnings.warn("At least two classes/labels are required" "for training. Training not inititated.") return None if len(images) != len(labels): warnings.warn("Mismatch in number of labels and number of" "training images. Training not initiated.") return None self.imageSize = images[0].shape[:2] h, w = self.imageSize images = [ img if img.shape[:2] == self.imageSize else cv2.resize( img, (w, h)) for img in images ] self.int_labels = [self.labels_dict[key] for key in labels] self.train_labels = labels labels = np.array(self.int_labels) self.train_imgs = images cv2imgs = [cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) for img in images] self.model.train(cv2imgs, labels)
def predict(self, imgs): """ **SUMMARY** Predict the class of the image using trained face recognizer. **PARAMETERS** * *image* - Image.The images must be of the same size as provided in training. **RETURNS** * *label* - Class of the image which it belongs to. **EXAMPLES** >>> f = FaceRecognizer() >>> imgs1 = ImageSet(path/to/images_of_type1) >>> labels1 = LabelSet("type1", imgs1) >>> imgs2 = ImageSet(path/to/images_of_type2) >>> labels2 = LabelSet("type2", imgs2) >>> imgs3 = ImageSet(path/to/images_of_type3) >>> labels3 = LabelSet("type3", imgs3) >>> imgs = concatenate(imgs1, imgs2, imgs3) >>> labels = concatenate(labels1, labels2, labels3) >>> f.train(imgs, labels) >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) Save Fisher Training Data >>> f.save("trainingdata.xml") Load Fisher Training Data and directly use without trainging >>> f1 = FaceRecognizer() >>> f1.load("trainingdata.xml") >>> imgs = ImageSet("path/to/testing_images") >>> print f1.predict(imgs) Use CSV files for training >>> f = FaceRecognizer() >>> f.train(csvfile="CSV_file_name", delimiter=";") >>> imgs = ImageSet("path/to/testing_images") >>> print f.predict(imgs) """ if not self.supported: warnings.warn("Fisher Recognizer is supported by OpenCV >= 2.4.4") return None h, w = self.imageSize images = [ img if img.shape[:2] == self.imageSize else cv2.resize( img, (w, h)) for img in imgs ] if isinstance(imgs, np.ndarray): if imgs.shape[:2] != self.imageSize: image = cv2.resize(imgs, (w, h)) cv2img = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) label, confidence = self.model.predict(cv2img) retLabel = self.labels_dict_rev.get(label) if not retLabel: retLabel = label return (retLabel, confidence) retVal = [] for image in images: cv2img = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) label, confidence = self.model.predict(cv2img) retLabel = self.labels_dict_rev.get(label) if not retLabel: retLabel = label retVal.append((retLabel, confidence)) return retVal