def checkResult( self, tested_image, reference_image ): """ Check if testVar and refVar are equal with the given precision and the given type @param tested_image: variable computed by the test @param reference_image: array variable containing the reference value(s), the precision and the type -> reference value(s) to compare with the testVar -> numerical precision to use to chekc if variables are equal -> type of variables to check @return: True is variables are equal, False otherwise """ print "Comparing {} and {}".format(reference_image, tested_image) command = "otbcli_CompareImages -ref.in {} -meas.in {}".format(reference_image, tested_image) mse = -9999 mae = -9999 psnr = -9999 res = run_process(command) lines = str(res).splitlines() # for line in lines: # # sensor_line = result_sensor[0] # mse = re.search('mse: (\d*)$', line) # if mse: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mse = mse.group(1) # continue # print "suite", lines # for line in lines: # mae = re.search('mae: (\d*)$', line) # if mae: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mae = mae.group(1) # print "MAE", mae # continue # for line in lines: # psnr = re.search('mse: (\d*)$', line) # if psnr: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mse = psnr.group(1) # continue mse_line = lines[-4] mae_line = lines[-3] psnr_line = lines[-2] mse = re.search('mse: (\d*)$', mse_line).group(1).replace(" ", "") mae = re.search('mae: (\d*)$', mae_line).group(1).replace(" ", "") psnr = re.search('psnr: (\d*)$', psnr_line).group(1).replace(" ", "") if mse == mae == psnr == "0": return True else: print '"{}"'.format(mse) print "mse: {}, \n mae {}, \n psnr: {}".format(mse, mae, psnr) return False
def checkResult(self, tested_image, reference_image): """ Check if testVar and refVar are equal with the given precision and the given type @param tested_image: variable computed by the test @param reference_image: array variable containing the reference value(s), the precision and the type -> reference value(s) to compare with the testVar -> numerical precision to use to chekc if variables are equal -> type of variables to check @return: True is variables are equal, False otherwise """ print "Comparing {} and {}".format(reference_image, tested_image) command = "otbcli_CompareImages -ref.in {} -meas.in {}".format( reference_image, tested_image) mse = -9999 mae = -9999 psnr = -9999 res = run_process(command) lines = str(res).splitlines() # for line in lines: # # sensor_line = result_sensor[0] # mse = re.search('mse: (\d*)$', line) # if mse: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mse = mse.group(1) # continue # print "suite", lines # for line in lines: # mae = re.search('mae: (\d*)$', line) # if mae: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mae = mae.group(1) # print "MAE", mae # continue # for line in lines: # psnr = re.search('mse: (\d*)$', line) # if psnr: # # group 1 parce qu'on a demande qqchose de particulier a la regexpr a cause des () # mse = psnr.group(1) # continue mse_line = lines[-4] mae_line = lines[-3] psnr_line = lines[-2] mse = re.search('mse: (\d*)$', mse_line).group(1).replace(" ", "") mae = re.search('mae: (\d*)$', mae_line).group(1).replace(" ", "") psnr = re.search('psnr: (\d*)$', psnr_line).group(1).replace(" ", "") if mse == mae == psnr == "0": return True else: print '"{}"'.format(mse) print "mse: {}, \n mae {}, \n psnr: {}".format(mse, mae, psnr) return False
def classify(self): dirDest = QtGui.QFileDialog.getExistingDirectory( None, str("Répertoire de destination des fichiers de la classification"), self.output_dir) if dirDest: self.output_dir = dirDest logger.debug("classify") simulation = False # Get rasters selectedrasterlayers = self.rasterlayerselector.getSelectedOptions() logger.debug("selectedrasterlayers" + str(selectedrasterlayers)) if len(selectedrasterlayers) < 1: QtGui.QMessageBox.critical( self, \ u"Erreur", \ u"Aucune couche raster sélectionnée" ) return # Get vectors selectedvectorlayers = self.vectorlayerselector.getSelectedOptions() if len(selectedvectorlayers) < 2: QtGui.QMessageBox.critical( self, \ u"Erreur", \ u"Au minimum deux couches vecteur doivent être sélectionnées" ) return try: errorDuringClassif = False outputdir = self.getOutputDir() # Build list of input raster files rasterlist = "" for r in selectedrasterlayers: rasterlist += '"%s" ' % (r.source()) #logger.debug( "rasterlist" + str( rasterlist ) ) #firstraster = '"%s" ' % (unicode(selectedrasterlayers[0].source())) logger.debug("selectedrasterlayers[0]" + str(selectedrasterlayers[0])) firstraster = '"%s" ' % (selectedrasterlayers[0].source()) # Build list of input vector files vectorlist = "" labeldescriptor = {} label = 0 for i in range(len(selectedvectorlayers)): v = selectedvectorlayers[i] inputshpfilepath = v[0].source() classcolor = v[1] classlabel = v[2] labeldescriptor[label] = (classcolor, classlabel) logger.debug("labeldescriptor" + str(labeldescriptor)) label += 1 # Reprocess input shp file to crop it to firstraster extent vectordir = os.path.join(outputdir, 'class%s' % (str(i))) ensure_clean_dir(vectordir) #preprocessedshpfile = os.path.join(vectordir, transform_spaces(os.path.basename(unicode(inputshpfilepath)))) preprocessedshpfile = os.path.join(vectordir, "preprocessed.shp") cropcommand = ('%s/cropvectortoimage.bat ' '"%s" ' '"%s" ' '"%s" ' '"%s" ' '"%s" ' % (self.app_dir, inputshpfilepath, selectedrasterlayers[0].source(), os.path.join(vectordir, "imageenveloppe.shp"), os.path.join(vectordir, "tmp_reprojected.shp"), preprocessedshpfile)) terre_image_run_process.run_process(cropcommand, True) # try: # if (simulation): # time.sleep(3) # else: # proc = subprocess.Popen(cropcommand.encode('mbcs'), # shell=True, # stdout=subprocess.PIPE, # stdin=subprocess.PIPE, # stderr=subprocess.STDOUT, # universal_newlines=False) # # loglines = [] # for line in iter(proc.stdout.readline, ""): # loglines.append(line) # # proc.wait() # if proc.returncode != 0: # raise OSError # # except OSError: # errorDuringClassif = True # # mbox = QtGui.QMessageBox() # mbox.setWindowTitle(u"Erreur") # mbox.setText(u"Une erreur a été rencontrée lors de la préparation des données vecteurs") # # # has no effect... # #mbox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # #mbox.setSizeGripEnabled(True) # # detailedtext = (u"Code de retour : %s%s" # "Commande : %s%s" # "Sortie standard :%s%s" # % ( proc.returncode, 2*os.linesep, # cropcommand, 2*os.linesep, # os.linesep, u''.join([u'%s%s' % (unicode(c, 'mbcs'), os.linesep) for c in loglines]) ) ) # # mbox.setDetailedText(detailedtext) # mbox.setIcon(QtGui.QMessageBox.Critical) # mbox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) # ret = mbox.exec_() # raise OSError vectorlist += '"%s" ' % (preprocessedshpfile) # Build classifcommand outputlog = os.path.join(outputdir, 'output.log') outputclassification = os.path.join(outputdir, 'classification.tif') outputresults = os.path.join(outputdir, 'classification.resultats.txt') launcher = "%s/classification.bat" % self.app_dir classifcommand = ('%s ' '-io.il %s ' '-io.vd %s ' '-io.out "%s" ' '-io.results "%s"' % (launcher, rasterlist, vectorlist, outputclassification, outputresults)) terre_image_run_process.run_process(classifcommand, True) # Execute commandline # try: # if (simulation): # time.sleep(3) # else: # proc = subprocess.Popen(classifcommand.encode('mbcs'), # shell=True, # stdout=subprocess.PIPE, # stdin=subprocess.PIPE, # stderr=subprocess.STDOUT, # universal_newlines=False) # # loglines = [] # with open(outputlog, "w") as logfile: # for line in iter(proc.stdout.readline, ""): # loglines.append(line) # logfile.writelines(line) # logfile.flush() # os.fsync(logfile.fileno()) # # proc.wait() # if proc.returncode != 0: # raise OSError # # except OSError: # errorDuringClassif = True # # mbox = QtGui.QMessageBox() # mbox.setWindowTitle(u"Erreur") # mbox.setText(u"Une erreur a été rencontrée lors de la classification") # # # has no effect... # #mbox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # #mbox.setSizeGripEnabled(True) # # detailedtext = (u"Code de retour : %s%s" # "Commande : %s%s" # "Sortie standard :%s%s" # % ( proc.returncode, 2*os.linesep, # classifcommand, 2*os.linesep, # os.linesep, u''.join([u'%s%s' % (unicode(c, 'mbcs'), os.linesep) for c in loglines]) ) ) # # mbox.setDetailedText(detailedtext) # mbox.setIcon(QtGui.QMessageBox.Critical) # mbox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) # ret = mbox.exec_() # # saferemovefile(outputlog) # saferemovefile(outputclassification) # saferemovefile(outputresults) if (not simulation and not errorDuringClassif): QGisLayers.loadLabelImage(outputclassification, labeldescriptor) notificationDialog = ConfusionMatrixViewer( selectedvectorlayers, outputresults) self.clearStatus() QtGui.QApplication.restoreOverrideCursor() notificationDialog.setModal(True) notificationDialog.show() pixmap = QtGui.QPixmap(notificationDialog.size()) notificationDialog.render(pixmap) pixmap.save(os.path.join(outputdir, 'resultats.png')) notificationDialog.exec_() # except: # raise finally: self.clearStatus() QtGui.QApplication.restoreOverrideCursor() return # this discards exception
def classify(self): dirDest = QtGui.QFileDialog.getExistingDirectory( None, str( "Répertoire de destination des fichiers de la classification" ), self.output_dir ) if dirDest : self.output_dir = dirDest logger.debug( "classify" ) simulation = False # Get rasters selectedrasterlayers = self.rasterlayerselector.getSelectedOptions() logger.debug( "selectedrasterlayers" + str(selectedrasterlayers) ) if len(selectedrasterlayers) < 1: QtGui.QMessageBox.critical( self, \ u"Erreur", \ u"Aucune couche raster sélectionnée" ) return # Get vectors selectedvectorlayers = self.vectorlayerselector.getSelectedOptions() if len(selectedvectorlayers) < 2: QtGui.QMessageBox.critical( self, \ u"Erreur", \ u"Au minimum deux couches vecteur doivent être sélectionnées" ) return try: errorDuringClassif = False outputdir = self.getOutputDir() # Build list of input raster files rasterlist = "" for r in selectedrasterlayers: rasterlist += '"%s" ' % (r.source()) #logger.debug( "rasterlist" + str( rasterlist ) ) #firstraster = '"%s" ' % (unicode(selectedrasterlayers[0].source())) logger.debug( "selectedrasterlayers[0]" + str(selectedrasterlayers[0]) ) firstraster = '"%s" ' % (selectedrasterlayers[0].source()) # Build list of input vector files vectorlist = "" labeldescriptor = {} label = 0 for i in range(len(selectedvectorlayers)): v = selectedvectorlayers[i] inputshpfilepath = v[0].source() classcolor = v[1] classlabel = v[2] labeldescriptor[label] = (classcolor, classlabel) logger.debug( "labeldescriptor" + str(labeldescriptor) ) label += 1 # Reprocess input shp file to crop it to firstraster extent vectordir = os.path.join(outputdir, 'class%s' % (str(i))) ensure_clean_dir(vectordir) #preprocessedshpfile = os.path.join(vectordir, transform_spaces(os.path.basename(unicode(inputshpfilepath)))) preprocessedshpfile = os.path.join(vectordir, "preprocessed.shp") cropcommand = ('%s/cropvectortoimage.bat ' '"%s" ' '"%s" ' '"%s" ' '"%s" ' '"%s" ' % ( self.app_dir, inputshpfilepath, selectedrasterlayers[0].source(), os.path.join(vectordir,"imageenveloppe.shp"), os.path.join(vectordir,"tmp_reprojected.shp"), preprocessedshpfile) ) terre_image_run_process.run_process(cropcommand, True) # try: # if (simulation): # time.sleep(3) # else: # proc = subprocess.Popen(cropcommand.encode('mbcs'), # shell=True, # stdout=subprocess.PIPE, # stdin=subprocess.PIPE, # stderr=subprocess.STDOUT, # universal_newlines=False) # # loglines = [] # for line in iter(proc.stdout.readline, ""): # loglines.append(line) # # proc.wait() # if proc.returncode != 0: # raise OSError # # except OSError: # errorDuringClassif = True # # mbox = QtGui.QMessageBox() # mbox.setWindowTitle(u"Erreur") # mbox.setText(u"Une erreur a été rencontrée lors de la préparation des données vecteurs") # # # has no effect... # #mbox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # #mbox.setSizeGripEnabled(True) # # detailedtext = (u"Code de retour : %s%s" # "Commande : %s%s" # "Sortie standard :%s%s" # % ( proc.returncode, 2*os.linesep, # cropcommand, 2*os.linesep, # os.linesep, u''.join([u'%s%s' % (unicode(c, 'mbcs'), os.linesep) for c in loglines]) ) ) # # mbox.setDetailedText(detailedtext) # mbox.setIcon(QtGui.QMessageBox.Critical) # mbox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) # ret = mbox.exec_() # raise OSError vectorlist += '"%s" ' % (preprocessedshpfile) # Build classifcommand outputlog = os.path.join(outputdir, 'output.log') outputclassification = os.path.join(outputdir, 'classification.tif') outputresults = os.path.join(outputdir, 'classification.resultats.txt') launcher = "%s/classification.bat" % self.app_dir classifcommand = ( '%s ' '-io.il %s ' '-io.vd %s ' '-io.out "%s" ' '-io.results "%s"' % (launcher, rasterlist, vectorlist, outputclassification, outputresults) ) terre_image_run_process.run_process(classifcommand, True) # Execute commandline # try: # if (simulation): # time.sleep(3) # else: # proc = subprocess.Popen(classifcommand.encode('mbcs'), # shell=True, # stdout=subprocess.PIPE, # stdin=subprocess.PIPE, # stderr=subprocess.STDOUT, # universal_newlines=False) # # loglines = [] # with open(outputlog, "w") as logfile: # for line in iter(proc.stdout.readline, ""): # loglines.append(line) # logfile.writelines(line) # logfile.flush() # os.fsync(logfile.fileno()) # # proc.wait() # if proc.returncode != 0: # raise OSError # # except OSError: # errorDuringClassif = True # # mbox = QtGui.QMessageBox() # mbox.setWindowTitle(u"Erreur") # mbox.setText(u"Une erreur a été rencontrée lors de la classification") # # # has no effect... # #mbox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # #mbox.setSizeGripEnabled(True) # # detailedtext = (u"Code de retour : %s%s" # "Commande : %s%s" # "Sortie standard :%s%s" # % ( proc.returncode, 2*os.linesep, # classifcommand, 2*os.linesep, # os.linesep, u''.join([u'%s%s' % (unicode(c, 'mbcs'), os.linesep) for c in loglines]) ) ) # # mbox.setDetailedText(detailedtext) # mbox.setIcon(QtGui.QMessageBox.Critical) # mbox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) # ret = mbox.exec_() # # saferemovefile(outputlog) # saferemovefile(outputclassification) # saferemovefile(outputresults) if (not simulation and not errorDuringClassif): QGisLayers.loadLabelImage(outputclassification, labeldescriptor) notificationDialog = ConfusionMatrixViewer(selectedvectorlayers, outputresults) self.clearStatus() QtGui.QApplication.restoreOverrideCursor() notificationDialog.setModal(True) notificationDialog.show() pixmap = QtGui.QPixmap(notificationDialog.size()) notificationDialog.render(pixmap) pixmap.save(os.path.join(outputdir, 'resultats.png')) notificationDialog.exec_() # except: # raise finally: self.clearStatus() QtGui.QApplication.restoreOverrideCursor() return # this discards exception