Ejemplo n.º 1
0
def caer_event_from_row(row):
    '''
    Takes binary dvs data as input,
    returns unpacked event data or False if event type does not exist.
    '''
    sys_ts, head, body = (v.tobytes() for v in row)
    if not sys_ts:
        # rows with 0 timestamp do not contain any data
        return 0, False
    d = unpack_header(head)
    d['dvs_data'] = body
    return int(sys_ts) * 1e-6, unpack_data(d)
Ejemplo n.º 2
0
    def readPacket(self, number):
        """
        Reads packet k in the dataset
        Parameters
        ----------
        number: number of packet, in range(0,numPackets)

        Returns
        -------
        packet of data, or False if packet is outside of range or cannot be extracted
        """
        if number >= self.numPackets or number < 0: return False
        dat = self.davisData[number]
        headerDat = dat[1]  # caer header
        if headerDat.shape[0] == 0:
            return False  # empty packet, can happen at end of recording
        packet = {'dvs_header': dat[1]}  # put it to the dict as header
        packet.update(caer.unpack_header(
            packet['dvs_header']))  # update the dict?
        # dat0 = dat[0]  # timestamp of the packet?
        packet['dvs_data'] = dat[
            2]  # put the data payload, dvs_data refers to DAVIS camera data, can be frames or IMU data too
        packet = caer.unpack_data(
            packet
        )  # use caer to unpack it, store it back to data, which gets timestamp and cooked data
        packet.timestamp = packet['timestamp']
        packet.etype = packet['etype']

        # # print some info
        # if data:  # if could not unpack, is False
        #     print('packet #' + str(k)
        #           + ' timestamp: ' + str(data['timestamp'])
        #           + ' etype: ' + str(data['etype'])
        #           + ' esize: ' + str(data['esize'])
        #           + ' enumber: ' + str(data['enumber'])
        #           )
        return packet
Ejemplo n.º 3
0
    pbar = get_progress_bar()
    sys_ts, t_pre, t_offset, ev_count, pbar_next = 0, 0, 0, 0, 0
    count = 0
    while m.has_data and sys_ts <= tstop * 1e-6 and count <= 10:
        try:
            sys_ts, d = m.get()
        except Empty:
            # wait for queue to fill up
            time.sleep(0.01)
            continue
        if not d:
            # skip unused data
            continue
        if d['etype'] == 'special_event':
            unpack_data(d)
            if any(d['data'] == 0):  # this is a timestamp reset
                print('ts reset detected, setting offset',
                      current_row['timestamp'])
                t_offset += current_row['timestamp']
                #NOTE the timestamp of this special event is not meaningful
                continue
        if d['etype'] in export_data_vi:
            current_row[d['etype']] = d['data']
            continue
        if t_pre == 0 and d['etype'] in ['frame_event', 'polarity_event']:
            print('resetting t_pre (first %s)' % d['etype'])
            t_pre = d['timestamp'] + t_offset
        if d['etype'] == 'frame_event' and args.export_aps:
            if fixed_dt:
                while t_pre + args.binsize < d['timestamp'] + t_offset:
