IDS_ringbuffer.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 ctypes import *
  37. from pyueye import ueye
  38. import numpy as np
  39. import cv2
  40. import sys
  41. import ctypes
  42. import struct
  43. import threading
  44. import time
  45. import datetime
  46. #---------------------------------------------------------------------------------------------------------------------------------------
  47. #Variables
  48. #start1 = datetime.datetime.now()
  49. hCam = ueye.HIDS(0) #0: first available camera; 1-254: The camera with the specified camera ID
  50. sInfo = ueye.SENSORINFO()
  51. cInfo = ueye.CAMINFO()
  52. pcImageMemory = ueye.c_mem_p()
  53. MemID = ueye.int()
  54. rectAOI = ueye.IS_RECT()
  55. pitch = ueye.INT()
  56. nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
  57. channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
  58. m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
  59. bytes_per_pixel = int(nBitsPerPixel / 8)
  60. formatInfo=ueye.IMAGE_FORMAT_INFO()
  61. now=ctypes.c_uint()
  62. m_pcSeqImgMem=[]
  63. m_lSeqMemId=[]
  64. nNum=ueye.INT()
  65. pcMem=ueye.c_mem_p()
  66. pcMemLast=ueye.c_mem_p()
  67. ImageFileParams=ueye.IMAGE_FILE_PARAMS()
  68. #---------------------------------------------------------------------------------------------------------------------------------------
  69. print("START")
  70. #print()
  71. # Starts the driver and establishes the connection to the camera
  72. nRet = ueye.is_InitCamera(hCam, None)
  73. if nRet != ueye.IS_SUCCESS:
  74. print("is_InitCamera ERROR")
  75. ueye.is_ParameterSet(hCam,ueye.IS_PARAMETERSET_CMD_LOAD_EEPROM,None,0)
  76. # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
  77. nRet = ueye.is_GetCameraInfo(hCam, cInfo)
  78. if nRet != ueye.IS_SUCCESS:
  79. print("is_GetCameraInfo ERROR")
  80. # You can query additional information about the sensor type used in the camera
  81. nRet = ueye.is_GetSensorInfo(hCam, sInfo)
  82. if nRet != ueye.IS_SUCCESS:
  83. print("is_GetSensorInfo ERROR")
  84. # Set display mode to DIB
  85. nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
  86. # Set the right color mode
  87. if int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
  88. # setup the color depth to the current windows setting
  89. ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
  90. bytes_per_pixel = int(nBitsPerPixel / 8)
  91. #print("IS_COLORMODE_BAYER: ", )
  92. #print("\tm_nColorMode: \t\t", m_nColorMode)
  93. #print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  94. #print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  95. #print()
  96. elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
  97. # for color camera models use RGB32 mode
  98. m_nColorMode = ueye.IS_CM_BGR8_PACKED
  99. nBitsPerPixel = ueye.INT(24)
  100. bytes_per_pixel = int(nBitsPerPixel / 8)
  101. #print("IS_COLORMODE_CBYCRY: ", )
  102. #print("\tm_nColorMode: \t\t", m_nColorMode)
  103. #print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  104. #print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  105. #print()
  106. elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
  107. # for color camera models use RGB32 mode
  108. m_nColorMode = ueye.IS_CM_MONO8
  109. nBitsPerPixel = ueye.INT(8)
  110. bytes_per_pixel = int(nBitsPerPixel / 8)
  111. #print("IS_COLORMODE_MONOCHROME: ", )
  112. #print("\tm_nColorMode: \t\t", m_nColorMode)
  113. #print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  114. #print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  115. #print()
  116. else:
  117. # for monochrome camera models use Y8 mode
  118. m_nColorMode = ueye.IS_CM_MONO8
  119. nBitsPerPixel = ueye.INT(8)
  120. bytes_per_pixel = int(nBitsPerPixel / 8)
  121. print("else")
  122. ueye.is_SetColorMode(hCam,ueye.IS_CM_BGRA8_PACKED)
  123. nBitsPerPixel=ueye.INT(32)
  124. # Can be used to set the size and position of an "area of interest"(AOI) within an image
  125. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
  126. if nRet != ueye.IS_SUCCESS:
  127. print("is_AOI ERROR")
  128. width =rectAOI.s32Width
  129. height =rectAOI.s32Height
  130. # Prints out some information about the camera and the sensor
  131. #print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
  132. #print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
  133. #print("Maximum image width:\t", width)
  134. #print("Maximum image height:\t", height)
  135. #print()
  136. #---------------------------------------------------------------------------------------------------------------------------------------
  137. for i in range(1,5):
  138. # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
  139. nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  140. # m_pcSeqImgMem.append(pcImageMemory)
  141. # m_lSeqMemId.append(MemID)
  142. ueye.is_AddToSequence(hCam,pcImageMemory,MemID)
  143. #path=ueye.c_wchar_p()
  144. #print(type(path))
  145. #nRet = ueye.is_ParameterSet(hCam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, path, 0)
  146. # Activates the camera's live video mode (free run mode)
  147. nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
  148. if nRet != ueye.IS_SUCCESS:
  149. print("is_CaptureVideo ERROR")
  150. nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
  151. if nRet != ueye.IS_SUCCESS:
  152. print("is_InquireImageMem ERROR")
  153. else:
  154. print("Press q to leave the programm")
  155. Count=0
  156. while(nRet == ueye.IS_SUCCESS):
  157. ueye.is_GetActSeqBuf(hCam, nNum, pcMem, pcMemLast)
  158. array = ueye.get_data(pcMemLast, width, height, nBitsPerPixel, pitch, copy=False)
  159. bytes_per_pixel = int(nBitsPerPixel / 8)
  160. # ...reshape it in an numpy array...
  161. frame = np.reshape(array,(height.value, width.value, bytes_per_pixel))
  162. # ...resize the image by a half
  163. frame = cv2.resize(frame,(0,0),fx=0.5, fy=0.5)
  164. #---------------------------------------------------------------------------------------------------------------------------------------
  165. #Include image data processing here
  166. Count = Count + 1
  167. '''
  168. if cv2.waitKey(2) & 0xFF == ord('p'):
  169. Count=Count+1
  170. ImageFileParams.pwchFileName = ueye.c_wchar_p('C:\\Users\\User\\Desktop\\test\\1.png')
  171. #ImageFileParams.pnImageID = &nID;
  172. #ImageFileParams.ppcImageMem = &pcMemory;
  173. ImageFileParams.nFileType = ueye.IS_IMG_PNG
  174. #ImageFileParams.ppcImageMem = pcMemLast;
  175. ImageFileParams.nQuality = 75
  176. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, ImageFileParams, ueye.sizeof(ImageFileParams));
  177. print(Count)
  178. print(nRet)
  179. '''
  180. #---------------------------------------------------------------------------------------------------------------------------------------
  181. #...and finally display it
  182. cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  183. time.sleep(0.5)
  184. ImageFileParams.pwchFileName = ueye.c_wchar_p('C:\\Users\\User\\Desktop\\tfcoffebean\\test\\1.png')
  185. ImageFileParams.nFileType = ueye.IS_IMG_PNG
  186. ImageFileParams.nQuality = 75
  187. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, ImageFileParams, ueye.sizeof(ImageFileParams));
  188. print(Count)
  189. print(nRet)
  190. # Press q if you want to end the loop
  191. #if cv2.waitKey(1) & 0xFF == ord('q'):
  192. #break
  193. #---------------------------------------------------------------------------------------------------------------------------------------
  194. # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
  195. ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
  196. # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
  197. ueye.is_ExitCamera(hCam)
  198. # Destroys the OpenCv windows
  199. cv2.destroyAllWindows()
  200. #print()
  201. print("END")
  202. #end1 = datetime.datetime.now()
  203. #print("完整執行時間:", end1 - start1)