예제 #1
0
    def startMainLoop(self):
        verbose = False
        self.video = VideoWindow(WINDOW_TITLE)

        ### CONFIGURATION ###
        # Extract init template and threshold
        initTemplate = self.__extractTemplate(INIT_TEXT)
        if initTemplate == None:
            return

        # Extract start template and threshold
        startTemplate = self.__extractTemplate(START_TEXT)
        if startTemplate == None:
            return

        initSearcherParams = []
        #initSearcherParams.append(InitTemplateSearcher.INIT_MATCHER_SHAPE)
        initSearcherParams.append(InitTemplateSearcher.INIT_MATCHER_COLOR)
        initSearcher = InitTemplateSearcher(initTemplate, initSearcherParams)
        hsvInit = cv2.cvtColor(initTemplate, cv2.COLOR_BGR2HSV)
        skinSearcher = SkinDetection(hsvInit, showSkin=True)

        startSearcherParams = []
        startSearcherParams.append(StartTemplateSearcher.INIT_MATCHER_COLOR)
        startSearcher = StartTemplateSearcher(startTemplate, startTemplate)
        ### START MAIN LOOP ###
        initBackgroundprob = 0
        startBackgroundprob = 0
        started = False
        trajectoryID = 0
        trajectoryStartTime = 0
        initThreshold = 0
        startThreshold = 0
        while True:
            img = self.__getFrame__()

            initProb, initLoc = initSearcher.matchInitTemplate(img, verbose)

            hsvImg = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            skinResult = skinSearcher.simpleMatch(hsvImg,
                                                  wholePictureGiven=True,
                                                  loc=initLoc,
                                                  showSkin=True)
            #            if initBackgroundprob == 0:
            #               print 'background not set'
            #            elif initProb > initBackgroundprob*3:
            #                print 'background prob:\t'+str(initBackgroundprob)
            #                print 'initprob:\t'+str(initProb)

            if initThreshold == 0:
                a = 1
                #print 'init not calibrated'
            elif (initProb >= initThreshold * 0.9) and skinResult:
                print 'init prob:\t' + str(initProb)
                print 'init thres:\t' + str(initThreshold)

                common.playSound('beep-10.wav')
                # not started yet, get it started
                if started == False:
                    started = True
                    trajectoryID = 1
                    trajectoryStartTime = time.time()
                    self.__skipFrames__(5)
                    img = self.__getFrame__()

                else:
                    started = False
                    trajectoryStartTime = time.time()
                    startProb, startLoc = startSearcher.matchInitTemplate(
                        img, verbose)
                    datapoint = TrajectoryPoint(
                        -2, startLoc,
                        time.time() - trajectoryStartTime, startProb)
                    self.trajectoryQueue.pushDataPoint(datapoint)
                    self.__skipFrames__(5)
                    img = self.__getFrame__()

            if started == True:
                startProb, startLoc = startSearcher.matchInitTemplate(
                    img, verbose)
                if startProb >= startThreshold * 0.9:
                    self.video.drawTextonImg(img, startLoc, str(startProb))
                    cv2.rectangle(img, startLoc,
                                  (startLoc[0] + startTemplate.shape[1],
                                   startLoc[1] + startTemplate.shape[0]),
                                  (0, 255, 0))
                    datapoint = TrajectoryPoint(
                        trajectoryID, startLoc,
                        time.time() - trajectoryStartTime, startProb)
                    self.trajectoryQueue.pushDataPoint(datapoint)

            self.video.drawTextonImg(
                img, initLoc, 'skin Match: ' + str(skinResult) +
                ' templateMATCH: ' + str(initProb))
            cv2.rectangle(img, initLoc, (initLoc[0] + initTemplate.shape[1],
                                         initLoc[1] + initTemplate.shape[0]),
                          (255, 255, 0))

            self.video.showImg(img)
            key = cv2.waitKey(1)
            key = key % 256
            # init = i = 105
            # start = s = 115
            # background = space = 32
            # print = p = 112

            # escape button pressed
            if key == 27:
                self.video.destroyWindow()
                self.capture.release()
                return -1
            # print images
            if key == 112:
                self.video.writeImg(img, "image.jpg")
                self.video.writeImg(initTemplate, "inittemplate.jpg")
                self.video.writeImg(startTemplate, "starttemplate.jpg")
            # calibrate init
            if key == 105:
                initThreshold = self.__extractForeground(
                    initTemplate, initSearcher, INIT_CALIBRATE)
                print 'init threshold:\t' + str(initThreshold)
                self.__skipFrames__(15, APPLICATION_STARTED)
            # calibrate start
            if key == 115:
                startThreshold = self.__extractForeground(
                    startTemplate, startSearcher, START_CALIBRATE)
                print 'start threshold:\t' + str(startThreshold)
                self.__skipFrames__(15)