Example #1
0
    def saveImage(self):
        ueye.is_AllocImageMem(self.hcam, self.sensorinfo.nMaxWidth, self.sensorinfo.nMaxHeight, 24, self.pccmem,
                              self.memID)
        ueye.is_SetImageMem(self.hcam, self.pccmem, self.memID)
        ueye.is_SetDisplayPos(self.hcam, 100, 100)

        self.nret = ueye.is_FreezeVideo(self.hcam, ueye.IS_WAIT)
        self.rawImageTimeStamp = datetime.datetime.now()
        self.imageTimeStamp = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
        self.imageName = 'imagem_%s.jpg' % self.imageTimeStamp
        self.imagePath = self.operationConfigs['savingDirectory'] + self.imageName
        # self.imagePath = self.operationConfigs['savingDirectory'] + "tmp.jpg"
        self.FileParams = ueye.IMAGE_FILE_PARAMS()
        self.FileParams.pwchFileName = self.imagePath
        self.FileParams.nFileType = ueye.IS_IMG_BMP
        self.FileParams.ppcImageMem = None
        self.FileParams.pnImageID = None

        self.nret = ueye.is_ImageFile(self.hcam, ueye.IS_IMAGE_FILE_CMD_SAVE, self.FileParams,
                                      ueye.sizeof(self.FileParams))

        ueye.is_FreeImageMem(self.hcam, self.pccmem, self.memID)
        #sleep(.01)
        #ueye.is_ExitCamera(self.hcam)
        self.image = np.uint8(ndimage.imread(self.imagePath, flatten=True))
        self.image = self.crop_end(self.image, 0, 100)
Example #2
0
    def show_image(self):
        nRet = ueye.is_InitCamera(self.h_cam, None)
        nRet = ueye.is_SetDisplayMode(self.h_cam, ueye.IS_SET_DM_DIB)
        nRet = ueye.is_AOI(self.h_cam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI,
                           ueye.sizeof(self.rectAOI))

        self.width = self.rectAOI.s32Width
        self.height = self.rectAOI.s32Height

        nRet = ueye.is_AllocImageMem(self.h_cam, self.width, self.height,
                                     self.nBitsPerPixel, self.pcImageMemory,
                                     self.MemID)
        nRet = ueye.is_SetImageMem(self.h_cam, self.pcImageMemory, self.MemID)
        nRet = ueye.is_SetColorMode(self.h_cam, self.ColorMode)
        nRet = ueye.is_CaptureVideo(self.h_cam, ueye.IS_DONT_WAIT)
        nRet = ueye.is_InquireImageMem(self.h_cam, self.pcImageMemory,
                                       self.MemID, self.width, self.height,
                                       self.nBitsPerPixel, self.pitch)

        while nRet == ueye.IS_SUCCESS:
            array = ueye.get_data(self.pcImageMemory,
                                  self.width,
                                  self.height,
                                  self.nBitsPerPixel,
                                  self.pitch,
                                  copy=False)
            frame = np.reshape(
                array,
                (self.height.value, self.width.value, self.bytes_per_pixel))
            frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
            size = (self.height, self.width)
            new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(
                self.camera_matrix, self.dist_coeff, size, 1, size)
            dst = cv2.undistort(frame, self.camera_matrix, self.dist_coeff,
                                None, new_camera_matrix)
            x, y, w, h = roi
            self.dst = dst[y:y + h, x:x + w]

            self.detect_colors()

            self.extrinsic_calibration()

            cv2.imshow("camera", self.dst)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

            elif cv2.waitKey(1) & 0xFF == ord('t'):
                cv2.imwrite("/home/lennart/dorna/camera/images/gps.bmp",
                            self.dst)

            elif cv2.waitKey(100) & 0xFF == ord('l'):
                self.found_container = False
                self.container_world_position.clear()
                print("Behälterposition zurückgesetzt")

        ueye.is_FreeImageMem(self.h_cam, self.pcImageMemory, self.MemID)
        ueye.is_ExitCamera(self.h_cam)
        cv2.destroyAllWindows()
Example #3
0
    def release(self):
        # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
        ueye.is_FreeImageMem(self.hCam, self.pcImageMemory, self.MemID)

        # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
        ueye.is_ExitCamera(self.hCam)

        print('Camera connection closed')
Example #4
0
    def disconnect_device(self, variable=None):

        ueye.is_FreeImageMem(self.hid, self.ppcImgMem, self.MemID)
        ueye.is_ExitCamera(self.hid)
        self.camera = False

        if variable:
            variable.delete(0, 'end')
            self.return_devices(variable)
