tisgrabber.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Nov 21 09:44:40 2016
  4. @author: Daniel Vassmer, Stefan_Geissler
  5. """
  6. from enum import Enum
  7. import ctypes as C
  8. import os
  9. import sys
  10. import numpy as np
  11. class SinkFormats(Enum):
  12. Y800 = 0
  13. RGB24 = 1
  14. RGB32 = 2
  15. UYVY = 3
  16. Y16 = 4
  17. ImageFileTypes = {'BMP':0, 'JPEG':1}
  18. class GrabberHandle(C.Structure):
  19. pass
  20. GrabberHandle._fields_ = [('unused', C.c_int)]
  21. class TIS_GrabberDLL(object):
  22. if sys.maxsize > 2**32 :
  23. __tisgrabber = C.windll.LoadLibrary("dlls\\tisgrabber_x64.dll")
  24. else:
  25. __tisgrabber = C.windll.LoadLibrary("dlls\\tisgrabber.dll")
  26. def __init__(self, **keyargs):
  27. """Initialize the Albatross from the keyword arguments."""
  28. self.__dict__.update(keyargs)
  29. GrabberHandlePtr = C.POINTER(GrabberHandle)
  30. # Initialize the ICImagingControl class library. This function must be called
  31. # only once before any other functions of this library are called.
  32. # @param szLicenseKey IC Imaging Control license key or NULL if only a trial version is available.
  33. # @retval IC_SUCCESS on success.
  34. # @retval IC_ERROR on wrong license key or other errors.
  35. # @sa IC_CloseLibrary
  36. InitLibrary = __tisgrabber.IC_InitLibrary(None)
  37. # Get the number of the currently available devices. This function creates an
  38. # internal array of all connected video capture devices. With each call to this
  39. # function, this array is rebuild. The name and the unique name can be retrieved
  40. # from the internal array using the functions IC_GetDevice() and IC_GetUniqueNamefromList.
  41. # They are usefull for retrieving device names for opening devices.
  42. #
  43. # @retval >= 0 Success, count of found devices.
  44. # @retval IC_NO_HANDLE Internal Error.
  45. #
  46. # @sa IC_GetDevice
  47. # @sa IC_GetUniqueNamefromList
  48. get_devicecount = __tisgrabber.IC_GetDeviceCount
  49. get_devicecount.restype = C.c_int
  50. get_devicecount.argtypes = None
  51. # Get unique device name of a device specified by iIndex. The unique device name
  52. # consist from the device name and its serial number. It allows to differ between
  53. # more then one device of the same type connected to the computer. The unique device name
  54. # is passed to the function IC_OpenDevByUniqueName
  55. #
  56. # @param iIndex The number of the device whose name is to be returned. It must be
  57. # in the range from 0 to IC_GetDeviceCount(),
  58. # @return Returns the string representation of the device on success, NULL
  59. # otherwise.
  60. #
  61. # @sa IC_GetDeviceCount
  62. # @sa IC_GetUniqueNamefromList
  63. # @sa IC_OpenDevByUniqueName
  64. get_unique_name_from_list = __tisgrabber.IC_GetUniqueNamefromList
  65. get_unique_name_from_list.restype = C.c_char_p
  66. get_unique_name_from_list.argtypes = (C.c_int,)
  67. # Creates a new grabber handle and returns it. A new created grabber should be
  68. # release with a call to IC_ReleaseGrabber if it is no longer needed.
  69. # @sa IC_ReleaseGrabber
  70. create_grabber = __tisgrabber.IC_CreateGrabber
  71. create_grabber.restype = GrabberHandlePtr
  72. create_grabber.argtypes = None
  73. # Open a video capture by using its UniqueName. Use IC_GetUniqueName() to
  74. # retrieve the unique name of a camera.
  75. #
  76. # @param hGrabber Handle to a grabber object
  77. # @param szDisplayName Memory that will take the display name.
  78. #
  79. # @sa IC_GetUniqueName
  80. # @sa IC_ReleaseGrabber
  81. open_device_by_unique_name = __tisgrabber.IC_OpenDevByUniqueName
  82. open_device_by_unique_name.restype = C.c_int
  83. open_device_by_unique_name.argtypes = (GrabberHandlePtr,
  84. C.c_char_p)
  85. set_videoformat = __tisgrabber.IC_SetVideoFormat
  86. set_videoformat.restype = C.c_int
  87. set_videoformat.argtypes = (GrabberHandlePtr,
  88. C.c_char_p)
  89. set_framerate = __tisgrabber.IC_SetFrameRate
  90. set_framerate.restype = C.c_int
  91. set_framerate.argtypes = (GrabberHandlePtr,
  92. C.c_float)
  93. # Returns the width of the video format.
  94. get_video_format_width = __tisgrabber.IC_GetVideoFormatWidth
  95. get_video_format_width.restype = C.c_int
  96. get_video_format_width.argtypes = (GrabberHandlePtr,)
  97. # returns the height of the video format.
  98. get_video_format_height = __tisgrabber.IC_GetVideoFormatHeight
  99. get_video_format_height.restype = C.c_int
  100. get_video_format_height.argtypes = (GrabberHandlePtr,)
  101. # Get the number of the available video formats for the current device.
  102. # A video capture device must have been opened before this call.
  103. #
  104. # @param hGrabber The handle to the grabber object.
  105. #
  106. # @retval >= 0 Success
  107. # @retval IC_NO_DEVICE No video capture device selected.
  108. # @retval IC_NO_HANDLE No handle to the grabber object.
  109. #
  110. # @sa IC_GetVideoFormat
  111. GetVideoFormatCount = __tisgrabber.IC_GetVideoFormatCount
  112. GetVideoFormatCount.restype = C.c_int
  113. GetVideoFormatCount.argtypes = (GrabberHandlePtr,)
  114. # Get a string representation of the video format specified by iIndex.
  115. # iIndex must be between 0 and IC_GetVideoFormatCount().
  116. # IC_GetVideoFormatCount() must have been called before this function,
  117. # otherwise it will always fail.
  118. #
  119. # @param hGrabber The handle to the grabber object.
  120. # @param iIndex Number of the video format to be used.
  121. #
  122. # @retval Nonnull The name of the specified video format.
  123. # @retval NULL An error occured.
  124. # @sa IC_GetVideoFormatCount
  125. GetVideoFormat = __tisgrabber.IC_GetVideoFormat
  126. GetVideoFormat.restype = C.c_char_p
  127. GetVideoFormat.argtypes = (GrabberHandlePtr,
  128. C.c_int,)
  129. # Get the number of the available input channels for the current device.
  130. # A video capture device must have been opened before this call.
  131. #
  132. # @param hGrabber The handle to the grabber object.
  133. #
  134. # @retval >= 0 Success
  135. # @retval IC_NO_DEVICE No video capture device selected.
  136. # @retval IC_NO_HANDLE No handle to the grabber object.
  137. #
  138. # @sa IC_GetInputChannel
  139. GetInputChannelCount = __tisgrabber.IC_GetInputChannelCount
  140. GetInputChannelCount.restype = C.c_int
  141. GetInputChannelCount.argtypes = (GrabberHandlePtr,)
  142. # Get a string representation of the input channel specified by iIndex.
  143. # iIndex must be between 0 and IC_GetInputChannelCount().
  144. # IC_GetInputChannelCount() must have been called before this function,
  145. # otherwise it will always fail.
  146. # @param hGrabber The handle to the grabber object.
  147. # @param iIndex Number of the input channel to be used..
  148. #
  149. # @retval Nonnull The name of the specified input channel
  150. # @retval NULL An error occured.
  151. # @sa IC_GetInputChannelCount
  152. GetInputChannel = __tisgrabber.IC_GetInputChannel
  153. GetInputChannel.restype = C.c_char_p
  154. GetInputChannel.argtypes = (GrabberHandlePtr,
  155. C.c_int,)
  156. # Get the number of the available video norms for the current device.
  157. # A video capture device must have been opened before this call.
  158. #
  159. # @param hGrabber The handle to the grabber object.
  160. #
  161. # @retval >= 0 Success
  162. # @retval IC_NO_DEVICE No video capture device selected.
  163. # @retval IC_NO_HANDLE No handle to the grabber object.
  164. #
  165. # @sa IC_GetVideoNorm
  166. GetVideoNormCount = __tisgrabber.IC_GetVideoNormCount
  167. GetVideoNormCount.restype = C.c_int
  168. GetVideoNormCount.argtypes = (GrabberHandlePtr,)
  169. # Get a string representation of the video norm specified by iIndex.
  170. # iIndex must be between 0 and IC_GetVideoNormCount().
  171. # IC_GetVideoNormCount() must have been called before this function,
  172. # otherwise it will always fail.
  173. #
  174. # @param hGrabber The handle to the grabber object.
  175. # @param iIndex Number of the video norm to be used.
  176. #
  177. # @retval Nonnull The name of the specified video norm.
  178. # @retval NULL An error occured.
  179. # @sa IC_GetVideoNormCount
  180. GetVideoNorm = __tisgrabber.IC_GetVideoNorm
  181. GetVideoNorm.restype = C.c_char_p
  182. GetVideoNorm.argtypes = (GrabberHandlePtr,
  183. C.c_int,)
  184. SetFormat = __tisgrabber.IC_SetFormat
  185. SetFormat.restype = C.c_int
  186. SetFormat.argtypes = (GrabberHandlePtr,
  187. C.c_int,)
  188. GetFormat = __tisgrabber.IC_GetFormat
  189. GetFormat.restype = C.c_int
  190. GetFormat.argtypes = (GrabberHandlePtr,)
  191. # Start the live video.
  192. # @param hGrabber The handle to the grabber object.
  193. # @param iShow The parameter indicates: @li 1 : Show the video @li 0 : Do not show the video, but deliver frames. (For callbacks etc.)
  194. # @retval IC_SUCCESS on success
  195. # @retval IC_ERROR if something went wrong.
  196. # @sa IC_StopLive
  197. StartLive = __tisgrabber.IC_StartLive
  198. StartLive.restype = C.c_int
  199. StartLive.argtypes = (GrabberHandlePtr,
  200. C.c_int,)
  201. StopLive = __tisgrabber.IC_StopLive
  202. StopLive.restype = C.c_int
  203. StopLive.argtypes = (GrabberHandlePtr,)
  204. SetHWND = __tisgrabber.IC_SetHWnd
  205. SetHWND.restype = C.c_int
  206. SetHWND.argtypes = (GrabberHandlePtr,
  207. C.c_int,)
  208. # Snaps an image. The video capture device must be set to live mode and a
  209. # sink type has to be set before this call. The format of the snapped images depend on
  210. # the selected sink type.
  211. #
  212. # @param hGrabber The handle to the grabber object.
  213. # @param iTimeOutMillisek The Timeout time is passed in milli seconds. A value of -1 indicates, that
  214. # no time out is set.
  215. #
  216. #
  217. # @retval IC_SUCCESS if an image has been snapped
  218. # @retval IC_ERROR if something went wrong.
  219. # @retval IC_NOT_IN_LIVEMODE if the live video has not been started.
  220. #
  221. # @sa IC_StartLive
  222. # @sa IC_SetFormat
  223. SnapImage=__tisgrabber.IC_SnapImage
  224. SnapImage.restype = C.c_int
  225. SnapImage.argtypes = (GrabberHandlePtr,
  226. C.c_int,)
  227. # Retrieve the properties of the current video format and sink type
  228. # @param hGrabber The handle to the grabber object.
  229. # @param *lWidth This recieves the width of the image buffer.
  230. # @param *lHeight This recieves the height of the image buffer.
  231. # @param *iBitsPerPixel This recieves the count of bits per pixel.
  232. # @param *format This recieves the current color format.
  233. # @retval IC_SUCCESS on success
  234. # @retval IC_ERROR if something went wrong.
  235. GetImageDescription = __tisgrabber.IC_GetImageDescription
  236. GetImageDescription.restype = C.c_int
  237. GetImageDescription.argtypes = (GrabberHandlePtr,
  238. C.POINTER(C.c_long),
  239. C.POINTER(C.c_long),
  240. C.POINTER(C.c_int),
  241. C.POINTER(C.c_int),)
  242. GetImagePtr = __tisgrabber.IC_GetImagePtr
  243. GetImagePtr.restype = C.c_void_p
  244. GetImagePtr.argtypes = (GrabberHandlePtr,)
  245. # ############################################################################
  246. ShowDeviceSelectionDialog = __tisgrabber.IC_ShowDeviceSelectionDialog
  247. ShowDeviceSelectionDialog.restype = GrabberHandlePtr
  248. ShowDeviceSelectionDialog.argtypes = (GrabberHandlePtr,)
  249. # ############################################################################
  250. ShowPropertyDialog = __tisgrabber.IC_ShowPropertyDialog
  251. ShowPropertyDialog.restype = GrabberHandlePtr
  252. ShowPropertyDialog.argtypes = (GrabberHandlePtr,)
  253. # ############################################################################
  254. IsDevValid = __tisgrabber.IC_IsDevValid
  255. IsDevValid.restype = C.c_int
  256. IsDevValid.argtypes = (GrabberHandlePtr,)
  257. # ############################################################################
  258. LoadDeviceStateFromFile = __tisgrabber.IC_LoadDeviceStateFromFile
  259. LoadDeviceStateFromFile.restype = GrabberHandlePtr
  260. LoadDeviceStateFromFile.argtypes = (GrabberHandlePtr,C.c_char_p)
  261. # ############################################################################
  262. SaveDeviceStateToFile = __tisgrabber.IC_SaveDeviceStateToFile
  263. SaveDeviceStateToFile.restype = C.c_int
  264. SaveDeviceStateToFile.argtypes = (GrabberHandlePtr,C.c_char_p)
  265. GetCameraProperty = __tisgrabber.IC_GetCameraProperty
  266. GetCameraProperty.restype = C.c_int
  267. GetCameraProperty.argtypes = (GrabberHandlePtr,
  268. C.c_int,
  269. C.POINTER(C.c_long),)
  270. SetCameraProperty = __tisgrabber.IC_SetCameraProperty
  271. SetCameraProperty.restype = C.c_int
  272. SetCameraProperty.argtypes = (GrabberHandlePtr,
  273. C.c_int,
  274. C.c_long,)
  275. SetPropertyValue = __tisgrabber.IC_SetPropertyValue
  276. SetPropertyValue.restype = C.c_int
  277. SetPropertyValue.argtypes = (GrabberHandlePtr,
  278. C.c_char_p,
  279. C.c_char_p,
  280. C.c_int, )
  281. GetPropertyValue = __tisgrabber.IC_GetPropertyValue
  282. GetPropertyValue.restype = C.c_int
  283. GetPropertyValue.argtypes = (GrabberHandlePtr,
  284. C.c_char_p,
  285. C.c_char_p,
  286. C.POINTER(C.c_long), )
  287. # ############################################################################
  288. SetPropertySwitch = __tisgrabber.IC_SetPropertySwitch
  289. SetPropertySwitch.restype = C.c_int
  290. SetPropertySwitch.argtypes= (GrabberHandlePtr,
  291. C.c_char_p,
  292. C.c_char_p,
  293. C.c_int,)
  294. GetPropertySwitch = __tisgrabber.IC_GetPropertySwitch
  295. GetPropertySwitch.restype = C.c_int
  296. GetPropertySwitch.argtypes= (GrabberHandlePtr,
  297. C.c_char_p,
  298. C.c_char_p,
  299. C.POINTER(C.c_long),)
  300. # ############################################################################
  301. IsPropertyAvailable = __tisgrabber.IC_IsPropertyAvailable
  302. IsPropertyAvailable.restype = C.c_int
  303. IsPropertyAvailable.argtypes= (GrabberHandlePtr,
  304. C.c_char_p,
  305. C.c_char_p,)
  306. PropertyOnePush = __tisgrabber.IC_PropertyOnePush
  307. PropertyOnePush.restype = C.c_int
  308. PropertyOnePush.argtypes = (GrabberHandlePtr,
  309. C.c_char_p,
  310. C.c_char_p, )
  311. SetPropertyAbsoluteValue = __tisgrabber.IC_SetPropertyAbsoluteValue
  312. SetPropertyAbsoluteValue.restype = C.c_int
  313. SetPropertyAbsoluteValue.argtypes = (GrabberHandlePtr,
  314. C.c_char_p,
  315. C.c_char_p,
  316. C.c_float, )
  317. GetPropertyAbsoluteValue = __tisgrabber.IC_GetPropertyAbsoluteValue
  318. GetPropertyAbsoluteValue.restype = C.c_int
  319. GetPropertyAbsoluteValue.argtypes = (GrabberHandlePtr,
  320. C.c_char_p,
  321. C.c_char_p,
  322. C.POINTER(C.c_float), )
  323. # ############################################################################
  324. EnableCameraAutoProperty = __tisgrabber.IC_EnableAutoCameraProperty
  325. EnableCameraAutoProperty.restype = C.c_int
  326. EnableCameraAutoProperty.argtypes = (GrabberHandlePtr,
  327. C.c_int,
  328. C.c_int)
  329. EnableVideoAutoProperty = __tisgrabber.IC_EnableAutoVideoProperty
  330. EnableVideoAutoProperty.restype = C.c_int
  331. EnableVideoAutoProperty.argtypes = (GrabberHandlePtr,
  332. C.c_int,
  333. C.c_int)
  334. # ############################################################################
  335. # definition of the frameready callback
  336. FRAMEREADYCALLBACK = C.CFUNCTYPE(C.c_void_p,C.c_int, C.POINTER(C.c_ubyte), C.c_ulong, C.py_object)
  337. # set callback function
  338. SetFrameReadyCallback = __tisgrabber.IC_SetFrameReadyCallback
  339. SetFrameReadyCallback.restype = C.c_int
  340. SetFrameReadyCallback.argtypes = [GrabberHandlePtr, FRAMEREADYCALLBACK, C.py_object]
  341. SetContinuousMode = __tisgrabber.IC_SetContinuousMode
  342. SaveImage = __tisgrabber.IC_SaveImage
  343. SaveImage.restype = C.c_int
  344. SaveImage.argtypes = [C.c_void_p, C.c_char_p, C.c_int, C.c_int ]
  345. OpenVideoCaptureDevice = __tisgrabber.IC_OpenVideoCaptureDevice
  346. OpenVideoCaptureDevice.restype = C.c_int
  347. OpenVideoCaptureDevice.argtypes = [C.c_void_p, C.c_char_p]
  348. # ############################################################################
  349. class TIS_CAM(object):
  350. @property
  351. def callback_registered(self):
  352. return self._callback_registered
  353. def __init__(self):
  354. self._handle = C.POINTER(GrabberHandle)
  355. self._handle = TIS_GrabberDLL.create_grabber()
  356. self._callback_registered = False
  357. self._frame = {'num' : -1,
  358. 'ready' : False}
  359. def s(self,strin):
  360. if sys.version[0] == "2":
  361. return strin
  362. if type(strin) == "byte":
  363. return strin
  364. return strin.encode("utf-8")
  365. def SetFrameReadyCallback(self, CallbackFunction, data):
  366. """ Set a callback function, which is called, when a new frame arrives.
  367. CallbackFunction : The callback function
  368. data : a self defined class with user data.
  369. """
  370. return TIS_GrabberDLL.SetFrameReadyCallback( self._handle, CallbackFunction, data )
  371. def SetContinuousMode(self, Mode):
  372. ''' Determines, whether new frames are automatically copied into memory.
  373. :param Mode: If 0, all frames are copied automatically into memory. This is recommened, if the camera runs in trigger mode.
  374. If 1, then snapImages must be called to get a frame into memory.
  375. :return: None
  376. '''
  377. return TIS_GrabberDLL.SetContinuousMode(self._handle, Mode)
  378. def open(self,unique_device_name):
  379. """ Open a device
  380. unique_device_name : The name and serial number of the device to be opened. The device name and serial number are separated by a space.
  381. """
  382. test = TIS_GrabberDLL.open_device_by_unique_name(self._handle,
  383. self.s(unique_device_name))
  384. return test
  385. def ShowDeviceSelectionDialog(self):
  386. self._handle = TIS_GrabberDLL.ShowDeviceSelectionDialog(self._handle)
  387. def ShowPropertyDialog(self):
  388. self._handle = TIS_GrabberDLL.ShowPropertyDialog(self._handle)
  389. def IsDevValid(self):
  390. return TIS_GrabberDLL.IsDevValid(self._handle)
  391. def SetHWND(self, Hwnd):
  392. return TIS_GrabberDLL.SetHWND(self._handle, Hwnd)
  393. def SaveDeviceStateToFile(self, FileName):
  394. return TIS_GrabberDLL.SaveDeviceStateToFile(self._handle, self.s(FileName))
  395. def LoadDeviceStateFromFile(self,FileName):
  396. self._handle = TIS_GrabberDLL.LoadDeviceStateFromFile(self._handle,self.s(FileName))
  397. def SetVideoFormat(self,Format):
  398. return TIS_GrabberDLL.set_videoformat(self._handle, self.s(Format))
  399. def SetFrameRate(self,FPS):
  400. return TIS_GrabberDLL.set_framerate(self._handle, FPS)
  401. def get_video_format_width(self):
  402. return TIS_GrabberDLL.get_video_format_width(self._handle)
  403. def get_video_format_height(self):
  404. return TIS_GrabberDLL.get_video_format_height(self._handle)
  405. def GetDevices(self):
  406. self._Devices=[]
  407. iDevices = TIS_GrabberDLL.get_devicecount()
  408. for i in range(iDevices):
  409. self._Devices.append(TIS_GrabberDLL.get_unique_name_from_list(i))
  410. return self._Devices
  411. def GetVideoFormats(self):
  412. self._Properties=[]
  413. iVideoFormats = TIS_GrabberDLL.GetVideoFormatCount(self._handle)
  414. for i in range(iVideoFormats):
  415. self._Properties.append(TIS_GrabberDLL.GetVideoFormat(self._handle,i))
  416. return self._Properties
  417. def GetInputChannels(self):
  418. self.InputChannels=[]
  419. InputChannelscount = TIS_GrabberDLL.GetInputChannelCount(self._handle)
  420. for i in range (InputChannelscount):
  421. self.InputChannels.append(TIS_GrabberDLL.GetInputChannel(self._handle,i))
  422. return self.InputChannels
  423. def GetVideoNormCount(self):
  424. self.GetVideoNorm=[]
  425. GetVideoNorm_Count=TIS_GrabberDLL.GetVideoNormCount(self._handle)
  426. for i in range(GetVideoNorm_Count):
  427. self.GetVideoNorm.append(TIS_GrabberDLL.GetVideoNorm(self._handle, i))
  428. return self.GetVideoNorm
  429. def SetFormat(self, Format):
  430. ''' SetFormat
  431. Sets the pixel format in memory
  432. @param Format Sinkformat enumeration
  433. '''
  434. TIS_GrabberDLL.SetFormat(self._handle, Format.value)
  435. def GetFormat(self):
  436. val = TIS_GrabberDLL.GetFormat(self._handle)
  437. if val == 0:
  438. return SinkFormats.Y800
  439. if val == 2:
  440. return SinkFormats.RGB32
  441. if val == 1:
  442. return SinkFormats.RGB24
  443. if val == 3:
  444. return SinkFormats.UYVY
  445. if val == 4:
  446. return SinkFormats.Y16
  447. return SinkFormats.RGB24
  448. def StartLive(self, showlive = 1):
  449. """
  450. Start the live video stream.
  451. showlive: 1 : a live video is shown, 0 : the live video is not shown.
  452. """
  453. Error = TIS_GrabberDLL.StartLive(self._handle, showlive)
  454. return Error
  455. def StopLive(self):
  456. """
  457. Stop the live video.
  458. """
  459. Error = TIS_GrabberDLL.StopLive(self._handle)
  460. return Error
  461. def SnapImage(self):
  462. Error = TIS_GrabberDLL.SnapImage(self._handle, 2000)
  463. return Error
  464. def GetImageDescription(self):
  465. lWidth=C.c_long()
  466. lHeight= C.c_long()
  467. iBitsPerPixel=C.c_int()
  468. COLORFORMAT=C.c_int()
  469. Error = TIS_GrabberDLL.GetImageDescription(self._handle, lWidth,
  470. lHeight,iBitsPerPixel,COLORFORMAT)
  471. return (lWidth.value,lHeight.value,iBitsPerPixel.value,COLORFORMAT.value)
  472. def GetImagePtr(self):
  473. ImagePtr = TIS_GrabberDLL.GetImagePtr(self._handle)
  474. return ImagePtr
  475. def GetImage(self):
  476. BildDaten = self.GetImageDescription()[:4]
  477. lWidth=BildDaten[0]
  478. lHeight= BildDaten[1]
  479. iBitsPerPixel=BildDaten[2]//8
  480. buffer_size = lWidth*lHeight*iBitsPerPixel*C.sizeof(C.c_uint8)
  481. img_ptr = self.GetImagePtr()
  482. Bild = C.cast(img_ptr, C.POINTER(C.c_ubyte * buffer_size))
  483. img = np.ndarray(buffer = Bild.contents,
  484. dtype = np.uint8,
  485. shape = (lHeight,
  486. lWidth,
  487. iBitsPerPixel))
  488. return img
  489. def GetImageEx(self):
  490. """ Return a numpy array with the image data tyes
  491. If the sink is Y16 or RGB64 (not supported yet), the dtype in the array is uint16, othereise it is uint8
  492. """
  493. BildDaten = self.GetImageDescription()[:4]
  494. lWidth=BildDaten[0]
  495. lHeight= BildDaten[1]
  496. iBytesPerPixel=BildDaten[2]//8
  497. buffer_size = lWidth*lHeight*iBytesPerPixel*C.sizeof(C.c_uint8)
  498. img_ptr = self.GetImagePtr()
  499. Bild = C.cast(img_ptr, C.POINTER(C.c_ubyte * buffer_size))
  500. pixeltype = np.uint8
  501. if BildDaten[3] == 4: #SinkFormats.Y16:
  502. pixeltype = np.uint16
  503. iBytesPerPixel = 1
  504. img = np.ndarray(buffer = Bild.contents,
  505. dtype = pixeltype,
  506. shape = (lHeight,
  507. lWidth,
  508. iBytesPerPixel))
  509. return img
  510. def GetCameraProperty(self,iProperty):
  511. lFocusPos = C.c_long()
  512. Error = TIS_GrabberDLL.GetCameraProperty(self._handle,iProperty, lFocusPos)
  513. return (lFocusPos.value)
  514. def SetCameraProperty(self,iProperty,iValue):
  515. Error = TIS_GrabberDLL.SetCameraProperty(self._handle,iProperty, iValue)
  516. return (Error)
  517. def SetPropertyValue(self, Property, Element, Value ):
  518. error = TIS_GrabberDLL.SetPropertyValue(self._handle,
  519. self.s(Property),
  520. self.s(Element),
  521. Value)
  522. return error
  523. def GetPropertyValue(self, Property, Element ):
  524. Value = C.c_long()
  525. error = TIS_GrabberDLL.GetPropertyValue(self._handle,
  526. self.s(Property),
  527. self.s(Element),
  528. Value)
  529. return Value.value
  530. def PropertyAvailable(self, Property):
  531. Null = None
  532. error = TIS_GrabberDLL.IsPropertyAvailable(self._handle,
  533. self.s(Property),
  534. Null)
  535. return error
  536. def SetPropertySwitch(self, Property, Element, Value):
  537. error = TIS_GrabberDLL.SetPropertySwitch(self._handle,
  538. self.s(Property),
  539. self.s(Element),
  540. Value)
  541. return error
  542. def GetPropertySwitch(self, Property, Element, Value):
  543. lValue = C.c_long()
  544. error = TIS_GrabberDLL.GetPropertySwitch(self._handle,
  545. self.s(Property),
  546. self.s(Element),
  547. lValue)
  548. Value[0] = lValue.value
  549. return error
  550. def PropertyOnePush(self, Property, Element ):
  551. error = TIS_GrabberDLL.PropertyOnePush(self._handle,
  552. self.s(Property),
  553. self.s(Element ))
  554. return error
  555. def SetPropertyAbsoluteValue(self, Property, Element, Value ):
  556. error = TIS_GrabberDLL.SetPropertyAbsoluteValue(self._handle,
  557. self.s(Property),
  558. self.s(Element),
  559. Value)
  560. return error
  561. def GetPropertyAbsoluteValue(self, Property, Element,Value ):
  562. """ Get a property value of absolute values interface, e.g. seconds or dB.
  563. Example code:
  564. ExposureTime=[0]
  565. Camera.GetPropertyAbsoluteValue("Exposure","Value", ExposureTime)
  566. print("Exposure time in secods: ", ExposureTime[0])
  567. :param Property: Name of the property, e.g. Gain, Exposure
  568. :param Element: Name of the element, e.g. "Value"
  569. :param Value: Object, that receives the value of the property
  570. :returns: 0 on success
  571. """
  572. lValue = C.c_float()
  573. error = TIS_GrabberDLL.GetPropertyAbsoluteValue(self._handle,
  574. self.s(Property),
  575. self.s(Element),
  576. lValue)
  577. Value[0] = lValue.value
  578. return error
  579. def SaveImage(self,FileName, FileType, Quality=75):
  580. ''' Saves the last snapped image. Can by of type BMP or JPEG.
  581. :param FileName : Name of the mage file
  582. :param FileType : Determines file type, can be "JPEG" or "BMP"
  583. :param Quality : If file typ is JPEG, the qualitly can be given from 1 to 100.
  584. :return: Error code
  585. '''
  586. return TIS_GrabberDLL.SaveImage(self._handle, self.s(FileName), IC.ImageFileTypes[self.s(FileType)],Quality)
  587. def openVideoCaptureDevice(self, DeviceName):
  588. ''' Open the device specified by DeviceName
  589. :param DeviceName: Name of the device , e.g. "DFK 72AUC02"
  590. :returns: 1 on success, 0 otherwise.
  591. '''
  592. return TIS_GrabberDLL.OpenVideoCaptureDevice(self._handle, self.s(DeviceName))
  593. def enableCameraAutoProperty(self, property, onoff):
  594. return TIS_GrabberDLL.EnableCameraAutoProperty(self._handle,property,onoff)
  595. def enableVideoAutoProperty(self, property, onoff):
  596. return TIS_GrabberDLL.EnableVideoAutoProperty(self._handle,property,onoff)