IDS_streaming.py 7.3 KB

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