def gph_datasets_file_reorder_by_mapping(self, dir, gph_mapping_restore_file): sys.stderr.write("Reordering gph files in %s by mapping"%gph_dir) prefix = '' choice = 1 instance = rename(dir, prefix, choice, gph_mapping_restore_file) instance.run() del instance #avoid the open file handlers
def datasets_reorder_by_sort(self, dir, organism, datasets_mapping_file): sys.stderr.write("Reordering datasets in %s by sorting"%dir) prefix = '%s_dataset'%organism choice = 4 instance = rename(dir, prefix, choice, datasets_mapping_file) instance.run() del instance
def detect(self): if not os.path.isfile(self.cascade_file): raise RuntimeError("%s: not found" % self.cascade_file) # rename files if self.name == 1: rename(self.src) # Create face classifier cascade = cv2.CascadeClassifier(self.cascade_file) # get files files = [ n for x in os.walk(self.src) for n in glob(os.path.join(x[0], '*.*')) ] # model loop for image_file in files: # create target path target_dir = "/".join(image_file.strip("/").split('/')[1:-1]) target_dir = os.path.join(self.dst, target_dir) + "/" # create target dir if doesn't exists if not os.path.exists(target_dir): os.makedirs(target_dir) image = cv2.imread(image_file) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) faces = cascade.detectMultiScale( gray, # model options scaleFactor=1.1, minNeighbors=5, minSize=(120, 120)) # crop, resize, and save image WIDTH = 96 HEIGHT = 96 for (x, y, w, h) in faces: crop_img = image[y:y + h, x:x + w] crop_img = cv2.resize(crop_img, (WIDTH, HEIGHT), interpolation=cv2.INTER_AREA) filename = os.path.basename(image_file).split('.')[0] cv2.imwrite(os.path.join(target_dir, filename + ".jpg"), crop_img) print("%s: has been cropped" % image_file)