def createNewFDVT(self):
        fn = QtGui.QFileDialog.getOpenFileName(self, "Select FDVT", '.',
                                               '*.csv')
        anno = A.Annotation()
        anno.loadFromFile(fn[0])
        # filterTuples = anno.extractAllFilterTuples()

        annotationSelector = SD.AnnotationSelector()
        annotationSelector.setAnnotationFile(fn[0])
        OD.OverlayDialogWidgetBase.getUserInput(self, annotationSelector)

        # annotationFilters = AS.AnnotationSelecter.getAnnotationSelection(self,
        #                                                                  anno)

        annotationSelection = annotationSelector.getUserSelection()
        annotationFilters = []
        colors = []
        for a, b, c in annotationSelection:
            annotationFilters += [A.AnnotationFilter(None, [a], [b])]
            colors += [c]

        filteredAnno = anno.filterFrameListMultiple(annotationFilters,
                                                    exactMatch=False)

        import tempfile
        tmpDir = tempfile.mkdtemp()

        fdvt = FDV.FrameDataVisualizationTreeBehaviour(tmpDir)
        fdvt.importAnnotation(filteredAnno, annoFilters=annotationFilters)

        self.addFDVT(fdvt, colors)

        print annotationFilters
 def setAnnotationFile(self, filename):
     try:
         self.anno = A.Annotation()
         self.anno.loadFromFile(filename)
         self.scanLabelFile()
     except IOError:
         self.anno = None
    def loadAnnotation(self, lst):
        from os.path import isfile

        videoLength = lst[0]

        self.loading = True

        f = self.bhvrPath
        if isfile(f):
            cfg.log.debug("AnnotationLoader: f exists create empty Annotation")
            out = Annotation.Annotation()
            cfg.log.debug(
                "AnnotationLoader: created Annotation. try to load..")
            try:
                out.loadFromFile(f)
                cfg.log.info(
                    "AnnotationLoader: loaded Annotation {0} {1}".format(
                        self.bhvrPath, self))
            except:
                cfg.log.warning("load annotation of " + f +
                                " failed, reset annotaions")
                if videoLength is None:
                    videoLength = self.retrieveVideoLength(self.bhvrPath)
                out = Annotation.Annotation(
                    frameNo=videoLength)  #, vialNames=self.vialNames)
        else:
            cfg.log.warning(
                "AnnotationLoader: f does NOT exist create empty Annotation at {0}"
                .format(self.bhvrPath))
            if videoLength is None:
                videoLength = self.retrieveVideoLength(self.videoPath)

            out = Annotation.Annotation(
                frameNo=videoLength)  #, vialNames=self.vialNames)
            cfg.log.info("new annotation with length {0}".format(videoLength))

        if out is None:
            cfg.log.warning("annotation is None. Should never, ever happen!")

        self.annotation = out
        #         self.loadedAnnotation.emit([self, self.path])

        cfg.log.debug("finished loading annotation {0}".format(self.bhvrPath))
        self.loadedAnnotation.emit([self.key, self.annotation.getLength()])
        self.loading = False
    def extractFeatures(self):
        """
        Iteration over video file to extract features
        :return:
        """
        if len(self.videoListRel) == 1:
            progressSteps = VE.videoExplorer.retrieveVideoLength(
                self.rel2absFile(self.videoListRel[0]))
        else:
            progressSteps = len(self.videoListRel)

        self.setStatus("extracting features..", progressSteps)
        for i, relVidFile in enumerate(self.videoListRel):
            if os.path.exists(self.generateColorFeatureSavePath(relVidFile))\
            and os.path.exists(self.generateDiffImgFeatureSavePath(relVidFile)):
                # do not recompute features already computed
                if len(self.videoListRel) > 1:
                    self.incrementStatus()

                continue

            colorFeat = []
            diffImgFeat = []
            vE = VE.videoExplorer()
            vE.setVideoStream(self.rel2absFile(relVidFile), frameMode='RGB')

            anno = A.Annotation()
            anno.loadFromFile(
                self.rel2absFile(self.convertVideoToAnnoFilename(relVidFile)))

            annotationList = anno.filterFrameLists(self.annotationFilters)
            prevFrame = None
            for frame in vE:
                colorFeat += [
                    self.extractColorFeatureFromFrame(frame, annotationList[i])
                ]

                if prevFrame:
                    diffImgFeat += [
                        self.extractDiffImgFeatureFromFrames(
                            prevFrame, frame, annotationList[i])
                    ]
                else:
                    diffImgFeat += [np.zeros((16, ), dtype=np.int32)]

                if len(self.videoListRel) == 1:
                    self.incrementStatus()

            np.save(self.generateColorFeatureSavePath(relVidFile),
                    np.asarray(colorFeat))

            np.save(self.generateDiffImgFeatureSavePath(relVidFile),
                    np.asarray(diffImgFeat))

            if len(self.videoListRel) > 1:
                self.incrementStatus()
    def __init__(self):
        super(Test, self).__init__()

        self.annoSelector = AnnotationSelecter(self)

        self.setCentralWidget(self.annoSelector)
        self.show()

        import pyTools.videoProc.annotation as A

        a = A.Annotation()
        a.loadFromFile('/media/peter/Seagate Backup Plus Drive1/peter_testCopy/WP609L_small.bhvr')
        self.annoSelector.setAnnotation(a)
    def createAnnotationArrays(self, annoFileList):
        maxClass = len(self.annotationFilters)
        annotationArrays = dict()
        for annoFile in sorted(annoFileList):
            if os.path.exists(self.rel2absFile(annoFile)):
                anno = A.Annotation()
                anno.loadFromFile(self.rel2absFile(annoFile))
                for i, annoFilter in enumerate(self.annotationFilters):
                    filtAnno = anno.filterFrameListBool(annoFilter)
                    if i == 0:
                        annotationArrays[annoFile] = np.zeros(
                            (filtAnno.shape[0], maxClass), dtype=np.bool)
                    annotationArrays[annoFile][:, i] = filtAnno

        return annotationArrays
    def classify(self):
        """
        Applies the classifier on all data
        :return:
        """
        print("classifying!")
        if len(self.videoListRel) == 1:
            progressSteps = VE.videoExplorer.retrieveVideoLength(
                self.rel2absFile(self.videoListRel[0]), 100000)
            # self.fdvt = FDV.FrameDataVisualizationTreeBehaviour()
            # self.fdvt.meta['singleFileMode'] = True
        else:
            progressSteps = len(self.videoListRel)
            # self.fdvt = FDV.FrameDataVisualizationTreeBehaviour()
            # self.fdvt.meta['singleFileMode'] = False

        self.setStatus("classifying...", progressSteps)

        filters = []

        # filt = A.AnnotationFilter(annotators=["simpleFlyShowCase"],
        #                           behaviours=['negative'],
        #                           vials=self.annotationFilters[0].vials)
        # self.fdvt.addNewClass(filt)
        # filters += [filt]

        clfy_res = []

        for af in self.annotationFilters:
            filt = A.AnnotationFilter(annotators=["SimpleHistogramPlugin"],
                                      behaviours=af.behaviours,
                                      vials=af.vials)
            # self.fdvt.addNewClass(filt)
            filters += [filt]

            clfy_res += [{'frames': [], 'meta': {}}]

        for i, videoPathRel in enumerate(self.videoListRel):
            feat = np.load(self.generateFeatureSavePath(videoPathRel))
            dv = []  # deltaVector
            for k in range(0, feat.shape[0], 100):
                f = feat[k:k + 100]
                prediction = self.classifier.predict(f)
                for j, p in enumerate(prediction):
                    # p-1 because tehre is no negative class!!!
                    # dv += [self.fdvt.getDeltaValue(videoPathRel, k + j,
                    #                            filters[int(p-1)])]

                    # UPDATE ANNOTATIONS
                    clfy_res[p]['frames'] += [k + j]
                    clfy_res[p]['meta'][k + j] = {
                        "confidence": 1,
                        "boundingBox": [None, None, None, None]
                    }

                if len(self.videoListRel) == 1:
                    self.incrementStatus(100)

            # SAVE RESULT IN ANNOTATIONS
            resAnno = A.Annotation(frameNo=feat.shape[0])
            for i, af in enumerate(self.annotationFilters):
                annotator = af.annotators[0]
                label = af.behaviours[0]

                if clfy_res[i]['frames']:
                    resAnno.addAnnotation(vial=None,
                                          frames=clfy_res[i]['frames'],
                                          annotator='SimpleHistogramPlugin',
                                          behaviour=label,
                                          metadata=clfy_res[i]['meta'])

            # self.fdvt.insertDeltaVector(dv)

            resAnno.saveToFile(
                self.generateFeatureSavePath(videoPathRel) + 'hist.csv')

            if len(self.videoListRel) > 1:
                self.incrementStatus()