def cameraNewFrame(frame, hCam): """Retrieve a new frame from the camera""" # constants ret = 1 cnt = 0 while ret and cnt <= 100: cnt += 1 ret = ueye.is_WaitForNextImage(hCam, timeOutMS, mBuff.mem_ptr, mBuff.mem_id) rr = ueye.is_GetActSeqBuf(hCam, buffCurrent.mem_id, buffLast.mem_ptr, buffLast.mem_ptr) if (not ret): logger.debug(f"ret = {ret}, copying data over to numpy array") fwidth, fheight = frame.shape[1::-1] array = ueye.get_data(mBuff.mem_ptr, fwidth, fheight, bpp, pitch, copy=True) arrayrs = np.reshape(array, (len(array) // fwidth, fwidth)) cwidth, cheight = arrayrs.shape[1::-1] ueye.is_UnlockSeqBuf(hCam, mBuff.mem_id, mBuff.mem_ptr) # bytes_per_pixel = int(nBitsPerPixel / 8) # ...reshape it in an numpy array... # Fill existing buffer with new data # frame = np.reshape(array,(height, width, bytes_per_pixel)) # frame[:] = np.reshape(array,(height, width)) frame[:fheight, :fwidth] = arrayrs[:fheight, :fwidth] return ret, frame else: logger.error("Reading error with new frame ") return ret, frame
def capture_image(cam, gripper_height): camera_height = gripper_height + 70 # Camera is placed 70mm above gripper # TODO: Find a curve that correlates distance from subject and focus value if camera_height > 300: nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_MANUAL_FOCUS, config.focus_overview, ueye.sizeof(config.focus_overview)) else: nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_MANUAL_FOCUS, config.focus_closeup, ueye.sizeof(config.focus_closeup)) #nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS_ONCE, None, 0) # autofocus_status = ueye.INT(0) # ueye.is_Focus(cam.handle(), ueye.FOC_CMD_GET_AUTOFOCUS_STATUS, autofocus_status, ueye.sizeof(autofocus_status)) img_buffer = ImageBuffer() # Create image_tools buffer #ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS_ONCE, None, 0) time.sleep(0.5) # cam.freeze_video(True) # Freeze video captures a single image_tools nRet = ueye.is_WaitForNextImage(cam.handle(), 1000, img_buffer.mem_ptr, img_buffer.mem_id) img_data = ImageData(cam.handle(), img_buffer) array = img_data.as_1d_image() img_data.unlock() return array
def run(self): while self.running: img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.cam.handle(), self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: self.notify(ImageData(self.cam.handle(), img_buffer))
def run(self): while self.running: img_buffer = ImageBuffer() #ueye.is_Focus(self.cam.handle(), ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS_ONCE, None, 0) ret = ueye.is_WaitForNextImage(self.cam.handle(), self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: self.notify(ImageData(self.cam.handle(), img_buffer))
def run(self): while self.running: img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.cam.handle(), self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: image_data = ImageData(self.cam.handle(), img_buffer, copy_=self.copy) self.notify(image_data) image_data.unlock()
def waitForNextFrame(self): self.timeout = 1000 self.img_buffer = self.ImageBuffer() #self.frame_buffer[-1] isWaiting = ueye.is_WaitForNextImage(self.cam, self.timeout, self.img_buffer.mem_ptr, self.img_buffer.mem_id) print('WAIT FOR NEXT IMG: {0}, IS_SUCCESS: {1}'.format( isWaiting, ueye.IS_SUCCESS)) if isWaiting == ueye.IS_SUCCESS: return True else: return False
def run(self): while self.running: img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.cam.handle(), self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: self.notify(ImageData(self.cam.handle(), img_buffer)) t = timeit.default_timer() fps = 1 / (t - self.t_old) print(fps, ret) self.t_old = t
def showVideo(cam): nRet = ueye.is_CaptureVideo(cam.handle(), ueye.IS_DONT_WAIT) img_buffer = ImageBuffer() img_data = ImageData(cam.handle(), img_buffer) while True: nRet = ueye.is_WaitForNextImage(cam.handle(), 1000, img_buffer.mem_ptr, img_buffer.mem_id) array = img_data.as_1d_image() #scanned_img = QR_Scanner_visualized(array) cv2.imshow("hei", array) img_data.unlock() if cv2.waitKey(10) & 0xFF == ord('q'): break
def capture_image(self, timeout=None): if timeout is None: timeout = self.__get_timeout() self.capture_video() img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.handle(), timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: imdata = ImageData(self.handle(), img_buffer) data = imdata.as_1d_image() imdata.unlock() self.stop_video() else: data = None return data
def run(self): if not os.path.isdir("/home/pi/swim4all/Tirocinio/Photo/" + str(self.x.day) + "-" + str(self.x.month) + "-" + str(self.x.year) + "/"): os.makedirs("/home/pi/swim4all/Tirocinio/Photo/" + str(self.x.day) + "-" + str(self.x.month) + "-" + str(self.x.year) + "/") tempo_conteggio = time.time() count = 0 while self.isRunning: t_old = time.time() img_buffer = ImageBuffer() self.cam.nRet = ueye.is_WaitForNextImage(self.cam.cam, self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if self.cam.nRet == ueye.IS_SUCCESS: mem_info = MemoryInfo(self.cam.cam, img_buffer) array = ueye.get_data(img_buffer.mem_ptr, mem_info.width, mem_info.height, mem_info.bits, mem_info.pitch, copy=True) self.cam.unlock_seq(img_buffer.mem_id, img_buffer.mem_ptr) if self.cam.mode_filename == 1: filename = "/home/pi/swim4all/Tirocinio/Photo/" + str(self.x.day) + "-" + str(self.x.month) + "-" + str(self.x.year) + "/" + str(self.cam.camID) + "-" + str(time.time()) + ".png" else: filename = "/home/pi/swim4all/Tirocinio/Photo/" + str(self.x.day) + "-" + str(self.x.month) + "-" + str(self.x.year) + "/" + str(time.time()) + "-" + str(self.cam.camID) + ".png" self.file_param.pwchFileName = filename self.file_param.nFiletype = ueye.IS_IMG_PNG self.file_param.ppcImageMem = None self.file_param.pnImageId = None nRet = ueye.is_ImageFile(self.cam.cam, ueye.IS_IMAGE_FILE_CMD_SAVE, self.file_param, ueye.sizeof(self.file_param)) if nRet != ueye.IS_SUCCESS: error_log(nRet, "is_ImageFile") if not self.file.closed: self.file.write("FPS: " + "Salvataggio non riuscito" + "\n") else: t = time.time() self.FPS = 1 / (t - t_old) if not self.file.closed: self.file.write("FPS: " + str(self.FPS) + "\n") else: error_log(self.cam.nRet, "is_WaitForNextImage") if not self.file.closed: self.file.write("FPS: " + "Frame perso" + "\n") count += 1 if count == 100: tempo_fine_conteggio = time.time() print("FPS: ", count / (tempo_fine_conteggio - tempo_conteggio)) print()
def take_picture(self): """ returns image as numpy array """ if self.cam.capture_video(): raise Exception("Can't start image capture") img_buffer = pyueye_utils.ImageBuffer() if ueye.is_WaitForNextImage(self.cam.handle(), 1000, img_buffer.mem_ptr, img_buffer.mem_id): raise Exception('Timeout on capture') image_data = pyueye_utils.ImageData(self.cam.handle(), img_buffer) img = image_data.as_1d_image() image_data.unlock() self.cam.stop_video() return img
def run(self): count = 0 while count < self.capture_time: img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.cam.handle(), self.timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: if self.notify(ImageData(self.cam.handle(), img_buffer)): break t = timeit.default_timer() fps = 1 / (t - self.t_old) #print(fps, ret) self.t_old = t count += 1 return self.rows
def capture_images(self, nmb, timeout=None): if timeout is None: timeout = self.__get_timeout() self.capture_video() ims = [] for i in range(nmb): img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(self.handle(), timeout, img_buffer.mem_ptr, img_buffer.mem_id) if ret == ueye.IS_SUCCESS: imdata = ImageData(self.handle(), img_buffer) ims.append(imdata.as_1d_image()) imdata.unlock() else: print(f"Warning: Missed {i}th frame !") ims.append(None) self.stop_video() return ims
def _poll_buffer(self): data = ueye.c_mem_p() buffer_id = ueye.int() try: self.check_success( ueye.is_WaitForNextImage(self.h, 1000, data, buffer_id)) self.check_success( ueye.is_CopyImageMem( self.h, data, buffer_id, self.transfer_buffer.ctypes.data_as( ctypes.POINTER(ctypes.c_uint8)))) except RuntimeError: try: self.check_success( ueye.is_UnlockSeqBuf(self.h, ueye.IS_IGNORE_PARAMETER, data)) except: pass finally: return if self.n_accum_current == 0: self.accum_buffer[:] = self.transfer_buffer else: self.accum_buffer[:] = self.accum_buffer + self.transfer_buffer self.n_accum_current += 1 self.check_success( ueye.is_UnlockSeqBuf(self.h, ueye.IS_IGNORE_PARAMETER, data)) if self.n_accum_current >= self.n_accum: self.full_buffers.put(self.accum_buffer) self.accum_buffer = self.free_buffers.get() self.n_accum_current = 0 self.n_full += 1
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
ueye.sizeof(formatID)) cam.alloc() # Allocate image_tools memory # Disable auto exposure dblEnable = ueye.DOUBLE(0) dblDummy = ueye.DOUBLE(0) ueye.is_SetAutoParameter(cam.handle(), ueye.IS_SET_ENABLE_AUTO_SENSOR_GAIN_SHUTTER, dblEnable, dblDummy) newExposure = ueye.DOUBLE(30) ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, newExposure, ueye.sizeof(newExposure)) ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_DISABLE_AUTOFOCUS, None, 0) # Disable autofocus nRet = ueye.is_CaptureVideo(cam.handle(), ueye.IS_WAIT) while True: img_buffer = ImageBuffer() nRet = ueye.is_WaitForNextImage(cam.handle(), 1000, img_buffer.mem_ptr, img_buffer.mem_id) img_data = ImageData(cam.handle(), img_buffer) array = img_data.as_1d_image() # scanned_img = QR_Scanner_visualized(array) cv2.imshow("hei", array) img_data.unlock() if cv2.waitKey(10) & 0xFF == ord('q'): break
def main(): # we need a QApplication, that runs our QT Gui Framework ##app = PyuEyeQtApp() # a basic qt window #view = PyuEyeQtView() #view.show() #view.user_callback = process_image # camera class to simplify uEye API access cam = Camera(1) cam.init() cam.set_colormode(ueye.IS_CM_BGR8_PACKED) #ret = cam.set_exposure(30.0) #ret = cam.set_LUT() # width, height = cam.get_size() x = 0 y = 0 ret = cam.set_aoi(x, y, width, height) cam.alloc(buffer_count=100) ret = cam.capture_video(True) # start to capture; fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('_output.avi', fourcc, 20.0, (width, height)) # a thread that waits for new images and processes all connected views #thread = FrameThread(cam) #thread.start() running = True img_buffer = ImageBuffer() DATA = ImageData(cam.handle(), img_buffer) while (ret == 0) and running: #img_buffer = ImageBuffer() ret = ueye.is_WaitForNextImage(cam.handle(), 1000, img_buffer.mem_ptr, img_buffer.mem_id) #print(img_buffer.mem_id) if ret == ueye.IS_SUCCESS: #DATA = ImageData(cam.handle(), img_buffer) DATA.getdata(img_buffer) #print(DATA.array.shape) image = DATA.as_1d_image() DATA.unlock() # make a gray image #imag = cv2.cvtColor(image, cv2.COLOR_BGR) cv2.imshow("Simple_black", image) out.write(image) if cv2.waitKey(1) == 27: break #cv2.Mat(ImageData.array) # cleanup #app.exit_connect(thread.stop) #app.exec_() #thread.stop() #thread.join() cam.stop_video() cam.exit() cv2.destroyAllWindows() out.release()