def _trackFrame(self, frame, requestedColor='r', requestedOutput='raw'):
     """
     Get the position of the mouse in frame and append to self.positions
     Returns the mask of the current frame with the mouse potentially drawn
     
     :param frame: The video frame to use.
     :type: video_frame.Frame
     :param str requestedColor: A character (for list of supported charaters see ObjectContour) idicating the color to draw the contour
     :param str requestedOutput: Which frame type to output (one of ['raw', 'mask', 'diff'])
     :returns: silhouette
     :rtype: binary mask or None
     """
     treatedFrame = frame.gray()
     fast = self.fast
     if not isPi and not fast:
         treatedFrame = treatedFrame.denoise().blur()
     silhouette, diff = self._getSilhouette(treatedFrame)
     
     biggestContour = self._getBiggestContour(silhouette)
     
     plotSilhouette = None
     if fast:
         requestedOutput = 'mask'
     self.positions.append(self.defaultPos)
     if biggestContour is not None:
         area = cv2.contourArea(biggestContour)
         if self.minArea < area < self.maxArea:
             if self.plot:
                 if requestedOutput == 'raw':
                     plotSilhouette = (frame.color()).copy()
                     color = requestedColor
                 elif requestedOutput == 'mask':
                     plotSilhouette = silhouette.copy()
                     color = 'w'
                 elif requestedOutput == 'diff':
                     plotSilhouette = (diff.color()).copy()
                     color = requestedColor
                 else:
                     raise NotImplementedError("Expected one of ['raw', 'mask', 'diff'] for requestedOutput, got: {}".format(requestedOutput))
             else:
                 color = 'w'
             mouse = ObjectContour(biggestContour, plotSilhouette, contourType='raw', color=color)
             if plotSilhouette is not None:
                 mouse.draw()
             self.positions[-1] = mouse.centre
         else:
             if area > self.maxArea:
                 if not fast:
                     print('Frame: {}, found something too big in the arena ({} > {})'.format(
                     self._stream.currentFrameIdx, area, self.maxArea))
             else:
                 if not fast:
                     print('Frame: {}, biggest structure too small ({} < {})'.format(
                     self._stream.currentFrameIdx, area, self.minArea))
             return None
     else:
         print('Frame {}, no contour found'.format(self._stream.currentFrameIdx))
         return None
     self._checkTeleportation(frame, silhouette)
     return plotSilhouette if plotSilhouette is not None else silhouette
 def paint(self, frame, roiColor='y', arenaColor='m'):
     if self.roi is not None:
         roiContour = ObjectContour(self.roi.points, frame, contourType='raw', color=roiColor, lineThickness=2)
         roiContour.draw()
     if self.extractArena:
         arenaContour = ObjectContour(self.arena.points, frame, contourType='raw', color=arenaColor, lineThickness=2)
         arenaContour.draw()
Example #3
0
 def paint(self, frame, roiColor='y', arenaColor='m'):
     if self.roi is not None:
         roiContour = ObjectContour(self.roi.points,
                                    frame,
                                    contourType='raw',
                                    color=roiColor,
                                    lineThickness=2)
         roiContour.draw()
     if self.extractArena:
         arenaContour = ObjectContour(self.arena.points,
                                      frame,
                                      contourType='raw',
                                      color=arenaColor,
                                      lineThickness=2)
         arenaContour.draw()
Example #4
0
    def _trackFrame(self, frame, requestedColor='r', requestedOutput='raw'):
        """
        Get the position of the mouse in frame and append to self.positions
        Returns the mask of the current frame with the mouse potentially drawn
        
        :param frame: The video frame to use.
        :type: video_frame.Frame
        :param str requestedColor: A character (for list of supported charaters see ObjectContour) idicating the color to draw the contour
        :param str requestedOutput: Which frame type to output (one of ['raw', 'mask', 'diff'])
        :returns: silhouette
        :rtype: binary mask or None
        """
        treatedFrame = frame.gray()
        fast = self.fast
        if not isPi and not fast:
            treatedFrame = treatedFrame.denoise().blur()
        silhouette, diff = self._getSilhouette(treatedFrame)

        biggestContour = self._getBiggestContour(silhouette)

        plotSilhouette = None
        if fast:
            requestedOutput = 'mask'
        self.positions.append(self.defaultPos)
        if biggestContour is not None:
            area = cv2.contourArea(biggestContour)
            if self.minArea < area < self.maxArea:
                if self.plot:
                    if requestedOutput == 'raw':
                        plotSilhouette = (frame.color()).copy()
                        color = requestedColor
                    elif requestedOutput == 'mask':
                        plotSilhouette = silhouette.copy()
                        color = 'w'
                    elif requestedOutput == 'diff':
                        plotSilhouette = (diff.color()).copy()
                        color = requestedColor
                    else:
                        raise NotImplementedError(
                            "Expected one of ['raw', 'mask', 'diff'] for requestedOutput, got: {}"
                            .format(requestedOutput))
                else:
                    color = 'w'
                mouse = ObjectContour(biggestContour,
                                      plotSilhouette,
                                      contourType='raw',
                                      color=color)
                if plotSilhouette is not None:
                    mouse.draw()
                self.positions[-1] = mouse.centre
            else:
                if area > self.maxArea:
                    if not fast:
                        print((
                            'Frame: {}, found something too big in the arena ({} > {})'
                            .format(self._stream.currentFrameIdx, area,
                                    self.maxArea)))
                else:
                    if not fast:
                        print(
                            ('Frame: {}, biggest structure too small ({} < {})'
                             .format(self._stream.currentFrameIdx, area,
                                     self.minArea)))
                return None
        else:
            print(('Frame {}, no contour found'.format(
                self._stream.currentFrameIdx)))
            return None
        self._checkTeleportation(frame, silhouette)
        return plotSilhouette if plotSilhouette is not None else silhouette