Example #1
0
 def run(self) -> Optional["GameState"]:
     audio_thread = None
     if self.music_file is not None:
         audio_thread = get_audio_thread(self.music_file, self.loop)
     if not isinstance(self.cam, SubscriberWindows):
         self.cam = read_updates(
             self.cam,
             size=(-1, -1),
         )
     started = False
     f_prev = None
     for f in self.cam:
         if f and f_prev is not next(iter(f.values()))[0]:
             vid: np.ndarray = next(iter(f.values()))[0]
             if not started:
                 if audio_thread is not None:
                     audio_thread.start()
                 self.on_start()
                 started = True
             if self.next_state is not None:
                 audio_thread.terminate()
                 return self.next_state
             self.video_shape = vid.shape
             vid = self.preprocess_video(vid)
             a = [self.on_video(vid)]
             self.cam.display_frames(a)
             f_prev = vid
             audio_thread.join(0)
             if not audio_thread.is_alive():
                 self.next_state = self.on_end()
                 audio_thread.terminate()
                 return self.next_state
     audio_thread.terminate()
Example #2
0
    def at_start(self) -> None:
        self._vs = read_updates(self.cam_index)

        frame, self.can_track = self._try_get_frame()

        if not self.can_track:
            # self._vs.release()
            self._vs.end()
            self._vs = None
            raise RuntimeError("invalid video source")

        self.frame_height, self.frame_width, _ = frame.shape
Example #3
0
    def __init__(
        self, cam_index=0, *, focal_length_px=490, ball_radius_cm=2, color_masks={},
    ):
        """
        Create a blob tracker.

        example usage:
        >>> tracker = BlobTracker(color_masks={
        ...         'tracker1':{
        ...             'h':(98, 10),
        ...             's':(200, 55),
        ...             'v':(250, 32)
        ...         },
        ... })

        >>> with tracker:
        ...     for _ in range(3):
        ...         poses = tracker.get_poses() # gets latest poses for the trackers
        ...         print (poses)
        ...         time.sleep(1/60) # some delay, doesn't affect the tracking
        {'tracker1': {'x': 0, 'y': 0, 'z': 0}}
        {'tracker1': {'x': 0, 'y': 0, 'z': 0}}
        {'tracker1': {'x': 0, 'y': 0, 'z': 0}}

        :param cam_index: index of the camera that will be used to track the blobs
        :param focal_length_px: focal length in pixels
        :param ball_radius_cm: the radius of the ball to be tracked in cm
        :param color_masks: color mask parameters, in opencv hsv color space, for color detection
        """
        super().__init__()
        self._vs = read_updates(cam_index)

        time.sleep(2)

        self.camera_focal_length = focal_length_px
        self.BALL_RADIUS_CM = ball_radius_cm
        self.cam_index = cam_index

        self.last_frame = None
        self.time_of_last_frame = -1

        frame, self.can_track = self._try_get_frame()

        if not self.can_track:
            self._vs.end()
            self._vs = None
            raise RuntimeError('invalid video source')


        self.frame_height, self.frame_width, _ = frame.shape if self.can_track else (0, 0, 0)

        self.markerMasks = color_masks

        self.poses = {}
        self.blobs = {}

        self.kalmanTrainSize = 15

        self.kalmanFilterz: Dict[str, LazyKalman] = {}
        self.kalmanWasInited = {}

        self.kalmanFilterz2: Dict[str, LazyKalman] = {}
        self.kalmanWasInited2 = {}

        for i in self.markerMasks:
            self.poses[i] = {"x": 0, "y": 0, "z": 0}
            self.blobs[i] = None

            self.kalmanFilterz[i] = None
            self.kalmanWasInited[i] = False

            self.kalmanFilterz2[i] = None
            self.kalmanWasInited2[i] = False

        # -------------------threading related------------------------

        self.alive = True
        self._lock = threading.Lock()
        self.daemon = False
        self.pose_que = queue.Queue()
Example #4
0
from displayarray import read_updates

with read_updates(0) as a, read_updates(0) as b:
    for i in range(1000):
        a.update()
        b.update()
        try:
            print(a.frames == b.frames)
        except ValueError:
            print(f"frame comparison: {(a.frames['0'][0] == b.frames['0'][0]).all()}")
def test_mocks_read():
    with MockReadUpdates(0, [rgby_tracking_image_np]):
        a = read_updates()
        assert isinstance(a, mock.MagicMock)
        a.update()
        assert (a.frames["0"][0] == rgby_tracking_image_np).all()
from displayarray import read_updates

for f in read_updates(0, size=(1, 1)):
    if f:
        print(f[0].shape)
        break