Example #5
0
    def __del__(self):

        ueye.is_FreeImageMem(self.hCam, self.pcImageMemory, self.MemID)

        # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
        ueye.is_ExitCamera(self.hCam)

        # Destroys the OpenCv windows
        self.timeStampsFile.close()
    def close_connection(self):
        """
        Close the connection with the Thorlabs camera and release the handle 
        as well as the memory usage

        """
        ueye.is_FreeImageMem(self.hcam, self.pcImageMemory, self.MemID)
        ueye.is_ExitCamera(self.hcam)
        print('Connection to the camera was closed')
Example #7
0
    def disconnect_device(self):
        """
        Disconnect a previously connected camera.
        
        """

        ueye.is_FreeImageMem(self.hid, self.ppcImgMem, self.MemID)
        ueye.is_ExitCamera(self.hid)
        self.camera = False
Example #8
0
def main_loop():
    color_trackbars()

    while (CameraApi.nRet == ueye.IS_SUCCESS):

        # In order to display the image in an OpenCV window we need to...
        # ...extract the data of our image memory
        array = ueye.get_data(CameraApi.pcImageMemory,
                              CameraApi.width,
                              CameraApi.height,
                              CameraApi.nBitsPerPixel,
                              CameraApi.pitch,
                              copy=False)

        # ...reshape it in an numpy array...
        frame = np.reshape(array,
                           (CameraApi.height.value, CameraApi.width.value,
                            CameraApi.bytes_per_pixel))

        # ...resize the image by a half
        # frame = cv2.resize(frame,(0,0), fx=0.5, fy=0.5)

        #---------------------------------------------------------------------------------------------------------------------------------------
        #Convert camera feed from BGRA to BGR
        frame_to_bgr = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)

        #Apply a Gaussian blur that has 11x11 kernel size to the BGR frame
        frame_to_bgr = cv2.GaussianBlur(frame_to_bgr, (5, 5), 0)

        cv2.imshow("BGR Frame", frame_to_bgr)

        #Convert camera feed from BGR color space to HSV color space
        hsv_frame = cv2.cvtColor(frame_to_bgr, cv2.COLOR_BGR2HSV)

        frame_threshold(frame_to_bgr, hsv_frame)

        #---------------------------------------------------------------------------------------------------------------------------------------

        # Press q if you want to end the loop
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    #---------------------------------------------------------------------------------------------------------------------------------------

    # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
    ueye.is_FreeImageMem(CameraApi.hCam, CameraApi.pcImageMemory,
                         CameraApi.MemID)

    # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
    ueye.is_ExitCamera(CameraApi.hCam)

    # Destroys the OpenCv windows
    cv2.destroyAllWindows()

    print()
    print("END")
Example #9
0
    def Exit(self):
        if self.KepcoInit_Switch == 1:
            self.kepco_inst.write("CURR {curr}".format(curr=0))
            self.kepco_inst.write("OUTP OFF")

        self.Movie_Switch = -1
        #time.sleep(1)
        #cv2.destroyAllWindows()
        ueye.is_FreeImageMem(self.hcam, self.mem_ptr, self.mem_id)
        self.ret = ueye.is_ExitCamera(self.hcam)
        self.close()
Example #10
0
	def setBuffer(self, size):
		self.bufCount=size
		if self.Imgs is not None:
			for i in range(self.bufCount):
				rv=ueye.is_FreeImageMem (self.hcam, self.Imgs[i], self.bufIds[i])
				if rv:
					self.status=True
					return True				
				self.bufIds[i] = 0
		if self.getAOI():
			self.status=True
			return True
		self.imgWidth=self.AOI.s32Width.value
		self.imgHeight=self.AOI.s32Height.value
		self.Imgs=[ueye.c_mem_p() for i in range(size)]
		self.bufIds=[ueye.c_int() for i in range(size)]


		for i in range(self.bufCount):
			rv=ueye.is_AllocImageMem(self.hcam, self.imgWidth, self.imgHeight,self.bitsPixel, self.Imgs[i], self.bufIds[i])
			if rv:
				self.status=True
				return True
			rv=ueye.is_AddToSequence (self.hcam, self.Imgs[i], self.bufIds[i])
			if rv:
				self.status=True
				return True
		self.LineInc=ueye.c_int()
		rv=ueye.is_GetImageMemPitch (self.hcam, self.LineInc)
		if rv:
			self.status=True
			return True
		return False
 def alloc(self, buffer_count=3):
     '''
     Initialization of the ring buffer.
     '''
     rec_aoi = self.Rect(*self.get_aoi())
     color_mode = ueye.is_SetColorMode(self.cam, ueye.IS_GET_COLOR_MODE)
     self.bpp = get_bits_per_pixel(color_mode)
     # --- freeing the memory from previous buffer --- #
     for buff in self.frame_buffer:
         hasWorked = ueye.is_FreeImageMem(self.cam, buff.mem_ptr,
                                          buff.mem_id)
         self.check(hasWorked, 'is_FreeImageMem')
     # --- allocate memory to buffer --- #
     for i in range(buffer_count):
         buff = self.ImageBuffer()
         ueye.is_AllocImageMem(self.cam, rec_aoi.width, rec_aoi.height,
                               self.bpp, buff.mem_ptr, buff.mem_id)
         hasWorked = ueye.is_AddToSequence(self.cam, buff.mem_ptr,
                                           buff.mem_id)
         self.check(hasWorked, 'is_AddToSequence')
         self.frame_buffer.append(buff)
     # ---  --- #
     hasWorked = ueye.is_InitImageQueue(
         self.cam, 0)  # init and return the success of image queued.
     self.check(hasWorked, 'is_InitImageQueue')
     # ---  --- #
     self.img_buffer = self.frame_buffer[-1]
