def classifyLocalImagesWithDismantler(self, corpusPath = None): Opt_dmtler = Option_Dismantler(isTrain = False) Dmtler = Dismantler(Opt_dmtler) if corpusPath is None: corpusPath = self.Opt.classifyCorpusPath if self.Classifier is not None: print 'Start classifying images with dismantler in local disk...' startTime = time.time() nImageAll = 0 header = ['file_path', 'number_subimages', 'sub_class_name', 'sub_probability', 'segmentation', 'subimage_height', 'subimage_width'] csvSavingPath = self.Opt.resultPath csvFilename = 'class_result' Common.saveCSV(csvSavingPath, csvFilename, header = header, mode = 'wb', consoleOut = False) for dirPath, dirNames, fileNames in os.walk(corpusPath): for f in fileNames: fname, suffix = Common.getFileNameAndSuffix(f) if suffix in self.Opt.validImageFormat: filename = os.path.join(dirPath, f) # Load image img = ImageLoader.loadImageByPath(filename) # Dismantle image nodeList = Dmtler.dismantle(img) # Get number of sub-images if len(nodeList) > 0: numSubImages = len(nodeList) else: numSubImages = 1 # Remove surrounding empty space nodeList = Dmtler.updateImageToEffectiveAreaFromNodeList(img, nodeList, Opt_dmtler.thresholds) # Load all sub-images if len(nodeList) > 0: imData, imDims, dimSum = ImageLoader.loadSubImagesByNodeList(img, nodeList, self.Opt.finalDim, self.Opt.preserveAspectRatio) else: imData, imDims, dimSum = ImageLoader.loadImagesByList([filename], self.Opt.finalDim, self.Opt.preserveAspectRatio) # Extracting Features X = self.FeatureDescriptor.extractFeatures(imData, 1) # Classify y_pred, y_proba = self.Classifier.predict(X) sub_classes = '' sub_probs = '' sub_height = '' sub_width = '' segmentations = '' for i in range(0, len(nodeList)): if i != 0: sub_classes += ',' sub_probs += ',' sub_height += ',' sub_width += ',' segmentations += ',' sub_classes += str(y_pred[i]) sub_height += str(imDims[i][0]) sub_width += str(imDims[i][1]) sub_prob = '' for j in range(0, len(y_proba[i])): if j != 0: sub_prob += ':' sub_prob += str(y_proba[i][j]) sub_probs += sub_prob node = nodeList[i] segmentation = str(node.info['start'][0]) + ':' + \ str(node.info['start'][1]) + ':' + \ str(node.info['end'][0]) + ':' + \ str(node.info['end'][1]) segmentations += segmentation result = zip([filename], [numSubImages], [sub_classes], [sub_probs], [segmentations], [sub_height], [sub_width]) print result Common.saveCSV(csvSavingPath, csvFilename, result, mode = 'ab', consoleOut = False) nImageAll += 1 if np.mod(nImageAll, 100) == 0: print '%d images have been classified.' % nImageAll costTime = time.time() - startTime print 'All %d images were classified and saved in %s within %d sec.' % (nImageAll, os.path.join(csvSavingPath, csvFilename), costTime) else: print 'Classifier not loaded'
def classifyCloudImagesWithDismantler(self, keyPath = None, host = None): Opt_dmtler = Option_Dismantler(isTrain = False) Dmtler = Dismantler(Opt_dmtler) if self.Classifier is not None: try: cImageLoader = CloudImageLoader(self.Opt, keyPath = keyPath, host = host) bucketList = cImageLoader.getBucketList() except: print 'Unable to connect cloud server' print 'Start classifying images on cloud server...' startTime = time.time() nImageAll = 0 header = ['file_path', 'number_subimages', 'sub_class_name', 'sub_probability', 'segmentation', 'subimage_height', 'subimage_width'] csvSavingPath = self.Opt.resultPath csvFilename = 'class_result' Common.saveCSV(csvSavingPath, csvFilename, header = header, mode = 'wb', consoleOut = False) for key in bucketList: isValidImage, suffix = cImageLoader.isKeyValidImageFormat(key) if isValidImage: # Get image from s3 img = CloudImageLoader.keyToValidImage(key) # Dismantle the image nodeList = Dmtler.dismantle(img) # Get number of sub-images if len(nodeList) > 0: numSubImages = len(nodeList) else: numSubImages = 1 # Remove surrounding empty space nodeList = Dmtler.updateImageToEffectiveAreaFromNodeList(img, nodeList, Opt_dmtler.thresholds) # Load all sub-images if len(nodeList) > 0: imData, imDims, dimSum = ImageLoader.loadSubImagesByNodeList(img, nodeList, self.Opt.finalDim, self.Opt.preserveAspectRatio) else: imData, imDim = ImageLoader.preImageProcessing(img, self.Opt.finalDim, self.Opt.preserveAspectRatio) # Extracting Features X = self.FeatureDescriptor.extractFeatures(imData, 1) # Classify y_pred, y_proba = self.Classifier.predict(X) sub_classes = '' sub_probs = '' sub_height = '' sub_width = '' segmentations = '' for i in range(0, len(nodeList)): if i != 0: sub_classes += ',' sub_probs += ',' sub_height += ',' sub_width += ',' segmentations += ',' sub_classes += str(y_pred[i]) sub_height += str(imDims[i][0]) sub_width += str(imDims[i][1]) sub_prob = '' for j in range(0, len(y_proba[i])): if j != 0: sub_prob += ':' sub_prob += str(y_proba[i][j]) sub_probs += sub_prob node = nodeList[i] segmentation = str(node.info['start'][0]) + ':' + \ str(node.info['start'][1]) + ':' + \ str(node.info['end'][0]) + ':' + \ str(node.info['end'][1]) segmentations += segmentation result = zip([key.name], [numSubImages], [sub_classes], [sub_probs], [segmentations], [sub_height], [sub_width]) Common.saveCSV(csvSavingPath, csvFilename, result, mode = 'ab', consoleOut = False) nImageAll += 1 if np.mod(nImageAll, 100) == 0: print '%d images have been classified' % nImageAll costTime = time.time() - startTime print 'All %d images were classified and saved in %s within %d sec.' % (nImageAll, os.path.join(csvSavingPath, csvFilename), costTime) else: print 'Classifier not loaded'