def main(config_path="/home/oran/Pictures/Settings/default-camera-settings.ini" ): print(config_path) # we need a QApplication, that runs our QT Gui Framework app = PyuEyeQtApp() # a basic qt window view = PyuEyeQtView() view.resize(1920 / 1.5, 1080 / 1.5) view.show() #update_config_gain(update={'red': '0','green' : '0','blue':'0'},set=True) #update_config_exposure(update=70,set=True) # view.user_callback = adjust_manually view.user_callback = print_means # view.user_callback = adjust_manually # camera class to simplify uEye API access cam = Camera() cam.init() # cam.set cam.set_colormode(ueye.IS_CM_BGR8_PACKED) pParam = ueye.wchar_p() pParam.value = config_path ueye.is_ParameterSet(1, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0) # cam.set(cv2.cv.CV_CAP_PROP_EXPOSURE, 10) # cam.__getattribute__('is_CameraStatus') # cam.__setattr__('GetCameraInfo',0) #cam.set_aoi(0,0, 1280, 1024) cam.set_aoi(0, 0, 4912, 3684) cam.alloc() cam.capture_video() ueye._is_GetError ueye.is_Exposure(1, ueye.c_uint(1), ueye.c_void_p(), cbSizeOfParam=ueye.c_int(0)) # ueye.IS_EXPOSURE_CMD_GET_FINE_INCREMENT_RANGE_MIN = 20 # ueye.IS_EXPOSURE_CMD_GET_FINE_INCREMENT_RANGE_MAX = 21 # a thread that waits for new images and processes all connected views thread = FrameThread(cam, view) thread.start() # update_config_gain() # cleanup app.exit_connect(thread.stop) app.exec_() thread.stop() thread.join() cam.stop_video() cam.exit()
def update_config(ini_file="raw_camera_params.ini",key = 'green',set=True): config = configparser.ConfigParser() config.sections() config.read(ini_file, encoding='utf-8-sig') if set: val = input('update ' + key + ' to:') config['Gain'][key] = val with open(ini_file, 'w') as configfile: config.write(configfile) pParam = ueye.wchar_p() pParam.value = ini_file ueye.is_ParameterSet(1, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0)
def grabanimage(): camera_port = 0 ramp_frames = 40 camera = cv2.VideoCapture(camera_port) ################## original (access camera thru cv2) ########################## # def get_image(): # retval, im = camera.read() # return im # # for i in xrange(ramp_frames): # temp = get_image() # print("Taking image...") ## Take the actual image we want to keep # camera_capture = get_image() # file = "USBtemp.png" ## A nice feature of the imwrite method is that it will automatically choose the ## correct format based on the file extension you provide. Convenient! # cv2.imwrite(file, camera_capture) ############# reading from IDS camera (added 6/1/19 by Abner) ################# def get_image(w, h): im = ueye.get_data(mem_ptr, w, h, bitspixel, lineinc, copy=True) return im # init camera hcam = ueye.HIDS(0) initTrigger = ueye.is_InitCamera(hcam, None) # load camera parameters memory = ueye.int(128) # cameraSettingFile = ueye.wchar_p('/media/pi/USB30FD/Frostad Research Group/Biofilm Project/Code/FINAL_Biofilm Project.ini') ret = ueye.is_ParameterSet(hcam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, cameraSettingFile, memory) # set color mode ret = ueye.is_SetColorMode(hcam, ueye.IS_CM_BGR8_PACKED) # set width and height -- FIND FUNCTION TO OUTPUT IMAGE RESOLUTION width = 2560 height = 1920 # allocate memory mem_ptr = ueye.c_mem_p() mem_id = ueye.int() bitspixel = 24 # for colormode = IS_CM_BGR8_PACKED ret = ueye.is_AllocImageMem(hcam, width, height, bitspixel, mem_ptr, mem_id) # set active memory region ret = ueye.is_SetImageMem(hcam, mem_ptr, mem_id) # continuous capture to memory ret = ueye.is_CaptureVideo(hcam, ueye.IS_DONT_WAIT) # get data from camera and display lineinc = width * int((bitspixel + 7) / 8) for i in xrange(ramp_frames): temp = get_image(width, height) print("Taking image...") # Take the actual image we want to keep camera_capture = get_image(width, height) file = "USBtemp.png"
def save_parameters(self, path): # Save parameters to file pParam = ueye.wchar_p() pParam.value = path err = ueye.is_ParameterSet(self._cam, ueye.IS_PARAMETERSET_CMD_SAVE_FILE, pParam, 0) if err != ueye.IS_SUCCESS: raise CameraException(self._cam, 'ueye>save_parameters>', err)
def update_config_gain(ini_file="/home/oran/Pictures/Settings/default-camera-settings.ini",update = {'green':'10'},set=False): print('updating' + ini_file) config = configparser.ConfigParser() config.sections() config.read(ini_file, encoding='utf-8-sig') if set: for key in update.keys(): config['Gain'][key] = update[key] else: for key in update.keys(): config['Gain'][key] = str(int(config['Gain'][key]) + int(update[key])) # print(config['Gain'][key]) with open(ini_file, 'w') as configfile: config.write(configfile) pParam = ueye.wchar_p() pParam.value = ini_file ueye.is_ParameterSet(1, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0)
def update_config_exposure(ini_file="/home/oran/Desktop/camera_params.ini",update = 0,set=False): config = configparser.ConfigParser() config.sections() config.read(ini_file, encoding='utf-8-sig') if set: config['Timing']['exposure'] = str(update) else: print('exposure set to ' + str(int(config['Timing']['exposure']) + update)) config['Timing']['exposure'] = str(int(config['Timing']['exposure']) + update) with open(ini_file, 'w') as configfile: config.write(configfile) pParam = ueye.wchar_p() pParam.value = ini_file ueye.is_ParameterSet(1, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0) return config['Timing']['exposure']
def load_parameters(self, path): # Load parameters from file # Taken from: https://stackoverflow.com/questions/56461431/error-loading-ueye-camera-configuration-file-with-pyueye pParam = ueye.wchar_p() pParam.value = path err = ueye.is_ParameterSet(self._cam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0) if err != ueye.IS_SUCCESS: raise CameraException(self._cam, 'ueye>load_parameters>', err) # Wait for parameters to take effect time.sleep(5)
def init(self): ret = ueye.is_InitCamera(self.h_cam, None) if ret != ueye.IS_SUCCESS: self.h_cam = None raise uEyeException(ret) check( ueye.is_ParameterSet(self.h_cam, ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM, ueye.INT(0), ueye.INT(0))) return ret
def update_config_gain( ini_file="C:\sandbox\camera_calibration\raw_camera_params.ini", update={'green': '10'}, set=False): config = configparser.ConfigParser() config.sections() config.read(ini_file, encoding='utf-8-sig') if set: for key in update.keys(): config['Gain'][key] = update[key] else: for key in update.keys(): config['Gain'][key] = str( int(config['Gain'][key]) + int(update[key])) # print(config['Gain'][key]) with open(ini_file, 'w') as configfile: config.write(configfile) pParam = ueye.wchar_p() pParam.value = ini_file ueye.is_ParameterSet(1, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0)
def __init__(self, pathToParameterSets, fps): self.hCam = ueye.HIDS(0) self.sInfo = ueye.SENSORINFO() self.cInfo = ueye.CAMINFO() self.pcImageMemory = ueye.c_mem_p() self.MemID = ueye.int() self.rectAOI = ueye.IS_RECT() self.pitch = ueye.INT() self.nBitsPerPixel = ueye.INT(24) self.channels = 3 self.m_nColorMode = ueye.INT() self.bytes_per_pixel = int(self.nBitsPerPixel / 8) self.timeStampsFilePath = "times.txt" self.timeStampsFile = open(self.timeStampsFilePath, 'w') self.FPS = ctypes.c_double(int(fps)) self.pathToParameterSets = pathToParameterSets nRet = ueye.is_InitCamera(self.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(self.hCam, self.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(self.hCam, self.sInfo) if nRet != ueye.IS_SUCCESS: print("is_GetSensorInfo ERROR") if int.from_bytes(self.sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME: # for color camera models use RGB32 mode self.m_nColorMode = ueye.IS_CM_MONO8 self.nBitsPerPixel = ueye.INT(8) self.bytes_per_pixel = int(self.nBitsPerPixel / 8) print("IS_COLORMODE_MONOCHROME: ", self.m_nColorMode) print("\tm_nColorMode: \t\t", self.m_nColorMode) print("\tnBitsPerPixel: \t\t", self.nBitsPerPixel) print("\tbytes_per_pixel: \t\t", self.bytes_per_pixel) print() #loading parameter set file pParam = ueye.wchar_p() pParam.value = self.pathToParameterSets nRet = ueye.is_ParameterSet(self.hCam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0) if nRet != ueye.IS_SUCCESS: print("Setting parameter set error") #setting fps newFPS = ctypes.c_double(0) ueye.is_SetFrameRate(self.hCam, self.FPS, newFPS) if nRet != ueye.IS_SUCCESS: print("Setting fps ERROR") else: print("FPS is: ", newFPS) # Can be used to set the size and position of an "area of interest"(AOI) within an image nRet = ueye.is_AOI(self.hCam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI, ueye.sizeof(self.rectAOI)) if nRet != ueye.IS_SUCCESS: print("is_AOI ERROR") self.width = self.rectAOI.s32Width self.height = self.rectAOI.s32Height # Prints out some information about the camera and the sensor print("Camera model:\t\t", self.sInfo.strSensorName.decode('utf-8')) print("Camera serial no.:\t", self.cInfo.SerNo.decode('utf-8')) print("Maximum image width:\t", self.width) print("Maximum image height:\t", self.height) print()
def set_parameters_from_memory(self): ret = ueye.is_ParameterSet(self.h_cam, ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM, None, 0) return ret
def cameraInit(load_parameters_EEPROM=load_parameters_EEPROM): """Initializes the camera with the correct parameters""" global mBuff, bpp, pitch, channels, bytes_per_pixel, tt hCam = ueye.HIDS(0) # 0: first available camera; 1-254: The camera with the specified camera ID sInfo = ueye.SENSORINFO() cInfo = ueye.CAMINFO() # Starts the driver and establishes the connection to the camera nRet = ueye.is_InitCamera(hCam, None) logger.debug("Setting Camera Data") if nRet != ueye.IS_SUCCESS: logger.info("is_InitCamera ERROR, camera not connected?") return -1, 0 # 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: logger.info("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: logger.info("is_GetSensorInfo ERROR") nRet = ueye.is_ResetToDefault(hCam) if nRet != ueye.IS_SUCCESS: logger.info("is_ResetToDefault ERROR") # Set display mode to DIB nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB) if int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME: # for color camera models use RGB32 mode logger.info("Setting colormode to black and white") m_nColorMode = ueye.IS_CM_MONO8 nBitsPerPixel = ueye.INT(8) bytes_per_pixel = int(nBitsPerPixel / 8) logger.info(f"IS_COLORMODE_MONOCHROME: ") logger.info(f"\tm_nColorMode: \t\t{m_nColorMode}") logger.info(f"\tnBitsPerPixel: \t\t{nBitsPerPixel}") logger.info(f"\tbytes_per_pixel: \t\t {bytes_per_pixel}") if load_parameters_EEPROM: logger.debug("Loading parameters from EEPROM") nullint = ueye._value_cast(0, ueye.ctypes.c_uint) rvv = ueye.is_ParameterSet(hCam, ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM, nullint, nullint) # 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: logger.error("is_AOI ERROR") width = rectAOI.s32Width height = rectAOI.s32Height # Prints out some information about the camera and the sensor logger.info(f"Camera model:\t\t {sInfo.strSensorName.decode('utf-8')}") logger.info(f"Camera serial no.:\t {cInfo.SerNo.decode('utf-8')}") logger.info(f"Maximum image width:\t {width}") logger.info(f"Maximum image height:\t {height}") # --------------------------------------------------------------------------------------------------------------------------------------- # 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: logger.info("is_AllocImageMem ERROR") else: # Makes the specified image memory the active memory nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID) if nRet != ueye.IS_SUCCESS: logger.info("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: logger.info("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: logger.info("is_InquireImageMem ERROR") else: logger.info("Press q to leave the programm") # --------------------------------------------------------------------------------------------------------------------------------------- # shutter = int.from_bytes(sInfo.bGlobShutter.value, byteorder='big') # rvv = ueye.is_ParameterSet(hCam,ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM,nullint,nullint) # Continuous image display tt = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rect_aoi, ueye.sizeof(rect_aoi)) width = rect_aoi.s32Width.value height = rect_aoi.s32Height.value for i in range(numBuffers): buff = ImageBuffer() ueye.is_AllocImageMem(hCam, width, height, bpp, buff.mem_ptr, buff.mem_id) ueye.is_AddToSequence(hCam, buff.mem_ptr, buff.mem_id) buffs.append(buff) rvIQ = ueye.is_InitImageQueue(hCam, 0) nRet = ueye.IS_SUCCESS ret = ueye.is_WaitForNextImage(hCam, 100, mBuff.mem_ptr, mBuff.mem_id) rr = ueye.is_GetActSeqBuf(hCam, buffCurrent.mem_id, buffLast.mem_ptr, buffLast.mem_ptr) if (not ret): # cnt+=1 array = ueye.get_data(mBuff.mem_ptr, width, height, bpp, pitch, copy=True) ueye.is_UnlockSeqBuf(hCam, mBuff.mem_id, mBuff.mem_ptr) return nRet, hCam