Exemplo n.º 1
0
    def open(self, camera_path='cam://0', sw_correction=True):
        '''
        Opens connection to the camera and closes it in controlled fashion when
        exiting the context.

        @param camera_path: String path to the camera. Default is 'cam://0'
        @param sw_correction: Use previously defined calibration file (.xca)
        '''
        self.handle = \
            xdll.XDLL.open_camera(camera_path.encode('utf-8'), 0, 0)
        print('XCHANDLE:', self.handle)
        if self.handle == 0:
            raise Exception('Handle is NULL')
        if not xdll.XDLL.is_initialised(self.handle):
            raise Exception('Initialization failed.')
        if self.calibration:
            if sw_correction:
                flag = xdll.XDLL.XLC_StartSoftwareCorrection
            else:
                flag = 0
            error = xdll.XDLL.load_calibration(self.handle, self.calibration,
                                               flag)
            if error != xdll.XDLL.I_OK:
                msg = 'Could\'t load' + \
                    'calibration file ' + \
                    str(self.calibration) + \
                    xdll.error2str(error)
                raise Exception(msg)
        return self
Exemplo n.º 2
0
    def stop_recording(self):
        '''
        Stops capturing frames after the latest one is done capturing.
        @return: Metadata tuple array
        '''
        start = time.time()
        self.enabled = False
        self._capture_thread.join(5)
        if self._capture_thread.isAlive():
            raise Exception('Thread didn\'t stop.')
        end = time.time()
        self._record_time += end - start
        error = xdll.XDLL.stop_capture(self.handle)
        if error != xdll.XDLL.I_OK:
            xdll.print_error(error)
            raise Exception('Could not stop capturing. %s' %
                            xdll.error2str(error))
        self.check_thread_exceptions()  # Raises exception

        # Return ENVI metadata about the recording
        frame_dims = self.get_frame_dims()
        frame_type = self.get_frame_type()
        meta = (
            ('samples', frame_dims[1]), ('bands', self.frames_count),
            ('lines', frame_dims[0]),
            ('data type',
             utils.datatype2envitype('u' +
                                     str(xdll.XDLL.pixel_sizes[frame_type]))),
            ('interleave', 'bil'), ('byte order', 1),
            ('description', 'Capture time = %d\nFrame time stamps = %s' %
             (self._record_time, str(self._times))))
        return meta
Exemplo n.º 3
0
    def get_frame(self, buffer, frame_t, size, flag=0):
        '''
        Reads a frame from camera. Raises an exception on errors.

        @param buffer: bytes buffer (output) to which a frame is read from
                       the camera.
        @param frame_t: frame type enumeration. Use get_frame_type() to find
                        the native type.
        @param size: frame size in bytes. Use get_frame_dims()
        @param flag: Type of execution. 0 is non-blocking, xdll.XGF_Blocking
                     is blocking.
        @return: True if got frame, otherwise False
        '''
        # frame_buffer = \
        #     np.zeros((frame_size / pixel_size,),
        #              dtype=np.int16)
        # frame_buffer = bytes(frame_size)
        error = xdll.XDLL.get_frame(
            self.handle,
            frame_t,
            flag,
            # frame_buffer.ctypes.data,
            buffer,
            size)
        # ctypes.cast(buffer, ctypes.POINTER(ctypes.c))
        if error not in (xdll.XDLL.I_OK, xdll.XDLL.E_NO_FRAME):
            raise Exception('Error while getting frame: %s' %
                            xdll.error2str(error))
        # frame_buffer = np.reshape(frame_buffer, frame_dims)
        return error == xdll.XDLL.I_OK  # , frame_buffer
Exemplo n.º 4
0
    def capture_single_frame(self, dump_buffer=False):
        '''
        Captures a single frame
        '''
        name = 'capture_single_frame'
        frame = None
        error = xdll.XDLL.start_capture(self.handle)

        if error != xdll.XDLL.I_OK:
            xdll.print_error(error)
            raise Exception('%s Starting capture failed! %s' %
                            (name, xdll.error2str(error)))

        if xdll.XDLL.is_capturing(self.handle) == 0:
            for i in range(5):
                if xdll.XDLL.is_capturing(self.handle) == 0:
                    print(name, 'Camera is not capturing. Retry number %d' % i)
                    time.sleep(0.1)
                else:
                    break

        if xdll.XDLL.is_capturing(self.handle) == 0:
            raise Exception('Camera is not capturing.')

        elif xdll.XDLL.is_capturing(self.handle):
            size = self.get_frame_size()
            dims = self.get_frame_dims()
            frame_t = self.get_frame_type()
            frame_buffer = bytes(size)

            if dump_buffer:
                ok = self.get_frame(frame_buffer,
                                    frame_t=frame_t,
                                    size=size,
                                    flag=xdll.XDLL.XGF_Blocking)
                frame_buffer = bytes(size)

            ok = self.get_frame(frame_buffer,
                                frame_t=frame_t,
                                size=size,
                                flag=xdll.XDLL.XGF_Blocking)

        else:
            raise Exception('Camera is not capturing.')

        return frame_buffer, size, dims, frame_t
