IDS_default.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #===========================================================================#
  2. # #
  3. # Copyright (C) 2006 - 2018 #
  4. # IDS Imaging Development Systems GmbH #
  5. # Dimbacher Str. 6-8 #
  6. # D-74182 Obersulm, Germany #
  7. # #
  8. # The information in this document is subject to change without notice #
  9. # and should not be construed as a commitment by IDS Imaging Development #
  10. # Systems GmbH. IDS Imaging Development Systems GmbH does not assume any #
  11. # responsibility for any errors that may appear in this document. #
  12. # #
  13. # This document, or source code, is provided solely as an example #
  14. # of how to utilize IDS software libraries in a sample application. #
  15. # IDS Imaging Development Systems GmbH does not assume any responsibility #
  16. # for the use or reliability of any portion of this document or the #
  17. # described software. #
  18. # #
  19. # General permission to copy or modify, but not for profit, is hereby #
  20. # granted, provided that the above copyright notice is included and #
  21. # reference made to the fact that reproduction privileges were granted #
  22. # by IDS Imaging Development Systems GmbH. #
  23. # #
  24. # IDS Imaging Development Systems GmbH cannot assume any responsibility #
  25. # for the use or misuse of any portion of this software for other than #
  26. # its intended diagnostic purpose in calibrating and testing IDS #
  27. # manufactured cameras and software. #
  28. # #
  29. #===========================================================================#
  30. # Developer Note: I tried to let it as simple as possible.
  31. # Therefore there are no functions asking for the newest driver software or freeing memory beforehand, etc.
  32. # The sole purpose of this program is to show one of the simplest ways to interact with an IDS camera via the uEye API.
  33. # (XS cameras are not supported)
  34. #---------------------------------------------------------------------------------------------------------------------------------------
  35. #Libraries
  36. from pyueye import ueye
  37. import numpy as np
  38. import cv2
  39. import sys
  40. #---------------------------------------------------------------------------------------------------------------------------------------
  41. #Variables
  42. hCam = ueye.HIDS(0) #0: first available camera; 1-254: The camera with the specified camera ID
  43. sInfo = ueye.SENSORINFO()
  44. cInfo = ueye.CAMINFO()
  45. pcImageMemory = ueye.c_mem_p()
  46. MemID = ueye.int()
  47. rectAOI = ueye.IS_RECT()
  48. pitch = ueye.INT()
  49. nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
  50. channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
  51. m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
  52. bytes_per_pixel = int(nBitsPerPixel / 8)
  53. #---------------------
  54. #---------------------------------------------------------------------------------------------------------------------------------------
  55. print("START")
  56. print()
  57. # Starts the driver and establishes the connection to the camera
  58. nRet = ueye.is_InitCamera(hCam, None)
  59. if nRet != ueye.IS_SUCCESS:
  60. print("is_InitCamera ERROR")
  61. ueye.is_ParameterSet(hCam,ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM,None,0)
  62. # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
  63. nRet = ueye.is_GetCameraInfo(hCam, cInfo)
  64. if nRet != ueye.IS_SUCCESS:
  65. print("is_GetCameraInfo ERROR")
  66. # You can query additional information about the sensor type used in the camera
  67. nRet = ueye.is_GetSensorInfo(hCam, sInfo)
  68. if nRet != ueye.IS_SUCCESS:
  69. print("is_GetSensorInfo ERROR")
  70. # Set display mode to DIB
  71. nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
  72. # Set the right color mode
  73. if int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
  74. # setup the color depth to the current windows setting
  75. ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
  76. bytes_per_pixel = int(nBitsPerPixel / 8)
  77. print("IS_COLORMODE_BAYER: ", )
  78. print("\tm_nColorMode: \t\t", m_nColorMode)
  79. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  80. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  81. print()
  82. elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
  83. # for color camera models use RGB32 mode
  84. m_nColorMode = ueye.IS_CM_BGRA8_PACKED
  85. nBitsPerPixel = ueye.INT(32)
  86. bytes_per_pixel = int(nBitsPerPixel / 8)
  87. print("IS_COLORMODE_CBYCRY: ", )
  88. print("\tm_nColorMode: \t\t", m_nColorMode)
  89. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  90. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  91. print()
  92. elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
  93. # for color camera models use RGB32 mode
  94. m_nColorMode = ueye.IS_CM_MONO8
  95. nBitsPerPixel = ueye.INT(8)
  96. bytes_per_pixel = int(nBitsPerPixel / 8)
  97. print("IS_COLORMODE_MONOCHROME: ", )
  98. print("\tm_nColorMode: \t\t", m_nColorMode)
  99. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  100. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  101. print()
  102. else:
  103. # for monochrome camera models use Y8 mode
  104. m_nColorMode = ueye.IS_CM_MONO8
  105. nBitsPerPixel = ueye.INT(8)
  106. bytes_per_pixel = int(nBitsPerPixel / 8)
  107. print("else")
  108. # Can be used to set the size and position of an "area of interest"(AOI) within an image
  109. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
  110. if nRet != ueye.IS_SUCCESS:
  111. print("is_AOI ERROR")
  112. width = rectAOI.s32Width
  113. height = rectAOI.s32Height
  114. # Prints out some information about the camera and the sensor
  115. print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
  116. print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
  117. print("Maximum image width:\t", width)
  118. print("Maximum image height:\t", height)
  119. print()
  120. #---------------------------------------------------------------------------------------------------------------------------------------
  121. #for i in range(1,10):
  122. # nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  123. # ueye.is_AddToSequence(hCam,pcImageMemory,MemID)
  124. # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
  125. nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  126. if nRet != ueye.IS_SUCCESS:
  127. print("is_AllocImageMem ERROR")
  128. else:
  129. # Makes the specified image memory the active memory
  130. nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
  131. if nRet != ueye.IS_SUCCESS:
  132. print("is_SetImageMem ERROR")
  133. else:
  134. # Set the desired color mode
  135. nRet = ueye.is_SetColorMode(hCam, m_nColorMode)
  136. # Activates the camera's live video mode (free run mode)
  137. nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
  138. if nRet != ueye.IS_SUCCESS:
  139. print("is_CaptureVideo ERROR")
  140. # Enables the queue mode for existing image memory sequences
  141. nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
  142. if nRet != ueye.IS_SUCCESS:
  143. print("is_InquireImageMem ERROR")
  144. else:
  145. print("Press q to leave the programm")
  146. #---------------------------------------------------------------------------------------------------------------------------------------
  147. pic = 0
  148. # Continuous image display
  149. while(nRet == ueye.IS_SUCCESS):
  150. # In order to display the image in an OpenCV window we need to...
  151. # ...extract the data of our image memory
  152. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  153. # bytes_per_pixel = int(nBitsPerPixel / 8)
  154. # ...reshape it in an numpy array...
  155. frame = np.reshape(array,(height.value, width.value, bytes_per_pixel))
  156. # ...resize the image by a half #frame後面的(800,600可以自行調整成想要的大小
  157. frame = cv2.resize(frame,(800,600),fx=0.5, fy=0.5)
  158. FileParams = ueye.IMAGE_FILE_PARAMS()
  159. FileParams.pwchFileName = "C:\\Users\\User\\Desktop\\test\\1.png"
  160. FileParams.nFileType = ueye.IS_IMG_PNG
  161. FileParams.ppcImageMem = None
  162. FileParams.pnImageID = None
  163. FileParams.nQuality = 75
  164. #---------------------------------------------------------------------------------------------------------------------------------------
  165. #Include image data processing here
  166. #---------------------------------------------------------------------------------------------------------------------------------------
  167. #...and finally display it
  168. cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  169. # Press q if you want to end the loop
  170. if cv2.waitKey(1) & 0xFF == ord('p'):
  171. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
  172. print(FileParams.pwchFileName)
  173. pic = pic + 1
  174. print(pic)
  175. print(nRet)
  176. elif cv2.waitKey(1) & 0xFF == ord('q'):
  177. break
  178. #---------------------------------------------------------------------------------------------------------------------------------------
  179. # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
  180. ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
  181. # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
  182. ueye.is_ExitCamera(hCam)
  183. # Destroys the OpenCv windows
  184. cv2.destroyAllWindows()