IDS_streaming.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. hCam = ueye.HIDS(0)
  2. sensor_info = ueye.SENSORINFO()
  3. camera_info = ueye.CAMINFO()
  4. pcImageMemory = ueye.c_mem_p()
  5. MemID = ueye.int()
  6. rectAOI = ueye.IS_RECT()
  7. rectAOI.s32X = 0
  8. rectAOI.s32Y = 152
  9. rectAOI.s32Width = 1936
  10. rectAOI.s32Height = 472
  11. pitch = ueye.INT()
  12. nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
  13. channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
  14. m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
  15. bytes_per_pixel = int(nBitsPerPixel / 8)
  16. dZoomValue = ueye.DOUBLE()
  17. fps = ueye.DOUBLE(30) # set you want fps
  18. Real_FPS =ueye.DOUBLE() # get real fps
  19. exposure_value = ueye.DOUBLE(10) # set exposure value
  20. cbSizeOfParam = 8
  21. gpio = ueye.IO_GPIO_CONFIGURATION()
  22. gpio.u32Gpio = ueye.IO_GPIO_1
  23. gpio.u32Configuration = ueye.IS_GPIO_INPUT
  24. gpio.u32State = ueye.UINT()
  25. print("START")
  26. # Starts the driver and establishes the connection to the camera
  27. nRet = ueye.is_InitCamera(hCam, None)
  28. if nRet != ueye.IS_SUCCESS:
  29. print("is_InitCamera ERROR")
  30. nRet = ueye.is_SetFrameRate(hCam,fps,Real_FPS)
  31. #ueye.IS_GET_DEFAULT_FRAMERATE
  32. if nRet != ueye.IS_SUCCESS:
  33. print("is_SetFrameRate ERROR")
  34. nRet = ueye.is_Exposure(hCam,ueye.IS_EXPOSURE_CMD_SET_EXPOSURE,exposure_value,cbSizeOfParam)
  35. if nRet != ueye.IS_SUCCESS:
  36. print("is_Exposure ERROR")
  37. # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
  38. nRet = ueye.is_GetCameraInfo(hCam, camera_info)
  39. if nRet != ueye.IS_SUCCESS:
  40. print("is_GetCameraInfo ERROR")
  41. # You can query additional information about the sensor type used in the camera
  42. nRet = ueye.is_GetSensorInfo(hCam, sensor_info)
  43. if nRet != ueye.IS_SUCCESS:
  44. print("is_GetSensorInfo ERROR")
  45. '''
  46. nRet = ueye.is_ResetToDefault(hCam)
  47. if nRet != ueye.IS_SUCCESS:
  48. print("is_ResetToDefault ERROR")
  49. '''
  50. # Set display mode to DIB
  51. nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
  52. # Set the right color mode
  53. if int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
  54. # setup the color depth to the current windows setting
  55. ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
  56. bytes_per_pixel = int(nBitsPerPixel / 8)
  57. print("IS_COLORMODE_BAYER: ", )
  58. print("\tm_nColorMode:", m_nColorMode)
  59. print("\tnBitsPerPixel:", nBitsPerPixel)
  60. print("\tbytes_per_pixel:", bytes_per_pixel)
  61. elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
  62. # for color camera models use RGB32 mode
  63. m_nColorMode = ueye.IS_CM_BGRA8_PACKED
  64. nBitsPerPixel = ueye.INT(32)
  65. bytes_per_pixel = int(nBitsPerPixel / 8)
  66. print("IS_COLORMODE_CBYCRY: ", )
  67. print("\tm_nColorMode: \t\t", m_nColorMode)
  68. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  69. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  70. elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
  71. # for color camera models use RGB32 mode
  72. m_nColorMode = ueye.IS_CM_MONO8
  73. nBitsPerPixel = ueye.INT(8)
  74. bytes_per_pixel = int(nBitsPerPixel / 8)
  75. print("IS_COLORMODE_MONOCHROME: ", )
  76. print("\tm_nColorMode: \t\t", m_nColorMode)
  77. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  78. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  79. else:
  80. # for monochrome camera models use Y8 mode
  81. m_nColorMode = ueye.IS_CM_MONO8
  82. nBitsPerPixel = ueye.INT(8)
  83. bytes_per_pixel = int(nBitsPerPixel / 8)
  84. print("else")
  85. '''
  86. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_SET_AOI, rectAOI, ueye.sizeof(rectAOI))
  87. if nRet != ueye.IS_SUCCESS:
  88. print("is_AOI ERROR")
  89. # Can be used to set the size and position of an "area of interest"(AOI) within an image
  90. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
  91. if nRet != ueye.IS_SUCCESS:
  92. print("is_AOI ERROR")
  93. width = rectAOI.s32Width
  94. height = rectAOI.s32Height
  95. x = rectAOI.s32X
  96. y = rectAOI.s32Y
  97. '''
  98. # Prints out some information about the camera and the sensor
  99. print("Now FPS :{}".format(fps))
  100. print("Real FPS :{}".format(Real_FPS))
  101. print("Exposure value : {}".format(exposure_value))
  102. print("Maximum image width:", width)
  103. print("Maximum image height:", height)
  104. print("Maximum image x:", x)
  105. print("Maximum image y:", y)
  106. # ---------------------------------------------------------------------------------------------------------------------------------------
  107. # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
  108. nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  109. if nRet != ueye.IS_SUCCESS:
  110. print("is_AllocImageMem ERROR")
  111. else:
  112. # Makes the specified image memory the active memory
  113. nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
  114. if nRet != ueye.IS_SUCCESS:
  115. print("is_SetImageMem ERROR")
  116. else:
  117. # Set the desired color mode
  118. nRet = ueye.is_SetColorMode(hCam, m_nColorMode)
  119. # Activates the camera's live video mode (free run mode)
  120. nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
  121. if nRet != ueye.IS_SUCCESS:
  122. print("is_CaptureVideo ERROR")
  123. # Enables the queue mode for existing image memory sequences
  124. nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
  125. if nRet != ueye.IS_SUCCESS:
  126. print("is_InquireImageMem ERROR")
  127. else:
  128. print("Press q to leave the programm")
  129. # ---------------------------------------------------------------------------------------------------------------------------------------
  130. f = 1
  131. # Continuous image display
  132. while nRet == ueye.IS_SUCCESS:
  133. # In order to display the image in an OpenCV window we need to...
  134. # ...extract the data of our image memory
  135. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  136. # bytes_per_pixel = int(nBitsPerPixel / 8)
  137. # ...reshape it in an numpy array...
  138. frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))
  139. #frame = cv2.resize(frame, (800, 600), fx=0.5, fy=0.5)
  140. cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  141. if cv2.waitKey(1) & 0xFF == ord('p'):
  142. FileParams = ueye.IMAGE_FILE_PARAMS()
  143. FileParams.pwchFileName = "C:\\Users\\User\\Desktop\\IDS\\" + str(f) + ".png"
  144. FileParams.nFileType = ueye.IS_IMG_PNG
  145. FileParams.ppcImageMem = None
  146. FileParams.pnImageID = None
  147. FileParams.nQuality = 75
  148. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
  149. elif cv2.waitKey(1) & 0xFF == ord('q'):
  150. break
  151. # ---------------------------------------------------------------------------------------------------------------------------------------
  152. # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
  153. ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
  154. # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
  155. ueye.is_ExitCamera(hCam)
  156. # Destroys the OpenCv windows
  157. cv2.destroyAllWindows()