Ejemplo n.º 1
0
 def _load_classifier(self, path):
     learner = None
     try:
         learner = BaseLearner(path, None, None)
     except:
         exception(self, 'Error on loading classifier')
     else:
         state = learner.state
         if state['has_definition']:
             learner.loadDefinition()
     return learner
Ejemplo n.º 2
0
 def _load_classifier(self, path):
     learner = None
     try:
         learner = BaseLearner(path, None, None)
     except:
         exception(self, 'Error on loading classifier')
     else:
         state = learner.state
         if state['has_definition']:
             learner.loadDefinition()
     return learner
Ejemplo n.º 3
0
    def _load_classifier(self, path):
        learner = None
        try:
            learner = BaseLearner(path, None, None)
        except:
            exception(self, 'Error on loading classifier')
        else:
            result = learner.check()
            #if result['has_arff']:
            #    self._learner.importFromArff()

            if result['has_definition']:
                learner.loadDefinition()
        return learner
Ejemplo n.º 4
0
 def _init_new_classifier(self):
     learner = BaseLearner(None, None, None)
     self._current_class = None
     self._class_sbox.setValue(1)
     self._class_text.setText('class1')
     self._class_color_btn.set_color(QColor('red'))
     class_table = self._class_table
     class_table.clearContents()
     class_table.setRowCount(0)
     ann_table = self._ann_table
     ann_table.clearContents()
     ann_table.setRowCount(0)
     if self._detect_objects:
         self._activate_objects_for_image(False, clear=True)
     self._annotations.remove_all()
     return learner
Ejemplo n.º 5
0
    def _loadInfos(self):
        print "LOAD"
        if not self.isInitialized:
            self._oLearner = BaseLearner(strEnvPath=self.path)
            try:
                self._oLearner.importFromArff()
                try:
                    self._oLearner.importSampleNames()
                except IOError:
                    has_samples = False
                else:
                    has_samples = True
                #print has_samples
            except IOError:
                pass
            else:
                print "import %s" % self.name

                for iLabel in self._oLearner.lstClassLabels:
                    strName = self._oLearner.dctClassNames[iLabel]
                    strHexColor = self._oLearner.dctHexColors[strName]
                    if strName in self._oLearner.dctFeatureData:
                        oClass = Class(strName, iLabel,
                                       len(self._oLearner.dctFeatureData[strName]),
                                       hexToFlexColor(strHexColor))
                        oClass.oClassifier = self
                        dctFeatures = {}
                        for iIdx, strFeatureName in enumerate(self._oLearner.lstFeatureNames):
                            dctFeatures[strFeatureName] = list(self._oLearner.dctFeatureData[strName][:,iIdx])
                        oClass.features = dctFeatures
                        if has_samples:
                            oClass.sample_names = [Sample(os.path.join(self._oLearner.dctEnvPaths['samples'], strName, filename))
                                                   for filename in self._oLearner.dctSampleNames[strName]]
                            #print strName,self._oLearner.dctSampleNames[strName]
                        self.dctClassInfos[strName] = oClass
                self.isInitialized = True

                for strFeatureName in self._oLearner.lstFeatureNames:
                    self.dctFeatureInfos[strFeatureName] = Feature(strFeatureName)