Exemplo n.º 5
0
 def capture_single_frame(self):
     '''
     TODO: Not tested
     '''
     name = 'capture_single_frame'
     frame = None
     error = xdll.XDLL.start_capture(self.handle)
     if error != xdll.XDLL.I_OK:
         xdll.print_error(error)
         raise Exception('%s Starting capture failed! %s' %
                         (name, xdll.error2str(error)))
     if xdll.XDLL.is_capturing(self.handle) == 0:
         for i in range(5):
             if xdll.XDLL.is_capturing(self.handle) == 0:
                 print(name, 'Camera is not capturing. Retry number %d' % i)
                 time.sleep(0.1)
             else:
                 break
     if xdll.XDLL.is_capturing(self.handle) == 0:
         raise Exception('Camera is not capturing.')
     elif xdll.XDLL.is_capturing(self.handle):
         size = self.get_frame_size()
         dims = self.get_frame_dims()
         frame_t = self.get_frame_type()
         # pixel_size = self.get_pixel_size()
         print(name, 'Size:', size, 'Dims:', dims, 'Frame type:', frame_t)
         frame_buffer = bytes(size)
         while True:
             ok = self.get_frame(frame_buffer,
                                 frame_t=frame_t,
                                 size=size,
                                 dims=dims,
                                 flag=0)  # Non-blocking
             # xdll.XGF_Blocking
             if ok:
                 frame = frame_buffer
                 break
             # else:
             #     print(name, 'Missed frame', i)
     else:
         raise Exception('Camera is not capturing.')
     print(name, 'Finished')
     return frame, size, dims, frame_t
Exemplo n.º 6
0
 def capture_frame_stream(self):
     '''
     Thread function for continuous camera capturing.
     Keeps running until 'enabled' property is set to False.
     '''
     name = 'capture_frame_stream'
     try:
         error = xdll.XDLL.start_capture(self.handle)
         if error != xdll.XDLL.I_OK:
             xdll.print_error(error)
             raise Exception('%s Starting capture failed! %s' %
                             (name, xdll.error2str(error)))
         if xdll.XDLL.is_capturing(self.handle) == 0:
             for i in range(5):
                 if xdll.XDLL.is_capturing(self.handle) == 0:
                     print(name,
                           'Camera is not capturing. Retry number %d' % i)
                     time.sleep(0.1)
                 else:
                     break
         if xdll.XDLL.is_capturing(self.handle) == 0:
             raise Exception('Camera is not capturing.')
         elif xdll.XDLL.is_capturing(self.handle):
             self.frames_count = 0
             size = self.get_frame_size()
             dims = self.get_frame_dims()
             frame_t = self.get_frame_type()
             # pixel_size = self.get_pixel_size()
             print(name, 'Size:', size, 'Dims:', dims, 'Frame type:',
                   frame_t)
             frame_buffer = bytes(size)
             # ctrl_frame_buffer = bytearray(4)  # 32 bits
             start_time = utils.get_time()
             while self._enabled:
                 # frame_buffer = \
                 #     np.zeros((size / pixel_size,),
                 #              dtype=np.int16)
                 # buffer = memoryview(frame_buffer)
                 while True:
                     ok = self.get_frame(frame_buffer,
                                         frame_t=frame_t,
                                         size=size,
                                         flag=0)  # Non-blocking
                     # xdll.XGF_Blocking
                     if ok:
                         curr_time = utils.get_time() - start_time
                         self._times.append(curr_time)
                         ctrl_frame_buffer = struct.pack(
                             'I', curr_time)  # 4 bytes
                         for h, incl_ctrl_frame in self.handlers:
                             # print(name,
                             #       'Writing to %s' % str(h.__class__.__name__))
                             if incl_ctrl_frame:
                                 h.write(ctrl_frame_buffer)
                             wrote_bytes = h.write(frame_buffer)
                             # print(name,
                             #       'Wrote to %s:' % str(h.__class__.__name__),
                             #       wrote_bytes,
                             #       'bytes')
                         break
                     # else:
                     #     print(name, 'Missed frame', i)
                 self.frames_count += 1
         else:
             raise Exception('Camera is not capturing.')
     except Exception as e:
         self.exc_queue.put(sys.exc_info())
         print(name,
               '%s(%s): %s' % (type(e).__name__, str(e.errno), e.strerror))
     print(name, 'Thread closed')