Example #12
0
    def alloc(self):
        """
        Allocate memory for futur images.
        """
        rect = self.get_aoi()
        bpp = self.nBitsPerPixel

        for buff in self.img_buffer:
            self.nRet = ueye.is_FreeImageMem(self.cam, buff.mem_ptr, buff.mem_id)
            if self.nRet != ueye.IS_SUCCESS:
                error_log(self.nRet, "is_FreeImageMem")
        
        self.img_buffer = []

        for i in range(self.buffer_count):
            buff = ImageBuffer()
            ueye.is_AllocImageMem(self.cam,
                                  rect.width,
                                  rect.height,
                                  bpp,
                                  buff.mem_ptr,
                                  buff.mem_id)
            ueye.is_AddToSequence(self.cam, buff.mem_ptr, buff.mem_id)
            self.img_buffer.append(buff)

        self.nRet = ueye.is_InitImageQueue(self.cam, 0)
        if self.nRet != ueye.IS_SUCCESS:
            error_log(self.nRet, "is_InitImageQueue")
def give_da_stream():

    hCam, sInfo, cInfo, pcImageMemory, MemID, rectAOI, pitch, nBitsPerPixel, channels, m_nColorMode, bytes_per_pixel, height, width = init_camera(
    )

    nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
    nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height,
                                   nBitsPerPixel, pitch)

    try:

        while (nRet == ueye.IS_SUCCESS):

            # In order to display the image in an OpenCV window we need to...
            # ...extract the data of our image memory
            array = ueye.get_data(pcImageMemory,
                                  width,
                                  height,
                                  nBitsPerPixel,
                                  pitch,
                                  copy=False)

            # bytes_per_pixel = int(nBitsPerPixel / 8)

            # ...reshape it in an numpy array...
            frame = np.reshape(array,
                               (height.value, width.value, bytes_per_pixel))

            # ...resize the image by a half
            # frame = cv2.resize(frame,(0,0),fx=0.5, fy=0.5)

            # Press q if you want to end the loop

            # Press q if you want to end the loop

            yield frame
            # ---------------------------------------------------------------------------------------------------------------------------------------
            # Include image data processing here

            # ---------------------------------------------------------------------------------------------------------------------------------------

    finally:
        # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
        ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

        # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
        ueye.is_ExitCamera(hCam)
Example #14
0
 def _deallocate_memory(self):
     if self._ppc_img_mem is None:
         return
     # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
     err = ueye.is_FreeImageMem(self._cam, self._ppc_img_mem, self._mem_id)
     if err != ueye.IS_SUCCESS:
         raise CameraException(self._cam, 'ueye>_deallocate_memory>', err)
     self._ppc_img_mem = None
Example #15
0
    def saveImage(self):
        try:
            time1 = datetime.datetime.now()
            ueye.is_AllocImageMem(self.hcam, self.sensorinfo.nMaxWidth,
                                  self.sensorinfo.nMaxHeight, 24, self.pccmem,
                                  self.memID)
            ueye.is_SetImageMem(self.hcam, self.pccmem, self.memID)
            ueye.is_SetDisplayPos(self.hcam, 100, 100)

            self.nret = ueye.is_FreezeVideo(self.hcam, ueye.IS_WAIT)

            self.imageTimeStamp = datetime.datetime.now().strftime(
                '%Y_%m_%d_%H_%M_%S')
            self.imageName = 'imagem_%s.jpg' % self.imageTimeStamp
            self.imagePath = self.operationConfigs[
                'savingDirectory'] + self.imageName

            self.FileParams = ueye.IMAGE_FILE_PARAMS()
            self.FileParams.pwchFileName = self.imagePath
            self.FileParams.nFileType = ueye.IS_IMG_BMP
            self.FileParams.ppcImageMem = None
            self.FileParams.pnImageID = None

            self.nret = ueye.is_ImageFile(self.hcam,
                                          ueye.IS_IMAGE_FILE_CMD_SAVE,
                                          self.FileParams,
                                          ueye.sizeof(self.FileParams))
            ueye.is_FreeImageMem(self.hcam, self.pccmem, self.memID)
            sleep(.01)
            ueye.is_ExitCamera(self.hcam)

            time2 = datetime.datetime.now()
            elapsed_time = time2 - time1
            logging.info('Saved: {}! Elasped time (ms): {}'.format(
                self.imageName, elapsed_time.microseconds / 1000))
            self.pijuice.status.SetLedBlink('D2', 2, [0, 255, 0], 50,
                                            [0, 255, 0], 50)
            sleep(.1)
            self.pijuice.status.SetLedState('D2', [0, 0, 0])
        except:
            logging.warning('NOT SAVED: {}!\n'.format(self.imageName))
            self.pijuice.status.SetLedBlink('D2', 2, [255, 0, 0], 50,
                                            [255, 0, 0], 50)
            sleep(.1)
            self.pijuice.status.SetLedState('D2', [0, 0, 0])
            pass