Ejemplo n.º 6
0
class Classifier(object):

    def __init__(self, name=None, path=None):
        #super(Classifier, self).__init__()
        self.name = name
        self.path = path

        self.dctClassInfos = OrderedDict()
        self.dctFeatureInfos = OrderedDict()
        self.isInitialized = False
        #self._loadInfos()

    def _loadInfos(self):
        print "LOAD"
        if not self.isInitialized:
            self._oLearner = BaseLearner(strEnvPath=self.path)
            try:
                self._oLearner.importFromArff()
                try:
                    self._oLearner.importSampleNames()
                except IOError:
                    has_samples = False
                else:
                    has_samples = True
                #print has_samples
            except IOError:
                pass
            else:
                print "import %s" % self.name

                for iLabel in self._oLearner.lstClassLabels:
                    strName = self._oLearner.dctClassNames[iLabel]
                    strHexColor = self._oLearner.dctHexColors[strName]
                    if strName in self._oLearner.dctFeatureData:
                        oClass = Class(strName, iLabel,
                                       len(self._oLearner.dctFeatureData[strName]),
                                       hexToFlexColor(strHexColor))
                        oClass.oClassifier = self
                        dctFeatures = {}
                        for iIdx, strFeatureName in enumerate(self._oLearner.lstFeatureNames):
                            dctFeatures[strFeatureName] = list(self._oLearner.dctFeatureData[strName][:,iIdx])
                        oClass.features = dctFeatures
                        if has_samples:
                            oClass.sample_names = [Sample(os.path.join(self._oLearner.dctEnvPaths['samples'], strName, filename))
                                                   for filename in self._oLearner.dctSampleNames[strName]]
                            #print strName,self._oLearner.dctSampleNames[strName]
                        self.dctClassInfos[strName] = oClass
                self.isInitialized = True

                for strFeatureName in self._oLearner.lstFeatureNames:
                    self.dctFeatureInfos[strFeatureName] = Feature(strFeatureName)

    @property
    def classes(self):
        self._loadInfos()
        return self.dctClassInfos.values()

    @property
    def classInfos(self):
        self._loadInfos()
        logging.debug("classes: %s" % len(self.dctClassInfos.values()))
        return self.dctClassInfos.values()

    @property
    def featureInfos(self):
        self._loadInfos()
        logging.debug("features: %s" % len(self.dctFeatureInfos.values()))
        return self.dctFeatureInfos.values()

    def getSamples(self, className):
        self._loadInfos()
        strPathSamples = os.path.join(self._oLearner.dctEnvPaths['samples'],
                                      className)
        lstResults = []
        if os.path.isdir(strPathSamples):
            #print len(self._oLearner.lstFeatureNames), len()
            dctImagePairs = OrderedDict()
            for strName, oMatch in collect_files_by_regex(strPathSamples, '(?P<prefix>.+?)__(?P<type>(img)|(msk)).+?', ['.png', '.jpg']):
                strPrefix = oMatch.group('prefix')
                strType = oMatch.group('type')
                if not strPrefix in dctImagePairs:
                    dctImagePairs[strPrefix] = {}
                dctImagePairs[strPrefix][strType] = strName

            iIdx = 0
            for dctPair in dctImagePairs.values():
                #oContainer = ccore.SingleObjectContainer(dctPair['img'], dctPair['msk'])
                #strCoords = ",".join(map(str,flatten(oContainer.getCrackCoordinates(1))))
                #print dctPair['img'], dctPair['msk']
                #dctFeatures = {}
                #for iF, strFeatureName in enumerate(self._oLearner.lstFeatureNames):
                #    dctFeatures[strFeatureName] = self._oLearner.dctFeatureData[className][iIdx][iF]
                oSample = Sample(dctPair['img'])
                lstResults.append(oSample)
                iIdx += 1
                #break
        return lstResults

    def getFeatureData(self, featureNames):
        self._loadInfos()
        lstFeatureData = []
        if len(featureNames) == 1:
            strFeatureName = featureNames[0]
            for strClassName, oClass in self.dctClassInfos.iteritems():
                aY, aX = numpy.histogram(oClass.features[strFeatureName], normed=True)
                lstData = [dict(x=fX, y=fY) for fX,fY in zip(aX, aY)]
                lstFeatureData.append(lstData)
        elif len(featureNames) == 2:
            for strClassName, oClass in self.dctClassInfos.iteritems():
                iSize = len(oClass.features.values()[0])
                lstData = [dict([(strFeatureName, oClass.features[strFeatureName][iIdx])
                                 for strFeatureName in featureNames])
                           for iIdx in range(iSize)]
                lstFeatureData.append(lstData)
        #print lstFeatureData
        return lstFeatureData