class feature: def __init__(self): if not detector.GLOBAL_WINDOWS: self.__term_ = TerminalController() def extract_features(self, images, algorithm='surf'): self.__total_ = len(images) if not detector.GLOBAL_WINDOWS: self.__progress_ = ProgressBar(self.__term_, 'Extracting features') self.update_progress(c_int(0)) p_jobs=10 else: p_jobs=5 if algorithm=='hog': images = Parallel(n_jobs=-1)(delayed(process_image_hog)(self, i) for i in images) else: if algorithm=='surf': self.__surf_extractor_=surf.surf() images = self.__surf_extractor_.extract_features(images, 100) return images def get_hog_fd(self, image): fd = hog(image, orientations=8, pixels_per_cell=(10, 10), cells_per_block=(1, 1), normalise=True) return fd def update_progress(self, c): if detector.GLOBAL_WINDOWS: print "Working on "+str(c.value)+"/"+str(self.__total_) else: self.__progress_.update(float(c.value)/self.__total_, 'working on %i' % c.value)
class preprocess: def __init__(self, image_paths): if not detector.GLOBAL_WINDOWS: self.__term_ = TerminalController() self.imgIndex = 0 self.__image_paths_ = image_paths def process_images(self): self.print_replace("Starting processing") if not detector.GLOBAL_WINDOWS: self.__progress_ = ProgressBar(self.__term_, "Processing images") p_jobs = 15 else: p_jobs = 10 images = Parallel(n_jobs=-1)(delayed(process_image2)(self, i) for i in self.__image_paths_) return images def rgb2singch(self, image): # return rgb2gray(image) #---Normal grayscale normalisation return image[:, :, 1] # Green channel # return image[:,:,0] #Red Channel # return image[:,:,2] #Blue Channel def median_filter(self, image): return skimage.filters.median(image, skimage.morphology.rectangle(3, 3)) def gaussian_filter(self, image): return skimage.filters.gaussian_filter(image, 2) def print_replace(self, text): sys.stdout.write(text) sys.stdout.flush() if not detector.GLOBAL_WINDOWS: sys.stdout.write(self.__term_.BOL + self.__term_.CLEAR_EOL) def update_progress(self, filename, c): if detector.GLOBAL_WINDOWS: print "Working on " + filename + str(c.value) + "/" + str(len(self.__image_paths_)) else: self.__progress_.update(float(c.value) / len(self.__image_paths_), "working on %s" % filename)