def fuseImages(locale,inputPath=None,notif=True,outputPath=None): if notif: note = notify.getNotify() note.push('Fusing images from %s' % locale) else: print('Fusing images from %s' % locale) if not inputPath: inputPath = util.checkFolder('JPEGS', Input=True) tiff = util.checkFolder(locale,path=inputPath) tiffImages = tracker.parseFolder(tiff,findPosition=False, type='tif') tiffPairs = tracker.findPair(tiffImages,'tif') if not outputPath: outputPath = util.checkFolder(locale, path=inputPath) outputPath = util.checkFolder('Fused',path=outputPath) try: for i,pair in enumerate(tiffPairs): if notif: note.push('fusing %d :%d'%(i+1,(len(tiffPairs)))) imageVV, imageVH = satImage(pair[0],findPosition=True), satImage(pair[1],findPosition=True) imageVV.fuseWith(imageVH,outputFolder=outputPath) except KeyboardInterrupt: sys.exit() if notif: note.push('Finished fusion')
def select(self, model, steps, index): if model not in self.models: raise ValueError('Model %s was not found in the model list ' % model) modelPath = util.checkFolder('models') modelPath = util.checkFolder(model, path=modelPath) folders = [ f for f in os.listdir(modelPath) if os.path.isdir(os.path.join(modelPath, f)) ] found = False for folder in folders: try: num = folder if str(num) == str(index): found = True break except: pass if found: modelPath = os.path.join(modelPath, str(index)) oldStep = self.get(model, modelPath, 'steps') if not steps: steps = 0 return modelPath, oldStep + steps else: raise ValueError( '%s model index: %d was not found in %s directory' % (model, index, modelPath))
def colourImage(self, colour, *args): colour = os.path.join(util.checkFolder('Colour'), colour) if os.path.exists(colour): output = util.checkFolder('Outputs') output = util.checkFolder('Colour', output) if len(args) is not 0: oType = args[0].upper() output = util.checkFolder(oType, output) name = self.name.split('.')[0] + '.' + oType.lower() oFile = os.path.join(output, name) gdaldem = [ 'gdaldem', 'colour-relief', self.path, colour, oFile, 'of', oType ] else: output = checkFolder(self.name.split('.')[1], output) name = self.name oFile = os.path.join(output, name) gdaldem = ['gdaldem', 'color-relief', self.path, colour, oFile] proc = subprocess.Popen(gdaldem) proc.wait() print("Colour relief for %s was created" % oFile) else: print('Could not find colour file %s' % colour)
def organise(locale): directory = util.checkFolder('JPEGS',Input=True) directory = util.checkFolder(locale,path=directory) images = tracker.parseFolder(directory,findPosition=False,type='jpg') imageData = tracker.findImages(images,'jpg',directory) imageData = labelImages(imageData) stalker = tracker() stalker.add(imageData) stalker.saveTracker()
def polymerization(self, image2, mode=None, saveImages=False, level=None, clean=True, intersectOutputPath=None, outputFolder=None): if not intersectOutputPath: intersectOutputPath = util.checkFolder('Fusion', Input=True) try: outputNames = [self.name, image2.name] except: outputNames = [self.name, 'IntersectOf' + self.name] intersectState = self.createIntersectionOf( image2, outputNames, saveImages=saveImages, outputFolder=intersectOutputPath) if not mode: mode = 'db7' if not level: level = 7 if type(intersectState) is list: array1 = intersectState[0] array2 = intersectState[1] # No need to delete image that weren't saved clean = False else: image1 = os.path.join(intersectOutputPath, outputNames[0]) array1 = io.imread(image1, True, 'gdal') image2 = os.path.join(intersectOutputPath, outputNames[1]) array2 = io.imread(image2, True, 'gdal') fusedImage = self.fuseImages(array1, array2, mode, level) if not outputFolder: outputFolder = util.checkFolder('Fusion', Output=True) oName = os.path.join(outputFolder, 'Fused' + self.name) io.imsave(oName, fusedImage) if clean: try: os.remove(image1) os.remove(image2) except FileNotFoundError: pass print('Created fused image :%s' % oName)
def latestPath(model): modelPath = util.checkFolder('models') modelPath = util.checkFolder(model, path=modelPath) latest = 0 folders = [ f for f in os.listdir(modelPath) if os.path.isdir(os.path.join(modelPath, f)) ] for folder in folders: try: num = int(folder) latest = num except: pass return util.checkFolder(str(latest), path=modelPath)
def createIntersectionOf( self, image, outPutNames, saveImages=False, outputFolder=None): #Creates two images in the same area src = self.dataset # We want a section of source that matches this: matchDs = image.dataset image1Array, image2Array, col1, row1, col2, row2, intersectLongLat = self.getIntersect( image) # Output / destination if saveImages: image1Filename = outPutNames[0] image2Filename = outPutNames[1] if not outputFolder: output = util.checkFolder("Transform", Output=True) else: output = outputFolder image1Output = os.path.join(output, image1Filename) image2Output = os.path.join(output, image2Filename) self.createTiff(image1Array, image1Output, col1, row1) self.createTiff(image2Array, image2Output, col2, row2) print('Created intersection images in :%s' % output) return True else: return [image1Array, image2Array]
def getFlip(filepath): dirpath = os.path.split(filepath)[0] dirpath = util.checkFolder('Flip',path = dirpath) name = tracker.getBasename(filepath) nameFlip = os.path.join(dirpath,name + 'f.jpg') imageOG = Image.open(filepath) return tracker.flip(nameFlip,imageOG)
def new(self, model, steps): if model not in nets.netModel: raise ValueError( 'Model %s was not found in the model list in nets' % model) modelPath = util.checkFolder('models') modelPath = util.checkFolder(model, path=modelPath) latest = 0 folders = [ f for f in os.listdir(modelPath) if os.path.isdir(os.path.join(modelPath, f)) ] for folder in folders: try: num = int(folder) latest = num except: pass latest += 1 modelPath = util.checkFolder(str(latest), path=modelPath) self.addModel(model, modelPath) return modelPath, steps
def loadCsv(self,name): filePath = util.checkFolder('JPEGS',Input = True) filePath = os.path.join(filePath,name) images = {} with open(filePath,'r') as imageFile: read = csv.reader(imageFile,delimiter=';') first = True for row in read: if first: first = False else: basename = row.pop(0) images.update({basename:row}) return images
def convertTo(self, type, outputPath=None): types = {'jpeg': ['.jpeg', self.toJpeg]} if not outputPath: outputPath = util.checkFolder('Tiff', Output=True) outputPath = os.path.join(outputPath, 'test.tiff') elif os.path.exists(os.path.dirname(outputPath)): outputName = self.name.split('.')[0] + types[type][0] outputPath = os.path.join(outputPath, outputName) print('Converting image to %s' % types[type][0][1:]) types[type][1](self.path, outputPath) print('Finished image to %s' % types[type][0])
def getRotate(filepath): images = [] dirpath = os.path.split(filepath)[0] dirpath = util.checkFolder('Rotate',path=dirpath) name = tracker.getBasename(filepath) name45 = os.path.join(dirpath, name + '45' + '.jpg') name90 = os.path.join(dirpath, name + '90' + '.jpg') name135 = os.path.join(dirpath, name +'135' + '.jpg') imageOG = Image.open(filepath) images.append(tracker.rotate(name45,45,imageOG)) images.append(tracker.rotate(name90,90,imageOG)) images.append(tracker.rotate(name135,135,imageOG)) return images
def writeCsv(self,name=None): filePath= util.checkFolder('JPEGS',Input=True) if not name: name = 'tracker.csv' filePath= os.path.join(filePath,name) with open(filePath, 'w', newline='') as imageFile: writer = csv.writer(imageFile, delimiter=';') headers = ['BASENAME','VH','VV','FUSED','LABEL'] writer.writerow(headers) for basename in self.files: tmp = [basename] for value in self.files[basename]: tmp.append(value) writer.writerow(tmp) print('finished writing')
def loadTracker(self): trackerName = 'imageTracker.pkl' trackerPath = util.checkFolder('trackers') self.path = os.path.join(trackerPath,trackerName) if os.path.isfile(self.path): try: with open(self.path, 'rb') as input: track = pickle.load(input) self.files = track.files self.labels = track.labels if hasattr(track,'trainList'): self.trainList = track.trainList if hasattr(track,'evalLIst'): self.evalList = track.evalList except: return else: return
def loadTracker(self): trackerName = 'modelTracker.pkl' trackerPath = util.checkFolder('trackers') self.path = os.path.join(trackerPath, trackerName) self.types = { 'new': self.new, 'latest': self.latest, 'select': self.select } if os.path.isfile(self.path): try: with open(self.path, 'rb') as input: track = pickle.load(input) self.models = track.models self.modelData = track.modelData except: return else: return
def fuseWith(self, image2, outputFolder=None, mode=None, level=None): array1 = self.array() array2 = image2.array() if not mode: mode = 'db7' if not level: level = 7 try: fusedImage = self.fuseImages(array1, array2, mode, level) except MemoryError: print('Unable to fuse %s' % self.name) raise return if not outputFolder: outputFolder = util.checkFolder('Fusion', Output=True) oName = os.path.join(outputFolder, 'Fused' + self.name) io.imsave(oName, fusedImage) print('Created fused image :%s' % oName)
def toJpeg(locale,inputPath =None,outputPath=None, fusion = False): directory = util.checkFolder('Proccessed', Output=True) if not inputPath: inputPath = util.checkFolder(locale,path=directory) if fusion: inputPath = util.checkFolder('Fused',path=inputPath) tiffImages = tracker.parseFolder(inputPath,findPosition=False, type='tif') if not outputPath: outputPath = util.checkFolder('JPEGS',path =directory) outputPath = util.checkFolder(locale,path=outputPath) if fusion: outputPath = util.checkFolder('Fused',path=outputPath) satImage(tiffImages[0]).convertTo('jpeg',outputPath=outputPath)
def evalNN(networkName='incept', status='latest', batchSize=35, index=None, numEvals=None, numClones=1, cloneCpu=False, tasks=0, workerReplias=1, numPSTasks=0, preprocessThread=2): modelStalker = modTracker() stalker = tracker() trainDir, fullSteps = modelStalker.load(networkName, status=status, index=index) datalist = modelStalker.modelData[trainDir][1] with tf.Graph().as_default(): #LOOK INTO MODEL CLASS**** # Use for parallelism, cant beat google deployConfig = modelDeploy.DeploymentConfig(num_clones=1, clone_on_cpu=cloneCpu, replica_id=tasks, num_replicas=workerReplias, num_ps_tasks=numPSTasks) with tf.device(deployConfig.variables_device()): globalStep = slim.create_global_step() network = getNetFunc(networkName, numClasses=stalker.numLabels(), isTraining=False) fileQueue, counter = dataset.getFileQueue(datalist, [100, 90]) tmp = '' sampleSize = 0 for l in counter: value = counter[l] sampleSize += value tmp += ' Label %d , Value %d' % (l, value) modelName = networkName + ': ' + os.path.basename(trainDir) checkpoint = tf.train.latest_checkpoint(trainDir) note.push('Running %s' % modelName) note.push('Dataset size : %s' % tmp) image, label, path = dataset.readFile(fileQueue) height, width = netsList[networkName].defaultImageSize, netsList[ networkName].defaultImageSize image = tf.image.resize_images(image, [height, width], method=0) # image = preprocess.preprocessImage(image,netsList[networkName].defaultImageSize,netsList[networkName].defaultImageSize,isTraining=True) images, labels = tf.train.batch([image, label], batch_size=batchSize, num_threads=preprocessThread, capacity=2 * batchSize) logits, _ = network(images) restoreVars = slim.get_variables_to_restore() prediction = tf.argmax(logits, 1) names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'Accuracy': slim.metrics.streaming_accuracy(prediction, labels), }) for name, value in names_to_values.items(): summary_name = 'eval/%s' % name op = tf.summary.scalar(summary_name, value, collections=[]) op = tf.Print(op, [value], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) if not numEvals: numEvals = math.ceil(len(datalist) / float(batchSize)) evalDir = util.checkFolder('eval', path=trainDir) note.push('Starting for %d evals' % numEvals) slim.evaluation.evaluate_once(master='', checkpoint_path=checkpoint, logdir=evalDir, num_evals=numEvals, eval_op=list(names_to_updates.values()), variables_to_restore=restoreVars) note.push('Finished eval')