Example #16
0
    def run(self):

        a = ueye.is_InitCamera(self.hCam, None)
        b = ueye.is_GetCameraInfo(self.hCam, self.cInfo)
        c = ueye.is_GetSensorInfo(self.hCam, self.sInfo)

        d = ueye.is_ResetToDefault(self.hCam)
        e = ueye.is_SetDisplayMode(self.hCam, ueye.IS_SET_DM_DIB)

        g = ueye.is_AOI(self.hCam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI,
                        ueye.sizeof(self.rectAOI))
        self.width = self.rectAOI.s32Width
        self.height = self.rectAOI.s32Height

        h = ueye.is_AllocImageMem(self.hCam, self.width, self.height,
                                  self.nBitsPerPixel, self.pcImageMemory,
                                  self.MemID)
        i = ueye.is_SetImageMem(self.hCam, self.pcImageMemory, self.MemID)
        f = ueye.is_SetColorMode(self.hCam, ueye.IS_CM_MONO12)

        ueye.is_CaptureVideo(self.hCam, ueye.IS_WAIT)

        j = ueye.is_InquireImageMem(self.hCam, self.pcImageMemory, self.MemID,
                                    self.width, self.height,
                                    self.nBitsPerPixel, self.pitch)
        self.IMAGE_FILE_PARAMS = ueye.IMAGE_FILE_PARAMS(
            self.pcImageMemory, self.MemID)
        self.IMAGE_FILE_PARAMS.nFileType = ueye.IS_IMG_PNG
        self.k = ueye.sizeof(self.IMAGE_FILE_PARAMS)

        # ueye.is_AutoParameter(self.hCam, ueye.IS_AES_CMD_SET_ENABLE, self.autoParameter, ueye.sizeof(self.autoParameter))

        while self.flag:
            self.data = np.ctypeslib.as_array(
                ctypes.cast(self.pcImageMemory,
                            ctypes.POINTER(ctypes.c_ubyte)),
                (self.height * self.pitch, ))
            self.data.dtype = 'uint16'

            self.CameraSignal.emit(self.data)
            time.sleep(0.1)

        ueye.is_FreeImageMem(self.hCam, self.pcImageMemory, self.MemID)
        ueye.is_ExitCamera(self.hCam)
def take_picture(hCam, pcImageMemory, MemID, pitch, nBitsPerPixel,
                 bytes_per_pixel, height, width):

    array = ueye.get_data(pcImageMemory,
                          width,
                          height,
                          nBitsPerPixel,
                          pitch,
                          copy=False)
    frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))

    ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

    # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
    ueye.is_ExitCamera(hCam)

    # Destroys the OpenCv windows
    cv2.destroyAllWindows()
    return frame
Example #18
0
    def DestroyBuffers(self):
        self._poll = False
        self.check_success(ueye.is_ExitImageQueue(self.h))
        self.check_success(ueye.is_ClearSequence(self.h))

        while len(self._buffers) > 0:
            buffer_id, data = self._buffers.pop()
            self.check_success(ueye.is_FreeImageMem(self.h, data, buffer_id))

        self.free_buffers = None
        self.n_full = 0