Ejemplo n.º 4
0
    def show(self, d, t=None):
        # handle keyboad input
        key_pressed = cv2.waitKey(1) & 0xFF  # http://www.asciitable.com/
        if key_pressed != -1:
            if key_pressed == ord('i'):  # 'i' pressed
                if self.display_color == 0:
                    self.display_color = 255
                elif self.display_color == 255:
                    self.display_color = 0
                    self.display_info = not self.display_info
                print('rotated car info display')
            elif key_pressed == ord('x'):  # exit
                print('exiting from x key')
                raise SystemExit
            elif key_pressed == ord('f'):  # f (faster) key pressed
                self.min_dt = self.min_dt*1.2
                print('increased min_dt to ', self.min_dt, ' s')
                # self.playback_speed = min(self.playback_speed + 0.2, 5.0)
                # print('increased playback speed to ',self.playback_speed)
            elif key_pressed == ord('s'):  # s (slower) key pressed
                self.min_dt = self.min_dt/1.2
                print('decreased min_dt to ', self.min_dt, ' s')
                # self.playback_speed = max(self.playback_speed - 0.2, 0.2)
                # print('decreased playback speed to ',self.playback_speed)
            elif key_pressed == ord('b'):  # brighter
                self.dvs_contrast = max(1, self.dvs_contrast-1)
                print('increased DVS contrast to ', self.dvs_contrast,
                      ' full scale event count')
            elif key_pressed == ord('d'):  # brighter
                self.dvs_contrast = self.dvs_contrast+1
                print('decreased DVS contrast to ', self.dvs_contrast,
                      ' full scale event count')
            elif key_pressed == ord(' '):  # toggle paused
                self.paused = not self.paused
                print('decreased DVS contrast to ', self.dvs_contrast,
                      ' full scale event count')
        if self.paused:
            while True:
                key_paused = cv2.waitKey(1) or 0xff
                if key_paused == ord(' '):
                    self.paused = False
                    break

        ''' receive and handle single event '''
        if 'etype' not in d:
            d['etype'] = d['name']
        etype = d['etype']
        if not self.t_pre.get(etype):
            self.t_pre[etype] = -1
        self.count[etype] = self.count.get(etype, 0) + 1
        if etype == 'frame_event' and \
                time.time() - self.t_pre[etype] > self.min_dt:
            if 'data' not in d:
                unpack_data(d)
            img = (d['data'] / 256).astype(np.uint8)
            if self.rotate180 is True:
                # grab the dimensions of the image and calculate the center
                # of the image
                (h, w) = img.shape[:2]
                center = (w // 2, h // 2)

                # rotate the image by 180 degrees
                M = cv2.getRotationMatrix2D(center, 180, 1.0)
                img = cv2.warpAffine(img, M, (w, h))
            if self.display_info:
                self._plot_steering_wheel(img)
                self._print(img, (50, 220), 'accelerator_pedal_position', '%')
                self._print(img, (100, 220), 'brake_pedal_status',
                            'brake', True)
                self._print(img, (200, 220), 'vehicle_speed', 'km/h')
                self._print(img, (300, 220), 'engine_speed', 'rpm')
            if t is not None:
                self._plot_timeline(img)
            if self.zoom != 1:
                img = cv2.resize(
                    img, None, fx=self.zoom, fy=self.zoom,
                    interpolation=cv2.INTER_CUBIC)
            cv2.imshow('frame', img)
            # cv2.waitKey(1)
            self.t_pre[etype] = time.time()
        elif etype == 'polarity_event':
            if 'data' not in d:
                unpack_data(d)
            # makes DVS image, but only from latest message
            self.pol_img[d['data'][:, 2], d['data'][:, 1]] += \
                (d['data'][:, 3]-.5)/self.dvs_contrast
            if time.time() - self.t_pre[etype] > self.min_dt:
                if self.zoom != 1:
                    self.pol_img = cv2.resize(
                            self.pol_img, None,
                            fx=self.zoom, fy=self.zoom,
                            interpolation=cv2.INTER_CUBIC)
                    if self.rotate180 is True:
                        # grab the dimensions of the image and
                        # calculate the center
                        # of the image
                        (h, w) = self.pol_img.shape[:2]
                        center = (w // 2, h // 2)

                        # rotate the image by 180 degrees
                        M = cv2.getRotationMatrix2D(center, 180, 1.0)
                        self.pol_img = cv2.warpAffine(self.pol_img, M, (w, h))
                        if self.display_info:
                            self._print_string(self.pol_img, (25, 25), "%.2fms"%(self.min_dt*1000))
                cv2.imshow('polarity', self.pol_img)
                # cv2.waitKey(1)
                self.pol_img = 0.5 * np.ones(DVS_SHAPE)
                
                self.t_pre[etype] = time.time()
        elif etype in VIEW_DATA:
            if 'data' not in d:
                d['data'] = d['value']
            self.cache[etype] = d['data']
            self.t_pre[etype] = time.time()
        if t is not None:
            self._set_t(t)
Ejemplo n.º 5
0
    def readEntire(self):
        """
        Read entire file to memory.

        Returns
        -------
        aps_ts: np.array,
            timestamps of aps frames.
        aps_frame: np.ndarray, [n, width, height]
            aps frames
        events: numpy record array.
            events, col names: ["ts", "y", "x", "polarity"], \
                data types: ["<f8", "<i8", "<i8", "<i8"]
        """
        sys_ts, t_offset, current = 0, 0, 0
        timestamp = 0
        frames, events = [], []
        while self.m.has_data and sys_ts <= self.stop * 1e-6:
            try:
                sys_ts, d = self.m.get()
            except Queue.Empty:
                # wait for queue to fill up
                time.sleep(0.01)
                continue
            if not d or sys_ts < self.start * 1e-6:
                # skip unused data
                continue
            if d['etype'] == 'special_event':
                unpack_data(d)
                # this is a timestamp reset
                if any(d['data'] == 0):
                    print('ts reset detected, setting offset', timestamp)
                    t_offset += current
                    # NOTE the timestamp of this special event is not meaningful
                continue
            if d['etype'] == 'frame_event':
                ts = d['timestamp'] + t_offset
                frame = filter_frame(unpack_data(d))
                data = np.array([(ts, frame)],
                                dtype=np.dtype([('ts', np.float64),
                                                ('frame', np.uint8,
                                                 frame.shape)]))
                frames.append(data)
                current = ts
                continue
            if d['etype'] == 'polarity_event':
                unpack_data(d)
                data = d["data"]
                data = np.hstack((data[:, 0][:, None] * 1e-6 + t_offset,
                                  data[:, 1][:, None], data[:, 2][:, None],
                                  data[:, 3].astype(np.int)[:, None] * 2 - 1))
                events.append(data)
                continue
        frames = np.hstack(frames)
        events = np.vstack(events)
        frames["ts"] -= frames["ts"][0]
        events[:, 0] -= events[0][0]
        self.f_in.exit.set()
        self.m.exit.set()
        self.f_in.join()
        self.m.join()

        return frames, events