Example #19
0
    def saveImage(self):
        # try:
        time1 = datetime.datetime.now()
        ueye.is_AllocImageMem(self.hcam, self.sensorinfo.nMaxWidth,
                              self.sensorinfo.nMaxHeight, 24, self.pccmem,
                              self.memID)
        ueye.is_SetImageMem(self.hcam, self.pccmem, self.memID)
        ueye.is_SetDisplayPos(self.hcam, 100, 100)

        self.nret = ueye.is_FreezeVideo(self.hcam, ueye.IS_WAIT)
        self.rawImageTimeStamp = datetime.datetime.now()
        self.imageTimeStamp = datetime.datetime.now().strftime(
            '%Y_%m_%d_%H_%M_%S')
        self.imageName = 'imagem_%s.jpg' % self.imageTimeStamp
        self.imagePath = self.operationConfigs[
            'savingDirectory'] + self.imageName
        # self.imagePath = self.operationConfigs['savingDirectory'] + "tmp.jpg"
        self.FileParams = ueye.IMAGE_FILE_PARAMS()
        self.FileParams.pwchFileName = self.imagePath
        self.FileParams.nFileType = ueye.IS_IMG_BMP
        self.FileParams.ppcImageMem = None
        self.FileParams.pnImageID = None

        self.nret = ueye.is_ImageFile(self.hcam, ueye.IS_IMAGE_FILE_CMD_SAVE,
                                      self.FileParams,
                                      ueye.sizeof(self.FileParams))

        ueye.is_FreeImageMem(self.hcam, self.pccmem, self.memID)
        sleep(.01)
        ueye.is_ExitCamera(self.hcam)

        # self.image = np.ones((self.sensorinfo.nMaxHeight.value, self.sensorinfo.nMaxWidth.value), dtype=np.uint8)
        # ueye.is_CopyImageMem(self.hcam, self.pccmem, self.memID, self.image.ctypes.data)

        self.image = np.uint8(ndimage.imread(self.imagePath, flatten=True))

        time2 = datetime.datetime.now()
        elapsed_time = time2 - time1
        logging.info('Saved: {}! Elasped time (s): {}'.format(
            self.imageName, elapsed_time.total_seconds()))
Example #20
0
    def saveImage(self):
        try:
            time1 = datetime.datetime.now()
            ueye.is_AllocImageMem(self.hcam, self.sensorinfo.nMaxWidth,
                                  self.sensorinfo.nMaxHeight, 24, self.pccmem,
                                  self.memID)
            ueye.is_SetImageMem(self.hcam, self.pccmem, self.memID)
            ueye.is_SetDisplayPos(self.hcam, 100, 100)

            self.nret = ueye.is_FreezeVideo(self.hcam, ueye.IS_WAIT)

            self.imageTimeStamp = datetime.datetime.now().strftime(
                '%Y_%m_%d_%H_%M_%S')
            self.imageName = 'imagem_%s.jpg' % self.imageTimeStamp
            self.imagePath = self.savingDirectory + self.imageName

            self.FileParams = ueye.IMAGE_FILE_PARAMS()
            self.FileParams.pwchFileName = self.imagePath
            self.FileParams.nFileType = ueye.IS_IMG_BMP
            self.FileParams.ppcImageMem = None
            self.FileParams.pnImageID = None

            self.nret = ueye.is_ImageFile(self.hcam,
                                          ueye.IS_IMAGE_FILE_CMD_SAVE,
                                          self.FileParams,
                                          ueye.sizeof(self.FileParams))
            # print(nret)
            ueye.is_FreeImageMem(self.hcam, self.pccmem, self.memID)
            sleep(.1)
            ueye.is_ExitCamera(self.hcam)

            time2 = datetime.datetime.now()
            elapsed_time = time2 - time1
            # print('Elasped time (s): {}'.format(elapsed_time))
            print('Image saved at {}! Elasped time (ms) {}'.format(
                self.imageTimeStamp, elapsed_time.microseconds / 1000))
        except:
            print('Image not saved at {}!\n'.format(self.imageTimeStamp))
            pass
Example #21
0
    def alloc(self, buffer_count=3):
        rect = self.get_aoi()
        bpp = get_bits_per_pixel(self.get_colormode())

        for buff in self.img_buffers:
            check(ueye.is_FreeImageMem(self.h_cam, buff.mem_ptr, buff.mem_id))

        for i in range(buffer_count):
            buff = ImageBuffer()
            ueye.is_AllocImageMem(self.h_cam, rect.width, rect.height, bpp,
                                  buff.mem_ptr, buff.mem_id)

            check(ueye.is_AddToSequence(self.h_cam, buff.mem_ptr, buff.mem_id))

            self.img_buffers.append(buff)

        ueye.is_InitImageQueue(self.h_cam, 0)
Example #22
0
 def alloc(self):
     """
     Allocate memory for futur images.
     """
     # Get camera settings
     rect = self.get_aoi()
     bpp = get_bits_per_pixel(self.get_colormode())
     # Check that already existing buffers are free
     for buff in self.img_buffers:
         check(ueye.is_FreeImageMem(self.h_cam, buff.mem_ptr, buff.mem_id))
     self.img_buffers = []
     # Create asked buffers
     for i in range(self.buffer_count):
         buff = ImageBuffer()
         ueye.is_AllocImageMem(self.h_cam, rect.width, rect.height, bpp,
                               buff.mem_ptr, buff.mem_id)
         check(ueye.is_AddToSequence(self.h_cam, buff.mem_ptr, buff.mem_id))
         self.img_buffers.append(buff)
     # Check that ...
     ueye.is_InitImageQueue(self.h_cam, 0)
    #...and finally display it
    cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame2)

    # Press q if you want to end the loop
    if cv2.waitKey(1):
    	nRet = ueye.is_FreezeVideo(hCam, ueye.IS_WAIT)
    	nRet1= ueye.is_GetImageInfo(hCam,MemID, Iinfo, ueye.sizeof(Iinfo) )
    	print(Iinfo.TimestampSystem.wSecond.value)

    if cv2.waitKey(1) & 0xFF == ord('s'):
        string="Cal"+str(n)+".jpg"
    	cv2.imwrite(string, frame)
        n=n+1 

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        
#---------------------------------------------------------------------------------------------------------------------------------------

# Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

# Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
ueye.is_ExitCamera(hCam)

# Destroys the OpenCv windows
cv2.destroyAllWindows()

print()
print("END")
Example #24
0
# Cameras' id
id = [1, 2, 3]

dev = ["Camera" + str(id[0]), "Camera" + str(id[1]), "Camera" + str(id[2])]

# Initialize Cameras with specific id
Cam0 = camera.Cam(id[0], 10)

# Setup cameras
Cam0.Setup()

p = tc.Capture_Thread(Cam0)
p.start()

# Continuous capture and saving of the image
while Cam0.nRet == ueye.IS_SUCCESS:
    try:

        if not p.joined:
            p.join()
            print("p joined")

    except KeyboardInterrupt:
        p.kill()
        break

# Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
ueye.is_FreeImageMem(Cam0.cam, Cam0.pcImageMemory, Cam0.MemID)

Cam0.exit()
Example #25
0
    #Calibrate camera
    tmp = calib()
    while(count < 5):
        cv2.waitKey(100)
        count+=1
    
    display()
    cv2.waitKey(0)

    (blue_xiyi, blue_angle, green_xiyi, green_angle) = markers_cycle_horizontal()

    if blue_xiyi is None or green_xiyi:
        #Clean-up
        cv2.destroyAllWindows()
        ueye.is_FreeImageMem(hcam, mem_ptr, mem_id)
        ueye.is_StopLiveVideo(hcam, ueye.IS_FORCE_VIDEO_STOP)
        ueye.is_ExitCamera(hcam)
        cmd.event('go_init')
        exit()


    # (..........)
    # (..........)
    # (..........)
    # (..........)
    # (..........)

    #Clean-up
    cv2.destroyAllWindows()
    ueye.is_FreeImageMem(hcam, mem_ptr, mem_id)
Example #26
0
print(wertSet)

help(ueye.is_Exposure)

wert = ueye.c_double()
sizeo = ueye.sizeof(wert)
nret = ueye.is_Exposure(hcam, ueye.IS_EXPOSURE_CMD_GET_EXPOSURE, wert, sizeo)
print(wert)
nret = ueye.is_FreezeVideo(hcam, ueye.IS_WAIT)
print(nret)
FileParams = ueye.IMAGE_FILE_PARAMS()
FileParams.pwchFileName = "c:\python-test-image.png"
FileParams.nFileType = ueye.IS_IMG_PNG
FileParams.ppcImageMem = None
FileParams.pnImageID = None
nret = ueye.is_ImageFile(hcam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams,
                         ueye.sizeof(FileParams))
print(nret)

nret = ueye.is_FreezeVideo(hcam, ueye.IS_WAIT)
print(nret)
FileParams = ueye.IMAGE_FILE_PARAMS()
FileParams.pwchFileName = "c:\python-test-image1.png"
FileParams.nFileType = ueye.IS_IMG_PNG
FileParams.ppcImageMem = None
FileParams.pnImageID = None
nret = ueye.is_ImageFile(hcam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams,
                         ueye.sizeof(FileParams))
print(nret)
ueye.is_FreeImageMem(hcam, pccmem, memID)
ueye.is_ExitCamera(hcam)
Example #27
0
 def close(self):
     ueye.is_FreeImageMem(self.input, self.mem_image, self.mem_id)
     ueye.is_ExitCamera(self.input)
Example #28
0
    def show_image(self):
        # Kamera initialisieren
        nRet = ueye.is_InitCamera(self.h_cam, None)
        nRet = ueye.is_SetDisplayMode(self.h_cam, ueye.IS_SET_DM_DIB)
        nRet = ueye.is_AOI(self.h_cam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI,
                           ueye.sizeof(self.rectAOI))

        self.width = self.rectAOI.s32Width
        self.height = self.rectAOI.s32Height

        nRet = ueye.is_AllocImageMem(self.h_cam, self.width, self.height,
                                     self.nBitsPerPixel, self.pcImageMemory,
                                     self.MemID)
        nRet = ueye.is_SetImageMem(self.h_cam, self.pcImageMemory, self.MemID)
        nRet = ueye.is_SetColorMode(self.h_cam, self.ColorMode)
        nRet = ueye.is_CaptureVideo(self.h_cam, ueye.IS_DONT_WAIT)
        nRet = ueye.is_InquireImageMem(self.h_cam, self.pcImageMemory,
                                       self.MemID, self.width, self.height,
                                       self.nBitsPerPixel, self.pitch)

        while nRet == ueye.IS_SUCCESS:
            # Daten der Kamera auslesen
            array = ueye.get_data(self.pcImageMemory,
                                  self.width,
                                  self.height,
                                  self.nBitsPerPixel,
                                  self.pitch,
                                  copy=False)
            # Bild zuschneiden
            frame = np.reshape(
                array,
                (self.height.value, self.width.value, self.bytes_per_pixel))
            frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
            size = (self.height, self.width)
            # optimale Kameramatrix erstellen
            self.new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(
                self.camera_matrix, self.dist_coeff, size, 1, size)
            # Bild entzerren
            dst = cv2.undistort(frame, self.camera_matrix, self.dist_coeff,
                                None, self.new_camera_matrix)
            x, y, w, h = roi
            self.dst = dst[y:y + h, x:x + w]
            # Farbmasken erstellen
            self.blue.create_color_mask(self.dst, self.found_blue_container)
            self.red.create_color_mask(self.dst, self.found_red_container)
            self.green.create_color_mask(self.dst, self.found_green_container)
            # Kamera extrinsisch kalibrieren
            self.extrinsic_calibration()
            # Bild auf dem Bildschirm ausgeben
            cv2.imshow("camera", self.dst)

            # mit q Kamera schließen
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            # mit t Bild aufnehemen
            elif cv2.waitKey(100) & 0xFF == ord('t'):
                cv2.imwrite(self.path_images + "image.bmp", self.dst)
                print("Bild aufgenommen")
            # mit l Position der Behälter zurücksetzen
            elif cv2.waitKey(100) & 0xFF == ord('l'):
                self.found_blue_container = False
                self.found_red_container = False
                self.found_green_container = False
                print("Behälterposition zurückgesetzt")

        ueye.is_FreeImageMem(self.h_cam, self.pcImageMemory, self.MemID)
        ueye.is_ExitCamera(self.h_cam)
        cv2.destroyAllWindows()
Example #29
0
def ueye_free_image_mem(hcam, membuf, memid) -> None:
    ueye.is_FreeImageMem(hcam, membuf, memid)
def main():
    from pyueye import ueye
    import numpy as np
    import cv2
    import sys
    import os
    import tensorflow as tf
    import SoundTheAlarm
    import time

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")

    # Import utilites
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'inference_graph'

    # Grab path to current working directory
    CWD_PATH = os.getcwd()

    # Path to frozen detection graph .pb file, which contains the model that is used
    # for object detection.
    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME, 'frozen_inference_graph.pb')

    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    # Number of classes the object detector can identify
    NUM_CLASSES = 3

    ## Load the label map.
    # Label maps map indices to category names, so that when our convolution
    # network predicts `5`, we know that this corresponds to `king`.
    # Here we use internal utility functions, but anything that returns a
    # dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.Session(graph=detection_graph)

    # Define input and output tensors (i.e. data) for the object detection classifier

    # Input tensor is the image
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Output tensors are the detection boxes, scores, and classes
    # Each box represents a part of the image where a particular object was detected
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represents level of confidence for each of the objects.
    # The score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

    # Number of objects detected
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    # Variables
    hCam = ueye.HIDS(0)  # 0: first available camera;  1-254: The camera with the specified camera ID
    sInfo = ueye.SENSORINFO()
    cInfo = ueye.CAMINFO()
    pcImageMemory = ueye.c_mem_p()
    MemID = ueye.int()
    rectAOI = ueye.IS_RECT()
    pitch = ueye.INT()
    nBitsPerPixel = ueye.INT(24)  # 24: bits per pixel for color mode; take 8 bits per pixel for monochrome
    channels = 3  # 3: channels for color mode(RGB); take 1 channel for monochrome
    m_nColorMode = ueye.INT()  # Y8/RGB16/RGB24/REG32
    ueye.is_SetColorMode(hCam, ueye.IS_CM_BGR8_PACKED)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    # ---------------------------------------------------------------------------------------------------------------------------------------
    print("START")
    print()

    # Starts the driver and establishes the connection to the camera
    nRet = ueye.is_InitCamera(hCam, None)
    if nRet != ueye.IS_SUCCESS:
        print("is_InitCamera ERROR")

    # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
    nRet = ueye.is_GetCameraInfo(hCam, cInfo)
    if nRet != ueye.IS_SUCCESS:
        print("is_GetCameraInfo ERROR")

    # You can query additional information about the sensor type used in the camera
    nRet = ueye.is_GetSensorInfo(hCam, sInfo)
    if nRet != ueye.IS_SUCCESS:
        print("is_GetSensorInfo ERROR")

    nRet = ueye.is_ResetToDefault(hCam)
    if nRet != ueye.IS_SUCCESS:
        print("is_ResetToDefault ERROR")

    # Set display mode to DIB
    nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)

    # Set the right color mode
    if int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
        # setup the color depth to the current windows setting
        ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
        bytes_per_pixel = int(nBitsPerPixel / 8)
        print("IS_COLORMODE_BAYER: ", )
        print("\tm_nColorMode: \t\t", m_nColorMode)
        print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
        print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
        print()

    elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
        # for color camera models use RGB32 mode
        m_nColorMode = ueye.IS_CM_BGRA8_PACKED
        nBitsPerPixel = ueye.INT(32)
        bytes_per_pixel = int(nBitsPerPixel / 8)
        print("IS_COLORMODE_CBYCRY: ", )
        print("\tm_nColorMode: \t\t", m_nColorMode)
        print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
        print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
        print()

    elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
        # for color camera models use RGB32 mode
        m_nColorMode = ueye.IS_CM_MONO8
        nBitsPerPixel = ueye.INT(8)
        bytes_per_pixel = int(nBitsPerPixel / 8)
        print("IS_COLORMODE_MONOCHROME: ", )
        print("\tm_nColorMode: \t\t", m_nColorMode)
        print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
        print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
        print()

    else:
        # for monochrome camera models use Y8 mode
        m_nColorMode = ueye.IS_CM_MONO8
        nBitsPerPixel = ueye.INT(8)
        bytes_per_pixel = int(nBitsPerPixel / 8)
        print("else")

    # Can be used to set the size and position of an "area of interest"(AOI) within an image
    nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
    if nRet != ueye.IS_SUCCESS:
        print("is_AOI ERROR")

    width = rectAOI.s32Width
    height = rectAOI.s32Height

    # Prints out some information about the camera and the sensor
    print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
    print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
    print("Maximum image width:\t", width)
    print("Maximum image height:\t", height)
    print()

    # ---------------------------------------------------------------------------------------------------------------------------------------

    # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
    nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
    if nRet != ueye.IS_SUCCESS:
        print("is_AllocImageMem ERROR")
    else:
        # Makes the specified image memory the active memory
        nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
        if nRet != ueye.IS_SUCCESS:
            print("is_SetImageMem ERROR")
        else:
            # Set the desired color mode
            nRet = ueye.is_SetColorMode(hCam, m_nColorMode)

    # Activates the camera's live video mode (free run mode)
    nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
    if nRet != ueye.IS_SUCCESS:
        print("is_CaptureVideo ERROR")

    # Enables the queue mode for existing image memory sequences
    nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
    if nRet != ueye.IS_SUCCESS:
        print("is_InquireImageMem ERROR")
    else:
        print("Press q to leave the programm")

    # -----------------------------------start image loop---------------------------------------------------------------

    # Continuous image display
    while (nRet == ueye.IS_SUCCESS):

        # In order to display the image in an OpenCV window we need to...
        # ...extract the data of our image memory
        array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
        # bytes_per_pixel = int(nBitsPerPixel / 8)

        # ...reshape it in an numpy array...
        frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))
        frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)



        # ------------------------------------------network_run---------------------------------------------------------

        frame_expanded = np.expand_dims(frame, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: frame_expanded})
        # print("check this point-2")
        # Draw the results of the detection (aka 'visualize the results')
        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.85)
        counter = []
        add = 0
        while add < (NUM_CLASSES + 1):
            counter.append(0)
            add += 1
        element = 0                                                             # i used this loop to configure the alarm
        for i in classes[0]:                                                    # it uses the variables to count how many of each object appear in each image
            if (scores[0][element]) > 0.85:  #=min_score_thresh                 # use it to feed an alarm detector script based on
                counter[int(i)] += 1                                            # what "should" and "shouldn't" appear
                element += 1
            else:
                break                               #the break stops from iterating through the entire array, which is uneccessary
        ####---the alarm protocol---####
        #if counter[2] and counter[3]:
            #if not counter[1]:
                #SoundTheAlarm.sound_loop()
        print("number of each class", counter)
        # All the results have been drawn on the frame, so it's time to display it.
        frame = cv2.resize(frame, None, fx=0.5, fy=0.5)
        cv2.destroyAllWindows()


        # --------------------------------------------------------------------------------------------------------------

        # ...and finally display it
        cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)

        # Press q if you want to end the loop
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        time.sleep(0.5)
    # ------------------------------------------------------------------------------------------------------------------

    # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
    ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

    # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
    ueye.is_ExitCamera(hCam)

    # Destroys the OpenCv windows
    cv2.destroyAllWindows()

    print()
    print("END")